I'm currently working on data with 8 waves, each wave is for now stored in it's own dataframe. I've done most of the data cleaning with a lot of repetition, since I couldn't figure out how to make R run the same script over different dataframes. Now I want to do a CFA using Lavaan, and I notice the script is getting very messy, with CFA's being done for multiple variables over all 8 waves. I would love to find a way to simplify my script, so that it's clear for both me and others. Underneath you can find a short example of what the 'long' version of the code would look like, but I'm hoping to get some help in shortening it!
I've tried using a for-loop, but I could not get it to work.
If you have any tips, please let me know so I can stop copy-pasting the same code and replacing the number in the df!
# Example with 2 waves in 2 df
model_ADI_aff <- "aff =~ bds89 + bds39 + bds50 + bds29 + bds84 + bds49 + bds70 + bds88 + bds11 + bds28
"
fit_ADI_aff_1 <- cfa(model_ADI_aff,
data = bds_1,
missing = "fiml",
estimator = "MLR",
se = "robust.huber.white",
test = "yuan.bentler")
summary(fit_ADI_aff_1, standardized = TRUE, fit.measures = TRUE)
modindices(fit_ADI_aff_1, sort.=TRUE, minimum.value=3)
fit_ADI_aff_2 <- cfa(model_ADI_aff,
data = bds_2,
missing = "fiml",
estimator = "MLR",
se = "robust.huber.white",
test = "yuan.bentler")
summary(fit_ADI_aff_2, standardized = TRUE, fit.measures = TRUE)
modindices(fit_ADI_aff_2, sort.=TRUE, minimum.value=3)
...
One way to make it simpler would be putting all your bds_x data.frames
into a list
and then running your model in all of them with lapply()
model_ADI_aff <- "aff =~ bds89 + bds39 + bds50 + bds29 + bds84 + bds49 + bds70 + bds88 + bds11 + bds28"
fun = function(bds){
fit = cfa(model_ADI_aff,
data = bds,
missing = "fiml",
estimator = "MLR",
se = "robust.huber.white",
test = "yuan.bentler")
summ = summary(fit)
mo = modindices(fit_ADI_aff_2, sort.=TRUE, minimum.value=3)
list(fit = fit,summary = summ, modindices = mo)
}
df_list = "list containing all bds"
results = lapply(df_list,fun)
results
will be a list of lists, each containing the fit, summary and modindices for each data.frame
@Fino
I made a few adjustments, and it seems to have done the trick, thank you so much!
fun = function(bds){
fit = cfa(model_ADI_aff,
data = bds,
missing = "fiml",
estimator = "MLR",
se = "robust.huber.white",
test = "yuan.bentler")
summ = summary(fit, standardized = TRUE, fit.measures = TRUE)
mo = modindices(fit, sort.=TRUE, minimum.value=3)
list(fit = fit,summary = summ, modindices = mo)
}
df_list <- list (bds_1, bds_2, bds_3, bds_4, bds_5, bds_6, bds_7, bds_8)
results = lapply(df_list,fun)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.