Andrew Miles
2011-Mar-17 12:50 UTC
[R] Incorrect degrees of freedom in SEM model using lavaan
I have been trying to use lavaan (version 0.4-7) for a simple path model, but the program seems to be computing far less degrees of freedom for my model then it should have. I have 7 variables, which should give (7)(8)/2 28 covariances, and hence 28 DF. The model seems to only think I have 13 DF. The code to reproduce the problem is below. Have I done something wrong, or is this something I should take to the developer? library(lavaan) myCov = matrix(c(24.40, 0, 0, 0, 0, 0, 0, .03, .03, 0, 0, 0, 0, 0, 6.75, - .08, 519.38, 0, 0, 0, 0, .36, .01, 2.74, .18, 0, 0, 0, .51, .0, -.31, .02, .2, .0, 0, -.17, .0, -1.6, -.04, .01, .25, 0, -.1, .02, -.03, .0, -.01, .01 , .02), nrow=7, byrow=TRUE, dimnames=list(c("Internet", "Stress3", "HHincome", "Race", "Age", "Gender", "Stress1"), c("Internet", "Stress3", "HHincome", "Race", "Age", "Gender", "Stress1"))) model = ' Internet ~ HHincome + Race + Age + Gender + Stress1 Stress3 ~ Internet + HHincome + Race + Age + Gender + Stress1 ' fit=sem(model, sample.nobs=161, sample.cov=myCov, mimic.Mplus=FALSE) #check the number of parameters being estimated inspect(fit, what="free") #Note the DF for the Chi-square is 0, when it should be 28-13 = 15 summary(fit) Andrew Miles [[alternative HTML version deleted]]
Gustavo Carvalho
2011-Mar-17 15:19 UTC
[R] Incorrect degrees of freedom in SEM model using lavaan
Your model is saturated. I think lavaan calculates the number of degrees of freedom this way: DF = n*(n + 1)/2 - t - n.fix*(n.fix + 1)/2 n = number of variables t = number of free parameters n.fix = number of fixed exogenous variables So, if you fix the exogenous variables, as in mimic = "Mplus": DF = 28 - 13 - 15 = 0 If you don't, as in mimic = "EQS": DF = 28 - 28 - 0 = 0 You are probably not considering the variances and covariances of the exogenous variables to arrive at 15 degrees of freedom: model = ' Internet + Stress3 ~ HHincome + Race + Age + Gender + Stress1 Stress3 ~ Internet HHincome ~~ 0*Race + 0*Age + 0*Gender + 0*Stress1 Race ~~ 0*Age + 0*Gender + 0*Stress1 Age ~~ 0*Gender + 0*Stress1 Gender ~~ 0*Stress1 HHincome ~~ 1*HHincome Race ~~ 1*Race Age ~~ 1*Age Gender ~~ 1*Gender Stress1 ~~ 1*Stress1 ' fit = lavaan(model, sample.nobs=161, sample.cov=myCov, int.ov.free T, int.lv.free = F, auto.fix.first = T, auto.fix.single = T, auto.resid.var = T, auto.cov.lv.x = T, auto.cov.y = T, mimic = "EQS") summary(fit) Cheers, Gustavo. On Thu, Mar 17, 2011 at 9:50 AM, Andrew Miles <rstuff.miles at gmail.com> wrote:> I have been trying to use lavaan (version 0.4-7) for a simple path model, > but the program seems to be computing far less degrees of freedom for my > model then it should have. ?I have 7 variables, which should give (7)(8)/2 > 28 covariances, and hence 28 DF. ?The model seems to only think I have 13 > DF. ?The code to reproduce the problem is below. ?Have I done something > wrong, or is this something I should take to the developer? > > > library(lavaan) > > myCov = matrix(c(24.40, 0, 0, 0, 0, 0, 0, .03, .03, 0, 0, 0, 0, 0, 6.75, - > .08, 519.38, 0, 0, 0, 0, .36, .01, 2.74, .18, 0, 0, 0, .51, .0, -.31, .02, > .2, .0, 0, -.17, .0, -1.6, -.04, .01, .25, 0, -.1, .02, -.03, .0, -.01, .01 > , .02), nrow=7, byrow=TRUE, dimnames=list(c("Internet", "Stress3", > "HHincome", "Race", "Age", "Gender", "Stress1"), c("Internet", "Stress3", > "HHincome", "Race", "Age", "Gender", "Stress1"))) > > > model = ' > > Internet ~ HHincome + Race + Age + Gender + Stress1 > > Stress3 ~ Internet + HHincome + Race + Age + Gender + Stress1 > > ' > > > fit=sem(model, sample.nobs=161, sample.cov=myCov, mimic.Mplus=FALSE) > > > #check the number of parameters being estimated > > inspect(fit, what="free") > > > #Note the DF for the Chi-square is 0, when it should be 28-13 = 15 > > summary(fit) > > > Andrew Miles > > ? ? ? ?[[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-Mar-17 15:27 UTC
[R] Incorrect degrees of freedom in SEM model using lavaan
Dear Andrew, The reported df in lavaan is 0 which is correct. It is because this path model is saturated. "28" is not the df, it is the no. of pieces of information. The no. of parameter estimates is also 28. Thus, the df is 0. However, you are correct that there are only 13, not 28, free parameters reported by lavaan. It is because "fixed.x=TRUE" was implicitly used in your example (see ?sem). Since HHincome + Race + Age + Gender + Stress1 are predictors, they are fixed at their sample values when "fixed.x=TRUE" was used. And their covariance elements are not involved in calculating the df. If you want to mirror the behaviors in other SEM packages, you may try: fit2 <- sem(model, sample.nobs=161, sample.cov=myCov, fixed.x=FALSE) inspect(fit2, what="free") summary(fit2) Hope it helps. 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, Mar 17, 2011 at 8:50 PM, Andrew Miles <rstuff.miles at gmail.com> wrote:> I have been trying to use lavaan (version 0.4-7) for a simple path model, > but the program seems to be computing far less degrees of freedom for my > model then it should have. ?I have 7 variables, which should give (7)(8)/2 > 28 covariances, and hence 28 DF. ?The model seems to only think I have 13 > DF. ?The code to reproduce the problem is below. ?Have I done something > wrong, or is this something I should take to the developer? > > > library(lavaan) > > myCov = matrix(c(24.40, 0, 0, 0, 0, 0, 0, .03, .03, 0, 0, 0, 0, 0, 6.75, - > .08, 519.38, 0, 0, 0, 0, .36, .01, 2.74, .18, 0, 0, 0, .51, .0, -.31, .02, > .2, .0, 0, -.17, .0, -1.6, -.04, .01, .25, 0, -.1, .02, -.03, .0, -.01, .01 > , .02), nrow=7, byrow=TRUE, dimnames=list(c("Internet", "Stress3", > "HHincome", "Race", "Age", "Gender", "Stress1"), c("Internet", "Stress3", > "HHincome", "Race", "Age", "Gender", "Stress1"))) > > > model = ' > > Internet ~ HHincome + Race + Age + Gender + Stress1 > > Stress3 ~ Internet + HHincome + Race + Age + Gender + Stress1 > > ' > > > fit=sem(model, sample.nobs=161, sample.cov=myCov, mimic.Mplus=FALSE) > > > #check the number of parameters being estimated > > inspect(fit, what="free") > > > #Note the DF for the Chi-square is 0, when it should be 28-13 = 15 > > summary(fit) > > > Andrew Miles > > ? ? ? ?[[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. >