From data to disaggregation to decisions: kfa

Author

NYU Global TIES for Children

Step 1: Load R packages we will be using today

Remember to load your R packages every time you open your workspace in R studio.

#| load packages

library(psych)
library(ggplot2)
library(gtsummary)
library(dplyr)
library(kfa)

Step 2: Read your data into R and check it

It’s important before starting analysis to check that your data is properly formatted. There are a different commands to do so in R - we give examples of two here. More information on these two commands can be found in D2D_descriptive_syntax.

#Load your data
dat <-read.csv(file.path("Data/cint_data.csv"), as.is = T)

#Check your data: Option 1
head(dat)

#Check your data: Option 2
str(dat)

Step 3: Select your items

In this step, you want to create a “mini data set” or data frame that contains only the items in your measure, not the demographic variables. The steps for doing so are fully documented in D2D_descriptive_syntax.

#|item data frame

#Create a character vector with your internalizing item names

cint<-paste0("cint", c(1, 2, 4, 11, 19, 21:24, 27:30))
print(cint)

#Create an internalizing item data frame

cint.dat<-dat %>%
select(all_of(cint))

#Check your new dataframe
str(cint.dat)

lapply(names(cint), function(x) table(cint[[x]]))

#If you have alot of rows with all missing data/NAs, this is a useful command to drop those rows
#cint.dat<-cint.dat[apply(is.na(cint), 1, mean) < 1, ] 

Step 4: Run the kfa

For additional options, see help -> kfa

#|kfa

#basic syntax with ordered categorical data and no missing data
mods.cint <- kfa(cint.dat,
                 ordered = TRUE)

#to add a custom model, write using lavaan syntax
custom2f <- "
f1 =~ cint1 + cint2 + cint4 + cint11 + cint23 + cint27 + cint28 + cint29 + cint30
f2 =~ cint19 + cint21 + cint22 + cint24
"

#add the custom cfa to the model
mods.cint.c2f <- kfa(cint.dat,
                ordered = TRUE,
                custom.cfas = custom2f)

#other options

#mods.cint <- kfa(cint, #your items
#                 k = 3, #number of folds if specified; otherwise calculated automatically based on power analysis
#                 m = 3, #number of factors extracted in the efa if specified, otherwise maximum number of factors is determined by dividing the number of items  by 4 
#                 seed = 165, #ensures randomization is replicable
#                 custom.cfas = custom2f, #adds custom cfa model
#                 rotation = "oblimin", #sets efa rotation; default is oblimin
#                 ordered = TRUE, #if TRUE is specified, treats data as ordered categorical; default is false
#                 estimator = "DWLS", #automatically changes to DWLS if ordered = TRUE
#                 missing = "listwise") #provides options for how to deal with missing data; default is listwise

Step 5: Output the kfa report

The below syntax is commented out because it interferes with this R markdown file! To run, copy and paste the syntax into an R script file and remove the ’#’s

#|kfa report

#kfa_report(mods.cint, 
#           file.name = "kfa/cint/D2D_kfa_cint", 
#           report.format = "html_document", #can also be "word_document"
#           report.title = "D2D_kfa_cint"
#          )