n.g<-500
group<-gl(2,n.g)
# Two independent variables
x1<-rnorm(n.g*2)
x2<-rnorm(n.g*2)
# One dependent variable with same coefficients for both groups
y1<-0.5*x1+2*x2+rnorm(n.g*2,sd=0.1)
# Another dependent variable, with different coefficients for both groups
y2<-ifelse(group==1,-0.5,0.5)*x1+ifelse(group==1,-2,2)*x2+rnorm(n.g*2,sd=0.1)
d.f<-data.frame(x1=x1,x2=x2,y1=y1,y2=y2,group=group)
mod.1="
y1~x1+x2
y2~x1+x2
"
# Saturated model
sem.1<-cfa(mod.1,data = d.f,group = "group")
summary(sem.1)
## lavaan (0.5-23.1097) converged normally after 171 iterations
##
## Number of observations per group
## 1 500
## 2 500
##
## Estimator ML
## Minimum Function Test Statistic 0.000
## Degrees of freedom 0
## Minimum Function Value 0.0000000000000
##
## Chi-square for each group:
##
## 1 0.000
## 2 0.000
##
## Parameter Estimates:
##
## Information Expected
## Standard Errors Standard
##
##
## Group 1 [1]:
##
## Regressions:
## Estimate Std.Err z-value P(>|z|)
## y1 ~
## x1 0.501 0.004 117.886 0.000
## x2 1.998 0.004 451.474 0.000
## y2 ~
## x1 -0.502 0.004 -117.898 0.000
## x2 -1.995 0.004 -450.083 0.000
##
## Covariances:
## Estimate Std.Err z-value P(>|z|)
## .y1 ~~
## .y2 0.000 0.000 0.713 0.476
##
## Intercepts:
## Estimate Std.Err z-value P(>|z|)
## .y1 0.003 0.004 0.642 0.521
## .y2 0.013 0.004 2.949 0.003
##
## Variances:
## Estimate Std.Err z-value P(>|z|)
## .y1 0.009 0.001 15.811 0.000
## .y2 0.009 0.001 15.811 0.000
##
##
## Group 2 [2]:
##
## Regressions:
## Estimate Std.Err z-value P(>|z|)
## y1 ~
## x1 0.497 0.005 108.354 0.000
## x2 2.001 0.004 459.332 0.000
## y2 ~
## x1 0.504 0.005 106.650 0.000
## x2 2.001 0.004 445.604 0.000
##
## Covariances:
## Estimate Std.Err z-value P(>|z|)
## .y1 ~~
## .y2 -0.000 0.000 -0.851 0.395
##
## Intercepts:
## Estimate Std.Err z-value P(>|z|)
## .y1 -0.005 0.004 -1.197 0.231
## .y2 0.004 0.005 0.852 0.394
##
## Variances:
## Estimate Std.Err z-value P(>|z|)
## .y1 0.010 0.001 15.811 0.000
## .y2 0.010 0.001 15.811 0.000
# Model with equal regression coefficients
sem.2<-cfa(mod.1,data = d.f,group = "group",group.equal=c("regressions"))
summary(sem.2)
## lavaan (0.5-23.1097) converged normally after 381 iterations
##
## Number of observations per group
## 1 500
## 2 500
##
## Estimator ML
## Minimum Function Test Statistic 3692.662
## Degrees of freedom 4
## P-value (Chi-square) 0.000
##
## Chi-square for each group:
##
## 1 0.568
## 2 3692.094
##
## Parameter Estimates:
##
## Information Expected
## Standard Errors Standard
##
##
## Group 1 [1]:
##
## Regressions:
## Estimate Std.Err z-value P(>|z|)
## y1 ~
## x1 (.p1.) 0.499 0.003 160.111 0.000
## x2 (.p2.) 1.999 0.003 644.264 0.000
## y2 ~
## x1 (.p3.) -0.501 0.004 -117.854 0.000
## x2 (.p4.) -1.992 0.004 -449.655 0.000
##
## Covariances:
## Estimate Std.Err z-value P(>|z|)
## .y1 ~~
## .y2 0.000 0.000 0.716 0.474
##
## Intercepts:
## Estimate Std.Err z-value P(>|z|)
## .y1 0.003 0.004 0.663 0.507
## .y2 0.013 0.004 3.010 0.003
##
## Variances:
## Estimate Std.Err z-value P(>|z|)
## .y1 0.009 0.001 15.811 0.000
## .y2 0.009 0.001 15.811 0.000
##
##
## Group 2 [2]:
##
## Regressions:
## Estimate Std.Err z-value P(>|z|)
## y1 ~
## x1 (.p1.) 0.499 0.003 160.111 0.000
## x2 (.p2.) 1.999 0.003 644.264 0.000
## y2 ~
## x1 (.p3.) -0.501 0.004 -117.854 0.000
## x2 (.p4.) -1.992 0.004 -449.655 0.000
##
## Covariances:
## Estimate Std.Err z-value P(>|z|)
## .y1 ~~
## .y2 0.007 0.018 0.363 0.717
##
## Intercepts:
## Estimate Std.Err z-value P(>|z|)
## .y1 -0.005 0.004 -1.143 0.253
## .y2 0.236 0.182 1.293 0.196
##
## Variances:
## Estimate Std.Err z-value P(>|z|)
## .y1 0.010 0.001 15.811 0.000
## .y2 16.607 1.050 15.811 0.000
# The fit is horrible
fitMeasures(sem.2)
## npar fmin chisq
## 14.000 1.846 3692.662
## df pvalue baseline.chisq
## 4.000 0.000 12092.301
## baseline.df baseline.pvalue cfi
## 10.000 0.000 0.695
## tli nnfi rfi
## 0.237 0.237 0.237
## nfi pnfi ifi
## 0.695 0.278 0.695
## rni logl unrestricted.logl
## 0.695 -2864.559 -1018.228
## aic bic ntotal
## 5757.118 5825.826 1000.000
## bic2 rmsea rmsea.ci.lower
## 5781.362 1.358 1.321
## rmsea.ci.upper rmsea.pvalue rmr
## 1.395 0.000 3.000
## rmr_nomean srmr srmr_bentler
## 3.000 0.648 0.648
## srmr_bentler_nomean srmr_bollen srmr_bollen_nomean
## 0.767 0.270 0.320
## srmr_mplus srmr_mplus_nomean cn_05
## 0.595 0.704 3.569
## cn_01 gfi agfi
## 4.595 1.000 1.000
## pgfi mfi
## 0.143 0.158
# The difference between the saturated and equal regression coefficients model is significant
anova(sem.1,sem.2)
## Chi Square Difference Test
##
## Df AIC BIC Chisq Chisq diff Df diff Pr(>Chisq)
## sem.1 0 2072.5 2160.8 0.0
## sem.2 4 5757.1 5825.8 3692.7 3692.7 4 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Let's try to model correctly this. The first coefficients are fixed between
# group on first variable, not on second one
mod.2="
y1~c(v1,v1)*x1+c(v2,v2)*x2
y2~x1+x2
"
sem.3<-cfa(mod.2,data = d.f,group = "group")
summary(sem.3)
## lavaan (0.5-23.1097) converged normally after 196 iterations
##
## Number of observations per group
## 1 500
## 2 500
##
## Estimator ML
## Minimum Function Test Statistic 0.655
## Degrees of freedom 2
## P-value (Chi-square) 0.721
##
## Chi-square for each group:
##
## 1 0.325
## 2 0.330
##
## Parameter Estimates:
##
## Information Expected
## Standard Errors Standard
##
##
## Group 1 [1]:
##
## Regressions:
## Estimate Std.Err z-value P(>|z|)
## y1 ~
## x1 (v1) 0.499 0.003 160.153 0.000
## x2 (v2) 2.000 0.003 644.488 0.000
## y2 ~
## x1 -0.502 0.004 -117.941 0.000
## x2 -1.995 0.004 -450.189 0.000
##
## Covariances:
## Estimate Std.Err z-value P(>|z|)
## .y1 ~~
## .y2 0.000 0.000 0.714 0.475
##
## Intercepts:
## Estimate Std.Err z-value P(>|z|)
## .y1 0.003 0.004 0.683 0.495
## .y2 0.013 0.004 2.950 0.003
##
## Variances:
## Estimate Std.Err z-value P(>|z|)
## .y1 0.009 0.001 15.811 0.000
## .y2 0.009 0.001 15.811 0.000
##
##
## Group 2 [2]:
##
## Regressions:
## Estimate Std.Err z-value P(>|z|)
## y1 ~
## x1 (v1) 0.499 0.003 160.153 0.000
## x2 (v2) 2.000 0.003 644.488 0.000
## y2 ~
## x1 0.504 0.005 106.674 0.000
## x2 2.001 0.004 445.774 0.000
##
## Covariances:
## Estimate Std.Err z-value P(>|z|)
## .y1 ~~
## .y2 -0.000 0.000 -0.851 0.395
##
## Intercepts:
## Estimate Std.Err z-value P(>|z|)
## .y1 -0.005 0.004 -1.153 0.249
## .y2 0.004 0.005 0.850 0.395
##
## Variances:
## Estimate Std.Err z-value P(>|z|)
## .y1 0.010 0.001 15.811 0.000
## .y2 0.010 0.001 15.811 0.000
# The fit is perfect
fitMeasures(sem.3)
## npar fmin chisq
## 16.000 0.000 0.655
## df pvalue baseline.chisq
## 2.000 0.721 12092.301
## baseline.df baseline.pvalue cfi
## 10.000 0.000 1.000
## tli nnfi rfi
## 1.001 1.001 1.000
## nfi pnfi ifi
## 1.000 0.200 1.000
## rni logl unrestricted.logl
## 1.000 -1018.556 -1018.228
## aic bic ntotal
## 2069.111 2147.635 1000.000
## bic2 rmsea rmsea.ci.lower
## 2096.818 0.000 0.000
## rmsea.ci.upper rmsea.pvalue rmr
## 0.064 0.903 0.002
## rmr_nomean srmr srmr_bentler
## 0.002 0.000 0.000
## srmr_bentler_nomean srmr_bollen srmr_bollen_nomean
## 0.001 0.000 0.000
## srmr_mplus srmr_mplus_nomean cn_05
## 0.000 0.000 9146.910
## cn_01 gfi agfi
## 14060.491 1.000 1.000
## pgfi mfi
## 0.071 1.001
# And the differents between saturated and equal regression coefficients model is non-significant
anova(sem.1,sem.3)
## Chi Square Difference Test
##
## Df AIC BIC Chisq Chisq diff Df diff Pr(>Chisq)
## sem.1 0 2072.5 2160.8 0.0000
## sem.3 2 2069.1 2147.6 0.6551 0.6551 2 0.7207