I have multiple Mplus CFA model outputs (cfa.out) from MplusAutomation. I want to create a R function based on these procedure. Can anyone guide me how to write a function in R? All the process will be the same except the output file (cfa.out here). I tried several different ways, but it didn't work well.
library(MplusAutomation)
library(dplyr)
library(stringr)
library(tidyr)
dat <-readModels("cfa.out", what = "parameters")$parameters
dat2 <- filter(as.data.frame(dat$unstandardized), grepl("BY", paramHeader))
#loadings
loading <- dat2 %>%
filter(grepl("BY", paramHeader)) %>%
select(c(param, est)) %>%
separate(param, c("loading","time")) %>%
mutate(time = paste0('time',time)) %>%
spread(loading, est)
Let's assume my dat2 is this;
paramHeader <- c('F1_3.BY','F1_3.BY','F1_3.BY','F1_15.BY','F1_15.BY', 'F1_15.BY',
'F1_25.BY','F1_25.BY','F1_25.BY','F1_35.BY','F1_35.BY','F1_35.BY',
'F1_47.BY','F1_47.BY','F1_47.BY','F1_57.BY','F1_57.BY','F1_57.BY',
'Intercepts','Intercepts','Intercepts','Intercepts','Intercepts',
'Intercepts','Intercepts','Intercepts','Intercepts','Intercepts',
'Intercepts','Intercepts','Intercepts','Intercepts','Intercepts',
'Intercepts','Intercepts','Intercepts')
param <- c('V1_3','V2_3','V3_3','V1_15','V2_15', 'V3_15',
'V1_25','V2_25','V3_25','V1_35','V2_35','V3_35',
'V1_47','V2_47','V3_47','V1_57','V2_57','V3_57',
'V1_3','V2_3','V3_3','V1_15','V2_15', 'V3_15',
'V1_25','V2_25','V3_25','V1_35','V2_35','V3_35',
'V1_47','V2_47','V3_47','V1_57','V2_57','V3_57')
est <- c(1,1.122,.917,1,1.069,.867,1,1.066,.917,1,1.048,.994,1,1.03,.937,1,1.133,1.032,
1,1.122,.917,1,1.069,.867,1,1.066,.917,1,1.048,.994,1,1.03,.937,1,1.133,1.032)
se <- c(0,.044,.045,0,.036,.039,0,.034,.037,0,.034,.037,0,.031,.034,0,036,.037,
0,.044,.045,0,.036,.039,0,.034,.037,0,.034,.037,0,.031,.034,0,036,.037)
est_se <- c(999,25.466,20.385,999,29.751,22.478,999,31.134,24.565,999,31.18,26.587,999,33.637,27.405,999,31.883,28.081,999,25.466,20.385,999,29.751,22.478,999,31.134,24.565,999,31.18,26.587,999,33.637,27.405,999,31.883,28.081)
pval <- c(999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,
999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0)
dat <- data.frame(paramHeader, param,est,se,est_se,pval)
dat$param <- as.character(dat$param)
dat2