Reputation: 17
Relatively new to r, and I'm trying to figure out how to run an identical analysis for two different sets of variables. This isn't my data, but here's how my code looks right now:
library(lavaan)
names(PoliticalDemocracy)
listoftype <- c("x", "y")
anexample <- function(tst) {
model <- '
tst3 ~
+ tst1
+ tst2
'
runmodel <- lavaan::sem(model,
data = PoliticalDemocracy,
missing = "default",
estimator = "ML",
se = "default",
)
lavaan::summary(runmodel, fit.measures=FALSE)
modelsummary <-lavaan::summary(runmodel, fit.measures=FALSE)
write(paste(utils::capture.output(summary(runmodel)),
collapse = "\n"), file = paste0("output_",tst.txt))
tst <-as.data.frame(modelsummary)
}
lapply(listoftype, FUN = anexample)
You can probably figure out what I'm trying to do, but the basic idea is that I'm trying to run the first analysis with x3, x2, and x1 and then the second one with y3, y2, and y1. I am trying to insert either "x" or "y" wherever I have "tst". I tried doing it with a loop as well (which is what I would use in other statistical packages) but that didn't work either, and my understanding is that lapply would work better for this purpose if I could ever figure it out.
So far, the error message I am getting is:
lavaan ERROR: missing observed variables in dataset: tst3 tst1 tst2
So clearly, R doesn't recognize that it's supposed to insert "x" or "y" where "tst" is located. Does anyone have a suggestion on how to get this to run? Thanks.
Upvotes: 0
Views: 313
Reputation: 11514
The problem in your case is that you do not actually substitute x
or y
for tst
. See e.g.
library(stringr)
anexample <- function(tst) {
model <- 'tst3 ~ + tst1 + tst2'
model <- str_replace_all(model, 'tst', tst)
return(model)
}
lapply(listoftype, FUN = anexample)
[[1]]
[1] "x3 ~ + x1 + x2"
[[2]]
[1] "y3 ~ + y1 + y2"
So, the following should work:
anexample <- function(tst) {
model <- 'tst3 ~ + tst1 + tst2'
model <- str_replace_all(model, 'tst', tst)
runmodel <- lavaan::sem(model,
data = PoliticalDemocracy,
missing = "default",
estimator = "ML",
se = "default",
)
lavaan::summary(runmodel, fit.measures=FALSE)
modelsummary <-lavaan::summary(runmodel, fit.measures=FALSE)
write(paste(utils::capture.output(summary(runmodel)),
collapse = "\n"), file = paste0("output_",tst, '.txt'))
tst <-as.data.frame(modelsummary)
}
lapply(listoftype, FUN = anexample)
To get your results with the appropriate name, have a look at the following as an example:
anexample <- function(tst) {
model <- str_replace_all('tst3 ~ + tst1 + tst2', 'tst', tst)
runmodel <- lavaan::sem(model,
data = PoliticalDemocracy,
missing = "default",
estimator = "ML",
se = "default",
)
modelsummary <-lavaan::summary(runmodel, fit.measures=FALSE)
return(as.data.frame(modelsummary))
}
output <- setNames(lapply(listoftype, anexample), listoftype)
str(output)
List of 2
$ x:'data.frame': 6 obs. of 8 variables:
..$ PE.lhs : chr [1:6] "x3" "x3" "x3" "x1" ...
..$ PE.op : chr [1:6] "~" "~" "~~" "~~" ...
...
$ y:'data.frame': 6 obs. of 8 variables:
..$ PE.lhs : chr [1:6] "y3" "y3" "y3" "y1" ...
..$ PE.op : chr [1:6] "~" "~" "~~" "~~" ...
...
This gives you a list with both x
and y
. To 'move' this into the global environment, you can additionally use list2env(output, globalenv())
.
Upvotes: 1