Solved – lavaan works with `cfa`, provides error with lavaan(method = ‘cfa’)

lavaanrstructural-equation-modeling

Estimating a simple Confirmatory Factor Analysis (CFA) model with lavaan gives inconsistent results when I use cfa and lavaan(method = "cfa").

library(lavaan)

10 observations, four indicators

foo <-  structure(list(ind1 = c(0.2, -1, -0.2, 0.6, 0.6, -0.2, -0.2, 
                            -1, 0.6, 0.2), 
                   ind2 = c(0.6, -0.2, -0.2, 0.6, 0.6, -0.2, -1,
                            -1, 0.6, 0.6),
                   ind3 = c(0.6, -0.2, -0.2, 0.6, 0.2, 0.2, -0.6, 
                            0.6, 0.6, 0.6), 
                   ind4 = c(1, -0.5, -1, 0.5, 0.5, -0.5, 0, 0, -0.5, 
                            0.5)), 
              codepage = 65001L, .Names = c("ind1", "ind2", "ind3", "ind4"),
              row.names = 1:10,
              class = "data.frame")

This model

cfa(model = 'latent =~ ind1 + ind2 + ind3 + ind4', data = foo)

Works fine. But this, which I thought was equivalent

lavaan(model = 'latent =~ ind1 + ind2 + ind3 + ind4',
   data = foo, 
   model.type = "cfa")

causes an error:

Error in lav_model_estimate(lavmodel = lavmodel, lavsamplestats = lavsamplestats,  : 
  lavaan ERROR: initial model-implied matrix (Sigma) is not positive definite;
  check your model and/or starting parameters.

What am I missing?

Best Answer

The cfa() function is a wrapper for lavaan, which (among other things) adds the arguments

auto.fix.first = TRUE
auto.var = TRUE

Changing your model type to CFA changes a few cosmetic things, but not the basic model. Because the lavaan() command has not put these in, you need to add the arguments, or add the appropriate parameters to the model.

lavaan(model = 'latent =~ 1*ind1 + ind2 + ind3 + ind4
                latent ~~ latent
                ind1 ~~ ind1
                ind2 ~~ ind2
                ind3 ~~ ind3
                ind4 ~~ ind4',
       data = foo, 
       model.type = "cfa")

That seems like more work, but I prefer to (almost) always use the lavaan() function - the trouble with using cfa() is that it does some of the work for you, but you can forget what it has done, and what you want to do. You can therefore screw up by, say, fixing the variance of the latent to 1 AND fixing the first loading to 1 (because the second thing is done automatically, you might forget). But cfa() is much easier for simple models that you're not going to futz around with much.