D. Alain
2011-Jun-01 10:31 UTC
[R] error in model specification for cfa with lavaan-package
Dear R-List, (I am not sure whether this list is the right place for my question...) I have a dataframe df.cfa df.cfa<-data.frame(x1=c(5,4,1,5,5,NA,4,NA,NA,5),x2=c(2,3,3,3,NA,1,2,1,2,1),x3=c(5,3,4,1,5,5,5,5,5,5),x4=c(5,3,4,5,5,5,5,5,5,5),x5=c(5,4,3,3,4,4,4,5,NA,5),x6=c(3,5,2,1,4,NA,NA,5,3,4),x7=c(5,4,3,4,4,3,4,NA,NA,4),x8=c(5,5,3,4,5,4,5,4,5,5),x9=c(5,1,1,1,NA,3,3,2,2,3),x10=c(5,5,2,3,4,3,1,3,2,5),x11=c(5,4,4,5,5,4,5,4,5,5),x12=c(5,4,5,5,5,4,5,5,5,5),x13=c(5,4,4,5,5,5,5,5,5,5),x14=c(5,3,3,NA,5,4,5,3,NA,5),x15=c(4,4,4,3,4,5,2,4,5,5),x16=c(5,4,3,3,4,4,3,3,4,5)) and I want to carry out a confirmatory factor analysis using the "cfa" function (lavaan). library(lavaan) then I specify my model cfa.model<-' f1=~x1+x2+x3+x4+x5 f2=~x6+x7+x8+x9+x10+x11 f3=~x12+x13+x14+x15+x16 ' and run the cfa routine fit<-cfa(cfa.model,data=df.cfa,missing="ml") The output is an error message (here partly in german) "Error in chol.default(S) : der führende Minor der Ordnung 4 ist nicht positiv definit Fehler in Sample(data = data, group = group, sample.cov = sample.cov, sample.mean = sample.mean, : sample covariance can not be inverted" Then I tried to "lavaanify" my model specification first cfa.model<- lavaanify(cfa.model) id lhs op rhs user group free ustart fixed.x label eq.id free.uncon 1 1 f1 =~ x1 1 1 1 NA 0 f1=~x1 0 1 2 2 f1 =~ x2 1 1 2 NA 0 f1=~x2 0 2 3 3 f1 =~ x3 1 1 3 NA 0 f1=~x3 0 3 4 4 f1 =~ x4 1 1 4 NA 0 f1=~x4 0 4 5 5 f1 =~ x5 1 1 5 NA 0 f1=~x5 0 5 6 6 f2 =~ x6 1 1 6 NA 0 f2=~x6 0 6 7 7 f2 =~ x7 1 1 7 NA 0 f2=~x7 0 7 8 8 f2 =~ x8 1 1 8 NA 0 f2=~x8 0 8 9 9 f2 =~ x9 1 1 9 NA 0 f2=~x9 0 9 10 10 f2 =~ x10 1 1 10 NA 0 f2=~x10 0 10 11 11 f2 =~ x11 1 1 11 NA 0 f2=~x11 0 11 12 12 f3 =~ x12 1 1 12 NA 0 f3=~x12 0 12 13 13 f3 =~ x13 1 1 13 NA 0 f3=~x13 0 13 14 14 f3 =~ x14 1 1 14 NA 0 f3=~x14 0 14 15 15 f3 =~ x15 1 1 15 NA 0 f3=~x15 0 15 16 16 f3 =~ x16 1 1 16 NA 0 f3=~x16 0 16 I run cfa again fit<-cfa(cfa.model,data=df.cfa,missing="ml") And this time I get another error information "Error in start.idx[i]:end.idx[i] : NA/NaN Argument" I must admit that I am stuck, can anyone help? Thanks, Alain [[alternative HTML version deleted]]
Mike Cheung
2011-Jun-01 10:44 UTC
[R] error in model specification for cfa with lavaan-package
Dear Alain, There were 16 variables with 10 cases with missing values. The sample covariance matrix is not positive definite. It has nothing to do with lavaan. You need more cases before you can fit a CFA with 16 variables. Regards, Mike -- --------------------------------------------------------------------- Mike W.L. Cheung Phone: (65) 6516-3702 Department of Psychology Fax: (65) 6773-1843 National University of Singapore http://courses.nus.edu.sg/course/psycwlm/internet/ --------------------------------------------------------------------- On Wed, Jun 1, 2011 at 6:31 PM, D. Alain <dialvac-r@yahoo.de> wrote:> Dear R-List, > > (I am not sure whether this list is the right place for my question...) > > I have a dataframe df.cfa > > > df.cfa<-data.frame(x1=c(5,4,1,5,5,NA,4,NA,NA,5),x2=c(2,3,3,3,NA,1,2,1,2,1),x3=c(5,3,4,1,5,5,5,5,5,5),x4=c(5,3,4,5,5,5,5,5,5,5),x5=c(5,4,3,3,4,4,4,5,NA,5),x6=c(3,5,2,1,4,NA,NA,5,3,4),x7=c(5,4,3,4,4,3,4,NA,NA,4),x8=c(5,5,3,4,5,4,5,4,5,5),x9=c(5,1,1,1,NA,3,3,2,2,3),x10=c(5,5,2,3,4,3,1,3,2,5),x11=c(5,4,4,5,5,4,5,4,5,5),x12=c(5,4,5,5,5,4,5,5,5,5),x13=c(5,4,4,5,5,5,5,5,5,5),x14=c(5,3,3,NA,5,4,5,3,NA,5),x15=c(4,4,4,3,4,5,2,4,5,5),x16=c(5,4,3,3,4,4,3,3,4,5)) > > > and I want to carry out a confirmatory factor analysis using the "cfa" > function (lavaan). > > library(lavaan) > > then I specify my model > > cfa.model<-' f1=~x1+x2+x3+x4+x5 > f2=~x6+x7+x8+x9+x10+x11 > f3=~x12+x13+x14+x15+x16 ' > > and run the cfa routine > > fit<-cfa(cfa.model,data=df.cfa,missing="ml") > > The output is an error message (here partly in german) > > "Error in chol.default(S) : der führende Minor der Ordnung 4 ist nicht > positiv definit > Fehler in Sample(data = data, group = group, sample.cov = sample.cov, > sample.mean = sample.mean, : sample covariance can not be inverted" > > Then I tried to "lavaanify" my model specification first > > cfa.model<- lavaanify(cfa.model) > > id lhs op rhs user group free ustart fixed.x label eq.id free.uncon > 1 1 f1 =~ x1 1 1 1 NA 0 f1=~x1 0 1 > 2 2 f1 =~ x2 1 1 2 NA 0 f1=~x2 0 2 > 3 3 f1 =~ x3 1 1 3 NA 0 f1=~x3 0 3 > 4 4 f1 =~ x4 1 1 4 NA 0 f1=~x4 0 4 > 5 5 f1 =~ x5 1 1 5 NA 0 f1=~x5 0 5 > 6 6 f2 =~ x6 1 1 6 NA 0 f2=~x6 0 6 > 7 7 f2 =~ x7 1 1 7 NA 0 f2=~x7 0 7 > 8 8 f2 =~ x8 1 1 8 NA 0 f2=~x8 0 8 > 9 9 f2 =~ x9 1 1 9 NA 0 f2=~x9 0 9 > 10 10 f2 =~ x10 1 1 10 NA 0 f2=~x10 0 10 > 11 11 f2 =~ x11 1 1 11 NA 0 f2=~x11 0 11 > 12 12 f3 =~ x12 1 1 12 NA 0 f3=~x12 0 12 > 13 13 f3 =~ x13 1 1 13 NA 0 f3=~x13 0 13 > 14 14 f3 =~ x14 1 1 14 NA 0 f3=~x14 0 14 > 15 15 f3 =~ x15 1 1 15 NA 0 f3=~x15 0 15 > 16 16 f3 =~ x16 1 1 16 NA 0 f3=~x16 0 16 > > I run cfa again > > fit<-cfa(cfa.model,data=df.cfa,missing="ml") > > And this time I get another error information > > "Error in start.idx[i]:end.idx[i] : NA/NaN Argument" > > I must admit that I am stuck, can anyone help? > > Thanks, Alain > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >[[alternative HTML version deleted]]
yrosseel
2011-Jun-01 13:13 UTC
[R] error in model specification for cfa with lavaan-package
Dear Alain, As for the first error ("sample covariance can not be inverted"): Mike is right: with only 10 observations and 16 variables, the ML estimation of the sample cov produces a covariance matrix that is not positive definite, and hence the inversion (deliberately) fails. The lesson for me is that lavaan should simply check for this situation (p > N), and refuse to proceed (with a gentle and more informative error message). As for the second error ("Error in start.idx[i]:end.idx[i] : NA/NaN Argument"): the first argument of cfa() must be a model syntax, not a lavaanified object. The 'lavaanify' function is useful to learn about lavaan, and to export the model specification to other software packages, but you should not pass a 'lavaanified' object to the cfa/sem/growth functions directly (although this may work in the future). Moreover, to get a 'complete' parameter list, you need to specify a number of additional arguments: model <- model <- lavaanify(cfa.model, auto.fix.first = TRUE, auto.var = TRUE, auto.cov.lv.x = TRUE) (see the 'Details' section in the man page of cfa() for a complete list). Hope this helps, Yves. On 06/01/2011 12:31 PM, D. Alain wrote:> Dear R-List, > > (I am not sure whether this list is the right place for my > question...) > > I have a dataframe df.cfa > > df.cfa<-data.frame(x1=c(5,4,1,5,5,NA,4,NA,NA,5),x2=c(2,3,3,3,NA,1,2,1,2,1),x3=c(5,3,4,1,5,5,5,5,5,5),x4=c(5,3,4,5,5,5,5,5,5,5),x5=c(5,4,3,3,4,4,4,5,NA,5),x6=c(3,5,2,1,4,NA,NA,5,3,4),x7=c(5,4,3,4,4,3,4,NA,NA,4),x8=c(5,5,3,4,5,4,5,4,5,5),x9=c(5,1,1,1,NA,3,3,2,2,3),x10=c(5,5,2,3,4,3,1,3,2,5),x11=c(5,4,4,5,5,4,5,4,5,5),x12=c(5,4,5,5,5,4,5,5,5,5),x13=c(5,4,4,5,5,5,5,5,5,5),x14=c(5,3,3,NA,5,4,5,3,NA,5),x15=c(4,4,4,3,4,5,2,4,5,5),x16=c(5,4,3,3,4,4,3,3,4,5)) > > > > > > and I want to carry out a confirmatory factor analysis using the > "cfa" function (lavaan). > > library(lavaan) > > then I specify my model > > cfa.model<-' f1=~x1+x2+x3+x4+x5 f2=~x6+x7+x8+x9+x10+x11 > f3=~x12+x13+x14+x15+x16 ' > > and run the cfa routine > > fit<-cfa(cfa.model,data=df.cfa,missing="ml") > > The output is an error message (here partly in german) > > "Error in chol.default(S) : der f?hrende Minor der Ordnung 4 ist > nicht positiv definit Fehler in Sample(data = data, group = group, > sample.cov = sample.cov, sample.mean = sample.mean, : sample > covariance can not be inverted" > > Then I tried to "lavaanify" my model specification first > > cfa.model<- lavaanify(cfa.model) > > id lhs op rhs user group free ustart fixed.x label eq.id > free.uncon 1 1 f1 =~ x1 1 1 1 NA 0 f1=~x1 0 > 1 2 2 f1 =~ x2 1 1 2 NA 0 f1=~x2 0 > 2 3 3 f1 =~ x3 1 1 3 NA 0 f1=~x3 0 > 3 4 4 f1 =~ x4 1 1 4 NA 0 f1=~x4 0 > 4 5 5 f1 =~ x5 1 1 5 NA 0 f1=~x5 0 > 5 6 6 f2 =~ x6 1 1 6 NA 0 f2=~x6 0 > 6 7 7 f2 =~ x7 1 1 7 NA 0 f2=~x7 0 > 7 8 8 f2 =~ x8 1 1 8 NA 0 f2=~x8 0 > 8 9 9 f2 =~ x9 1 1 9 NA 0 f2=~x9 0 > 9 10 10 f2 =~ x10 1 1 10 NA 0 f2=~x10 0 > 10 11 11 f2 =~ x11 1 1 11 NA 0 f2=~x11 0 > 11 12 12 f3 =~ x12 1 1 12 NA 0 f3=~x12 0 > 12 13 13 f3 =~ x13 1 1 13 NA 0 f3=~x13 0 > 13 14 14 f3 =~ x14 1 1 14 NA 0 f3=~x14 0 > 14 15 15 f3 =~ x15 1 1 15 NA 0 f3=~x15 0 > 15 16 16 f3 =~ x16 1 1 16 NA 0 f3=~x16 0 > 16 > > I run cfa again > > fit<-cfa(cfa.model,data=df.cfa,missing="ml") > > And this time I get another error information > > "Error in start.idx[i]:end.idx[i] : NA/NaN Argument" > > I must admit that I am stuck, can anyone help? > > Thanks, Alain [[alternative HTML version deleted]] > > > > > ______________________________________________ R-help at r-project.org > mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do > read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Mike Cheung
2011-Jun-02 03:15 UTC
[R] error in model specification for cfa with lavaan-package
Dear Alain, You may speed up the analysis by using the sample covariance matrix based on a listwise deletion: cov.cfa <- cov(your.raw.data, use="complete.obs") Since you have 36671 cases, the results should be similar to those based on the raw data unless you have lots of missing data and/or the data are missing at random. By the way, if your questions are directly related to SEM, you may get more responses from SEMNET (http://alabamamaps.ua.edu/archives/semnet.html). Hope it helps. Regards, Mike -- --------------------------------------------------------------------- Mike W.L. Cheung Phone: (65) 6516-3702 Department of Psychology Fax: (65) 6773-1843 National University of Singapore http://courses.nus.edu.sg/course/psycwlm/internet/ --------------------------------------------------------------------- On Thu, Jun 2, 2011 at 1:21 AM, yrosseel <yrosseel@gmail.com> wrote:> On 06/01/2011 05:39 PM, D. Alain wrote: > >> Thank you Yves and Mike, >> >> your comments make sence, however they do not resolved my problem: The p >> < N is the result of my poor attempt to give a reproducible example. My >> "real" dataframe has a dim of 36671 cases an 41 variables. >> Following your advice, Yves, I passed my model without lavaanification >> (just "cfa(cfa.model, data=df.cfa,missing="ml")"), but now R is working >> for hours without printing any results... >> > > To monitor progress, you can use the verbose=TRUE argument. If you are not > fitting your final model, you may want to use se="none" (no standard errors) > and test="none" (no test statistic) to speed things up. > > I've ran analyses that took multiple days, using both lavaan and commercial > software. It is annoying (the wait), but not unusual. The time to fit is a > function of the number of missing patterns you have in your data, and the > number of variables. > > Yves. >[[alternative HTML version deleted]]