I am currently try to use the marginal maximum likelihood estimation techniques to fit a categorical factor analysis model but I constantly run into this error:
Here you can find an example of the code I am using both to generate the data and estimate the model:
sampleSize <- 1000 #subjects per group
Groups <- 2
#setting aribtrary thresholds
thresh1 <- c(-2,0,2)
thresh2 <- c(-1,0.5,1.6)
thresh3 <- c(-1.5,0.9,1.8)
thresh4 <- c(-1.7,0.3,1.4)
thresh5 <- c(-2.1,0.8,1.7)
thresh6 <- c(-1.2,0.4,1.2)
#use a factor structure as data generating mechanism
F <- matrix(c(.9,.8, .7 , .6, .5, .8), ncol = 1) #the model
rownames(F) <- paste('V', seq(1:6), sep = '')#add labels
colnames(F) <- c('F1')
R <- F%*%t(F) # create the correlation matrix
diag(R) <- 1 # adjust the diagonal to the matrix
R
#Create two groups using the same starting values and add a grouping variable
Data <- data.frame(NA)
Data2facTot <- list(NA)
DataOrdered2fac <- list()
for (i in 1:Groups){
Data2facTot[[i]]<- as.data.frame(rmvnorm(sampleSize, sigma = R))
# Make categorical:
Data2facTot[[i]][,1] <- as.numeric(cut(Data2facTot[[i]][,1],breaks = c(-Inf,thresh1,Inf)))
Data2facTot[[i]][,2] <- as.numeric(cut(Data2facTot[[i]][,2],breaks = c(-Inf,thresh2,Inf)))
Data2facTot[[i]][,3] <- as.numeric(cut(Data2facTot[[i]][,3],breaks = c(-Inf,thresh3,Inf)))
Data2facTot[[i]][,4] <- as.numeric(cut(Data2facTot[[i]][,4],breaks = c(-Inf,thresh4,Inf)))
Data2facTot[[i]][,5] <- as.numeric(cut(Data2facTot[[i]][,5],breaks = c(-Inf,thresh5,Inf)))
Data2facTot[[i]][,6] <- as.numeric(cut(Data2facTot[[i]][,6],breaks = c(-Inf,thresh6,Inf)))
#Now calculate polychoric correlation
DataOrdered2fac[[i]] <- Data2facTot[[i]]
DataOrdered2fac[[i]][,1] <- ordered(Data2facTot[[i]][,1])
DataOrdered2fac[[i]][,2] <- ordered(Data2facTot[[i]][,2])
DataOrdered2fac[[i]][,3] <- ordered(Data2facTot[[i]][,3])
DataOrdered2fac[[i]][,4] <- ordered(Data2facTot[[i]][,4])
DataOrdered2fac[[i]][,5] <- ordered(Data2facTot[[i]][,5])
DataOrdered2fac[[i]][,6] <- ordered(Data2facTot[[i]][,6])
names(DataOrdered2fac[[i]]) <- c("a","b",'c','d','e','f')
}
Groupingvar <- c(rep('Tomatoes',sampleSize), rep('Potatoes', sampleSize))
AggregatedData <- Reduce(rbind, DataOrdered2fac)
AggregatedData$Groupingvar <- Groupingvar
AggregatedDataNum <- Reduce(rbind, Data2facTot)
AggregatedDataNum$Groupingvar <- Groupingvar
lavCor(DataOrdered2fac)
#compare lavcor with original cor mat generated from 2 factor model
lavCor(DataOrdered2fac)
R
#################################################################################################
#FIT THE MODEL CAN BE DONE USING DIFFERENT SETS OF CONSTRAINTS, THIS IS LAVAAN DEFAULT
#MODEL
Model2fac <- '
#Latent variable definition
F1 =~ a + b + c + d + e + f
#thresholds
a | t1 + t2 + t3
b | t1 + t2 + t3
c | t1 + t2 + t3
d | t1 + t2 + t3
e | t1 + t2 + t3
f | t1 + t2 + t3
'
#FIT DEFAULT LAVAAN MODEL
fit <- cfa(Model2fac,
AggregatedData,
group = 'Groupingvar',
#group.equal = 'thresholds',
estimator = 'MML')