I have a bunch of lists that define scale make-up. For instance, here's a simple example of a fictional "scale1" with a total factor and two subscales:
myList1 <- list(scale1.tot=c("s1.item1", "s1.item2", "s1.item3", "s1.item4", "s1.item5", "s1.item6"),
scale1.sub1=c("s1.item1", "s1.item2", "s1.item3"),
scale1.sub2=c("s1.item4", "s1.item5", "s1.item6"))
I've already defined these lists because I am using the score.items() function of the psych package, and these lists instruct R how to calculate scale scores. Now I would like to transform these lists into model specifications:
mod1 <- '
scale1.tot =~ s1.item1+s1.item2+s1.item3+s1.item4+s1.item5+s1.item6
'
I want to loop through all of my lists a vector called scales and go from myList1 to mod1 as shown. Here's what I've tried.
# data
s1.item1 <- sample(1:10, 50, replace=TRUE)
s1.item2 <- sample(1:10, 50, replace=TRUE)
s1.item3 <- sample(1:10, 50, replace=TRUE)
s1.item4 <- sample(1:10, 50, replace=TRUE)
s1.item5 <- sample(1:10, 50, replace=TRUE)
s1.item6 <- sample(1:10, 50, replace=TRUE)
dat.test <- as.data.frame(cbind(s1.item1, s1.item2, s1.item3, s1.item4,
s1.item5, s1.item6))
# list
myList1 <- list(scale1.tot=c("s1.item1", "s1.item2", "s1.item3", "s1.item4", "s1.item5", "s1.item6"),
scale1.sub1=c("s1.item1", "s1.item2", "s1.item3"),
scale1.sub2=c("s1.item4", "s1.item5", "s1.item6"))
# vector of lists
scales <- c("myList1", "myList2", "myList3") # other lists not shown
# loop over scale lists
for (s in scales) {
myList <- get(s)
# loop over elements of list
for (i in 1:length(myList)) {
lhs <- names(myList)[i]
rhs <- paste(myList[[i]], collapse="+")
mod <-
as.name(paste0("'", lhs, "=~", rhs, "'", sep=" "))
assign(paste("mod", names(myList)[i], sep="."), mod)
}
remove(myList)
remove(lhs)
remove(rhs)
remove(mod)
}
This gives me several objects, including mod.scale1.tot. It prints as:
`'scale1.tot=~s1.item1+s1.item2+s1.item3+s1.item4+s1.item5+s1.item6' `
If I assign this object to mod.test, I can try to fit the model. I want to do this as part of the loop as well, but it's easier to show just one iteration.
mod.test <- mod.scale1.tot
fit.test <- cfa(mod.test, data = dat.test)
When I run this, I get the following error:
object 'FLAT' not found
Any idea what I am doing wrong? Is there a better approach to the task?