简体   繁体   English

使用Lavaan的多个数据帧的R循环

[英]R loop for several dataframes using Lavaan

I'm currently working on data with 8 waves, each wave is for now stored in it's own dataframe. 我目前正在处理8个波形的数据,现在每个波形都存储在自己的数据框中。 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. 我已经进行了很多重复的大多数数据清理工作,因为我不知道如何使R在不同的数据帧上运行相同的脚本。 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. 现在,我想使用Lavaan进行CFA,并且我注意到脚本变得非常混乱,因为在所有8个波中都针对多个变量执行了CFA。 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. 我已经尝试过使用for循环,但是无法正常工作。

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! 如果您有任何提示,请告诉我,这样我就可以停止复制粘贴相同的代码并替换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() 一种简化的方法是将所有bds_x data.frames放入list ,然后使用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 results将是列表的列表,每个都包含用于每个配合,摘要和modindices 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM