R: Hand-calculating CFA with Maximum Likelihood Estimation

How can I obtain ML estimates for CFA loadings and error variances in R? Example approach:

initLoad <- rep(0.7, 6)
initError <- diag(0.3, 6)
objectiveFunc <- function(S, L, D) {
  modSigma <- L %*% t(L) + D
  log(det(modSigma)) + sum(diag(S %*% solve(modSigma))) - log(det(S)) - 6
}

Another method that worked well in my experience was using the ‘nlminb’ optimizer in R. I set up the same likelihood objective function and then pass the constraints for ensuring the modified covariance matrix stays positive definite directly in the ‘nlminb’ options. This approach can handle the parameter boundaries better and avoids some convergence issues I observed with other methods. My experiments indicated that this method provides robust estimates even with less ideal initial values, so it might be worth giving a try.

i uesd optim() in r to obtain ml estimates by refactoring your objective and proper inits; ensure modsigma stays positve. tweaking method like ‘BFGS’ worked for me. good luck!