library(lavaan)
## Warning: 程辑包'lavaan'是用R版本4.3.1 来建造的
## This is lavaan 0.6-15
## lavaan is FREE software! Please report any bugs.

验证性因素分析

View(HolzingerSwineford1939)

#结构对应的代码放在引号中,每行末尾回车

HS.model <- '
visual  =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed   =~ x7 + x8 + x9
'

#cfa函数进行验证性因素分析,要指定模型和数据
fit <- cfa(HS.model, data = HolzingerSwineford1939)

#输出结果
summary(fit, fit.measures = TRUE, standardized = TRUE, modindices=TRUE)
## lavaan 0.6.15 ended normally after 35 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        21
## 
##   Number of observations                           301
## 
## Model Test User Model:
##                                                       
##   Test statistic                                85.306
##   Degrees of freedom                                24
##   P-value (Chi-square)                           0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                               918.852
##   Degrees of freedom                                36
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.931
##   Tucker-Lewis Index (TLI)                       0.896
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -3737.745
##   Loglikelihood unrestricted model (H1)      -3695.092
##                                                       
##   Akaike (AIC)                                7517.490
##   Bayesian (BIC)                              7595.339
##   Sample-size adjusted Bayesian (SABIC)       7528.739
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.092
##   90 Percent confidence interval - lower         0.071
##   90 Percent confidence interval - upper         0.114
##   P-value H_0: RMSEA <= 0.050                    0.001
##   P-value H_0: RMSEA >= 0.080                    0.840
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.065
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   visual =~                                                             
##     x1                1.000                               0.900    0.772
##     x2                0.554    0.100    5.554    0.000    0.498    0.424
##     x3                0.729    0.109    6.685    0.000    0.656    0.581
##   textual =~                                                            
##     x4                1.000                               0.990    0.852
##     x5                1.113    0.065   17.014    0.000    1.102    0.855
##     x6                0.926    0.055   16.703    0.000    0.917    0.838
##   speed =~                                                              
##     x7                1.000                               0.619    0.570
##     x8                1.180    0.165    7.152    0.000    0.731    0.723
##     x9                1.082    0.151    7.155    0.000    0.670    0.665
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   visual ~~                                                             
##     textual           0.408    0.074    5.552    0.000    0.459    0.459
##     speed             0.262    0.056    4.660    0.000    0.471    0.471
##   textual ~~                                                            
##     speed             0.173    0.049    3.518    0.000    0.283    0.283
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .x1                0.549    0.114    4.833    0.000    0.549    0.404
##    .x2                1.134    0.102   11.146    0.000    1.134    0.821
##    .x3                0.844    0.091    9.317    0.000    0.844    0.662
##    .x4                0.371    0.048    7.779    0.000    0.371    0.275
##    .x5                0.446    0.058    7.642    0.000    0.446    0.269
##    .x6                0.356    0.043    8.277    0.000    0.356    0.298
##    .x7                0.799    0.081    9.823    0.000    0.799    0.676
##    .x8                0.488    0.074    6.573    0.000    0.488    0.477
##    .x9                0.566    0.071    8.003    0.000    0.566    0.558
##     visual            0.809    0.145    5.564    0.000    1.000    1.000
##     textual           0.979    0.112    8.737    0.000    1.000    1.000
##     speed             0.384    0.086    4.451    0.000    1.000    1.000
## 
## Modification Indices:
## 
##        lhs op rhs     mi    epc sepc.lv sepc.all sepc.nox
## 1   visual =~  x4  1.211  0.077   0.069    0.059    0.059
## 2   visual =~  x5  7.441 -0.210  -0.189   -0.147   -0.147
## 3   visual =~  x6  2.843  0.111   0.100    0.092    0.092
## 4   visual =~  x7 18.631 -0.422  -0.380   -0.349   -0.349
## 5   visual =~  x8  4.295 -0.210  -0.189   -0.187   -0.187
## 6   visual =~  x9 36.411  0.577   0.519    0.515    0.515
## 7  textual =~  x1  8.903  0.350   0.347    0.297    0.297
## 8  textual =~  x2  0.017 -0.011  -0.011   -0.010   -0.010
## 9  textual =~  x3  9.151 -0.272  -0.269   -0.238   -0.238
## 10 textual =~  x7  0.098 -0.021  -0.021   -0.019   -0.019
## 11 textual =~  x8  3.359 -0.121  -0.120   -0.118   -0.118
## 12 textual =~  x9  4.796  0.138   0.137    0.136    0.136
## 13   speed =~  x1  0.014  0.024   0.015    0.013    0.013
## 14   speed =~  x2  1.580 -0.198  -0.123   -0.105   -0.105
## 15   speed =~  x3  0.716  0.136   0.084    0.075    0.075
## 16   speed =~  x4  0.003 -0.005  -0.003   -0.003   -0.003
## 17   speed =~  x5  0.201 -0.044  -0.027   -0.021   -0.021
## 18   speed =~  x6  0.273  0.044   0.027    0.025    0.025
## 19      x1 ~~  x2  3.606 -0.184  -0.184   -0.233   -0.233
## 20      x1 ~~  x3  0.935 -0.139  -0.139   -0.203   -0.203
## 21      x1 ~~  x4  3.554  0.078   0.078    0.173    0.173
## 22      x1 ~~  x5  0.522 -0.033  -0.033   -0.067   -0.067
## 23      x1 ~~  x6  0.048  0.009   0.009    0.020    0.020
## 24      x1 ~~  x7  5.420 -0.129  -0.129   -0.195   -0.195
## 25      x1 ~~  x8  0.634 -0.041  -0.041   -0.079   -0.079
## 26      x1 ~~  x9  7.335  0.138   0.138    0.247    0.247
## 27      x2 ~~  x3  8.532  0.218   0.218    0.223    0.223
## 28      x2 ~~  x4  0.534 -0.034  -0.034   -0.052   -0.052
## 29      x2 ~~  x5  0.023 -0.008  -0.008   -0.011   -0.011
## 30      x2 ~~  x6  0.785  0.039   0.039    0.062    0.062
## 31      x2 ~~  x7  8.918 -0.183  -0.183   -0.192   -0.192
## 32      x2 ~~  x8  0.054 -0.012  -0.012   -0.017   -0.017
## 33      x2 ~~  x9  1.895  0.075   0.075    0.094    0.094
## 34      x3 ~~  x4  0.142 -0.016  -0.016   -0.028   -0.028
## 35      x3 ~~  x5  7.858 -0.130  -0.130   -0.212   -0.212
## 36      x3 ~~  x6  1.855  0.055   0.055    0.100    0.100
## 37      x3 ~~  x7  0.638 -0.044  -0.044   -0.054   -0.054
## 38      x3 ~~  x8  0.059 -0.012  -0.012   -0.019   -0.019
## 39      x3 ~~  x9  4.126  0.102   0.102    0.147    0.147
## 40      x4 ~~  x5  2.534  0.186   0.186    0.457    0.457
## 41      x4 ~~  x6  6.220 -0.235  -0.235   -0.646   -0.646
## 42      x4 ~~  x7  5.920  0.098   0.098    0.180    0.180
## 43      x4 ~~  x8  3.805 -0.069  -0.069   -0.162   -0.162
## 44      x4 ~~  x9  0.196 -0.016  -0.016   -0.035   -0.035
## 45      x5 ~~  x6  0.916  0.101   0.101    0.253    0.253
## 46      x5 ~~  x7  1.233 -0.049  -0.049   -0.083   -0.083
## 47      x5 ~~  x8  0.347  0.023   0.023    0.049    0.049
## 48      x5 ~~  x9  0.999  0.040   0.040    0.079    0.079
## 49      x6 ~~  x7  0.259 -0.020  -0.020   -0.037   -0.037
## 50      x6 ~~  x8  0.275  0.018   0.018    0.043    0.043
## 51      x6 ~~  x9  0.097 -0.011  -0.011   -0.024   -0.024
## 52      x7 ~~  x8 34.145  0.536   0.536    0.859    0.859
## 53      x7 ~~  x9  5.183 -0.187  -0.187   -0.278   -0.278
## 54      x8 ~~  x9 14.946 -0.423  -0.423   -0.805   -0.805
#fit.measures = TRUE,输出拟合指数
#standardized = TRUE,输出标准化系数
#modindices=TRUE,输出修正指数

多组的验证性因素分析

HS.model <- '
visual  =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed   =~ x7 + x8 + x9
'

fit1 <- cfa(HS.model, group='sex',data = HolzingerSwineford1939)
fit2 <- cfa(HS.model, group='sex',data = HolzingerSwineford1939, group.equal = "loadings")
fit3 <- cfa(HS.model, group='sex',data = HolzingerSwineford1939, group.equal = c("intercepts", "loadings"))
fit4<- cfa(HS.model, group='sex',data = HolzingerSwineford1939, group.equal = c("intercepts","loadings" ,"residuals"))

# group.equal指定两组样本之间哪些参数等值


lavTestLRT(fit1, fit2, fit3,fit4)
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff    RMSEA Df diff Pr(>Chisq)   
## fit1 48 7517.6 7740.1 105.80                                          
## fit2 54 7526.1 7726.2 126.23     20.431 0.126416       6   0.002320 **
## fit3 60 7532.8 7710.8 145.00     18.771 0.118926       6   0.004567 **
## fit4 69 7537.0 7681.5 167.11     22.115 0.098401       9   0.008521 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

多组CFA中不等值的原因

# 上述分析表明,男女样本只有结构等值,没有因子负荷等值,接下来寻找原因,究竟哪个因子不相等呢?

HS.model <- '
visual  =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed   =~ x7 + x8 + x9
'

# 结构等值模型
fit1 <- cfa(HS.model, group='sex',data = HolzingerSwineford1939)

# 限定某些因子相等/不相等,以确定哪些模型与结构等值模型没有显著差异

HS1.model <- '
visual  =~ c(a1, a2)*x1 + b*x2 + c*x3
textual =~ d*x4 + e*x5 + f*x6
speed   =~ g*x7 + h*x8 + i*x9
'

# 相等的因子负荷,使用一个字母即可;不相等的因子负荷,使用c(a1,a2)类的向量

# 受限制的因子负荷等值模型
fit2 <- cfa(HS1.model, group='sex',data = HolzingerSwineford1939)
## Warning in lavaanify(model = FLAT, constraints = constraints, varTable = DataOV, : lavaan WARNING: using a single label per parameter in a multiple group
##   setting implies imposing equality constraints across all the groups;
##   If this is not intended, either remove the label(s), or use a vector
##   of labels (one for each group);
##   See the Multiple groups section in the man page of model.syntax.
#检验模型1和2是否有差异,如果没有,说明新增加的限定模型2与结构等值模型1效果相同,因为1和2的差异只有a1和a2不相等,其他都相等。
lavTestLRT(fit1, fit2)
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff   RMSEA Df diff Pr(>Chisq)   
## fit1 48 7517.6 7740.1 105.80                                         
## fit2 54 7526.1 7726.2 126.23     20.431 0.12642       6    0.00232 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 依次检验b、c、d、e、f、g、h和i是否相等,最终找出不相等的因子负荷。

# 也有人建议采用如下方式,更方便地操作代码


HS1.model <- '
visual  =~ c(a1,a2)*x1 + c(b,b)*x2 + c(c,c)*x3
textual =~ c(d,d)*x4 + c(e,e)*x5 + c(f,f)*x6
speed   =~ c(g,g)*x7 + c(h,h)*x8 + c(i,i)*x9
'
HS2.model <- '
visual  =~ c(a,a)*x1 + c(b1,b2)*x2 + c(c,c)*x3
textual =~ c(d,d)*x4 + c(e,e)*x5 + c(f,f)*x6
speed   =~ c(g,g)*x7 + c(h,h)*x8 + c(i,i)*x9
'

HS3.model <- '
visual  =~ c(a,a)*x1 + c(b,b)*x2 + c(c1,c2)*x3
textual =~ c(d,d)*x4 + c(e,e)*x5 + c(f,f)*x6
speed   =~ c(g,g)*x7 + c(h,h)*x8 + c(i,i)*x9
'

HS4.model <- '
visual  =~ c(a,a)*x1 + c(b,b)*x2 + c(c,c)*x3
textual =~ c(d1,d2)*x4 + c(e,e)*x5 + c(f,f)*x6
speed   =~ c(g,g)*x7 + c(h,h)*x8 + c(i,i)*x9
'

HS5.model <- '
visual  =~ c(a1,a2)*x1 + c(b,b)*x2 + c(c,c)*x3
textual =~ c(d,d)*x4 + c(e1,e1)*x5 + c(f,f)*x6
speed   =~ c(g,g)*x7 + c(h,h)*x8 + c(i,i)*x9
'
HS6.model <- '
visual  =~ c(a,a)*x1 + c(b,b)*x2 + c(c,c)*x3
textual =~ c(d,d)*x4 + c(e,e)*x5 + c(f1,f2)*x6
speed   =~ c(g,g)*x7 + c(h,h)*x8 + c(i,i)*x9
'
HS7.model <- '
visual  =~ c(a,a)*x1 + c(b,b)*x2 + c(c,c)*x3
textual =~ c(d,d)*x4 + c(e,e)*x5 + c(f,f)*x6
speed   =~ c(g1,g2)*x7 + c(h,h)*x8 + c(i,i)*x9
'

HS8.model <- '
visual  =~ c(a,a)*x1 + c(b,b)*x2 + c(c,c)*x3
textual =~ c(d,d)*x4 + c(e,e)*x5 + c(f,f)*x6
speed   =~ c(g,g)*x7 + c(h1,h2)*x8 + c(i,i)*x9
'
HS9.model <- '
visual  =~ c(a,a)*x1 + c(b,b)*x2 + c(c,c)*x3
textual =~ c(d,d)*x4 + c(e,e)*x5 + c(f,f)*x6
speed   =~ c(g,g)*x7 + c(h,h)*x8 + c(i1,i2)*x9
'
HS<-c(HS1.model,HS2.model,HS3.model,HS4.model,HS5.model,HS6.model,HS7.model,HS8.model,HS9.model)

# 避免重复工作,可以使用循环

for(i in HS){
  print(i)
  fit2 <- cfa(i, group='sex',data = HolzingerSwineford1939)
  print(lavTestLRT(fit1, fit2))
}
## [1] "\nvisual  =~ c(a1,a2)*x1 + c(b,b)*x2 + c(c,c)*x3\ntextual =~ c(d,d)*x4 + c(e,e)*x5 + c(f,f)*x6\nspeed   =~ c(g,g)*x7 + c(h,h)*x8 + c(i,i)*x9\n"
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff   RMSEA Df diff Pr(>Chisq)   
## fit1 48 7517.6 7740.1 105.80                                         
## fit2 54 7526.1 7726.2 126.23     20.431 0.12642       6    0.00232 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## [1] "\nvisual  =~ c(a,a)*x1 + c(b1,b2)*x2 + c(c,c)*x3\ntextual =~ c(d,d)*x4 + c(e,e)*x5 + c(f,f)*x6\nspeed   =~ c(g,g)*x7 + c(h,h)*x8 + c(i,i)*x9\n"
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff   RMSEA Df diff Pr(>Chisq)   
## fit1 48 7517.6 7740.1 105.80                                         
## fit2 53 7527.0 7730.9 125.16     19.361 0.13815       5   0.001646 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## [1] "\nvisual  =~ c(a,a)*x1 + c(b,b)*x2 + c(c1,c2)*x3\ntextual =~ c(d,d)*x4 + c(e,e)*x5 + c(f,f)*x6\nspeed   =~ c(g,g)*x7 + c(h,h)*x8 + c(i,i)*x9\n"
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff   RMSEA Df diff Pr(>Chisq)   
## fit1 48 7517.6 7740.1 105.80                                         
## fit2 53 7525.0 7728.9 123.21     17.417 0.12846       5   0.003773 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## [1] "\nvisual  =~ c(a,a)*x1 + c(b,b)*x2 + c(c,c)*x3\ntextual =~ c(d1,d2)*x4 + c(e,e)*x5 + c(f,f)*x6\nspeed   =~ c(g,g)*x7 + c(h,h)*x8 + c(i,i)*x9\n"
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff   RMSEA Df diff Pr(>Chisq)   
## fit1 48 7517.6 7740.1 105.80                                         
## fit2 54 7526.1 7726.2 126.23     20.431 0.12642       6    0.00232 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## [1] "\nvisual  =~ c(a1,a2)*x1 + c(b,b)*x2 + c(c,c)*x3\ntextual =~ c(d,d)*x4 + c(e1,e1)*x5 + c(f,f)*x6\nspeed   =~ c(g,g)*x7 + c(h,h)*x8 + c(i,i)*x9\n"
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff   RMSEA Df diff Pr(>Chisq)   
## fit1 48 7517.6 7740.1 105.80                                         
## fit2 54 7526.1 7726.2 126.23     20.431 0.12642       6    0.00232 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## [1] "\nvisual  =~ c(a,a)*x1 + c(b,b)*x2 + c(c,c)*x3\ntextual =~ c(d,d)*x4 + c(e,e)*x5 + c(f1,f2)*x6\nspeed   =~ c(g,g)*x7 + c(h,h)*x8 + c(i,i)*x9\n"
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC Chisq Chisq diff   RMSEA Df diff Pr(>Chisq)   
## fit1 48 7517.6 7740.1 105.8                                         
## fit2 53 7527.9 7731.8 126.1     20.306 0.14262       5   0.001095 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## [1] "\nvisual  =~ c(a,a)*x1 + c(b,b)*x2 + c(c,c)*x3\ntextual =~ c(d,d)*x4 + c(e,e)*x5 + c(f,f)*x6\nspeed   =~ c(g1,g2)*x7 + c(h,h)*x8 + c(i,i)*x9\n"
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff   RMSEA Df diff Pr(>Chisq)   
## fit1 48 7517.6 7740.1 105.80                                         
## fit2 54 7526.1 7726.2 126.23     20.431 0.12642       6    0.00232 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## [1] "\nvisual  =~ c(a,a)*x1 + c(b,b)*x2 + c(c,c)*x3\ntextual =~ c(d,d)*x4 + c(e,e)*x5 + c(f,f)*x6\nspeed   =~ c(g,g)*x7 + c(h1,h2)*x8 + c(i,i)*x9\n"
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff    RMSEA Df diff Pr(>Chisq)  
## fit1 48 7517.6 7740.1 105.80                                         
## fit2 53 7519.6 7723.5 117.79     11.999 0.096443       5     0.0348 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## [1] "\nvisual  =~ c(a,a)*x1 + c(b,b)*x2 + c(c,c)*x3\ntextual =~ c(d,d)*x4 + c(e,e)*x5 + c(f,f)*x6\nspeed   =~ c(g,g)*x7 + c(h,h)*x8 + c(i1,i2)*x9\n"
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff RMSEA Df diff Pr(>Chisq)
## fit1 48 7517.6 7740.1 105.80                                    
## fit2 53 7512.0 7715.9 110.12     4.3273     0       5     0.5033
# 结果表明,当限定了x9系数不相等时,模型与结构等值模型没有差异。说明,男女生的x9在speed的负荷有显著差异,可以进一步讨论此题在放映因子时的男女生差异


#也可以采用group.partial逐一排除

HS.model <- '
visual  =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed   =~ x7 + x8 + x9
'

# 因子负荷相等,但是排除x1在visual上的因子负荷
fit2 <- cfa(HS.model, group='sex',data = HolzingerSwineford1939,group.equal = "loadings", group.partial="visual=~x1")

# 为了避免代码重复,可以建立一个向量,每次排除一个,然后循环执行

excluded<-c('visual=~x1','visual=~x2','visual=~x3','textual=~x4','textual=~x5','textual=~x6','speed=~x7','speed=~x8','speed=~x9')

for (i in excluded){
  print(paste('排除',i))
  fit1 <- cfa(HS.model, group='sex',data = HolzingerSwineford1939)
  fit2<- cfa(HS.model, group='sex',data = HolzingerSwineford1939,group.equal = "loadings", group.partial=i)
  print(lavTestLRT(fit1, fit2))

}
## [1] "排除 visual=~x1"
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff   RMSEA Df diff Pr(>Chisq)   
## fit1 48 7517.6 7740.1 105.80                                         
## fit2 54 7526.1 7726.2 126.23     20.431 0.12642       6    0.00232 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## [1] "排除 visual=~x2"
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff   RMSEA Df diff Pr(>Chisq)   
## fit1 48 7517.6 7740.1 105.80                                         
## fit2 53 7527.0 7730.9 125.16     19.361 0.13815       5   0.001646 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## [1] "排除 visual=~x3"
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff   RMSEA Df diff Pr(>Chisq)   
## fit1 48 7517.6 7740.1 105.80                                         
## fit2 53 7525.0 7728.9 123.21     17.417 0.12846       5   0.003773 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## [1] "排除 textual=~x4"
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff   RMSEA Df diff Pr(>Chisq)   
## fit1 48 7517.6 7740.1 105.80                                         
## fit2 54 7526.1 7726.2 126.23     20.431 0.12642       6    0.00232 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## [1] "排除 textual=~x5"
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff   RMSEA Df diff Pr(>Chisq)   
## fit1 48 7517.6 7740.1 105.80                                         
## fit2 53 7527.5 7731.4 125.67     19.878 0.14061       5   0.001317 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## [1] "排除 textual=~x6"
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC Chisq Chisq diff   RMSEA Df diff Pr(>Chisq)   
## fit1 48 7517.6 7740.1 105.8                                         
## fit2 53 7527.9 7731.8 126.1     20.306 0.14262       5   0.001095 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## [1] "排除 speed=~x7"
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff   RMSEA Df diff Pr(>Chisq)   
## fit1 48 7517.6 7740.1 105.80                                         
## fit2 54 7526.1 7726.2 126.23     20.431 0.12642       6    0.00232 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## [1] "排除 speed=~x8"
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff    RMSEA Df diff Pr(>Chisq)  
## fit1 48 7517.6 7740.1 105.80                                         
## fit2 53 7519.6 7723.5 117.79     11.999 0.096443       5     0.0348 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## [1] "排除 speed=~x9"
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC  Chisq Chisq diff RMSEA Df diff Pr(>Chisq)
## fit1 48 7517.6 7740.1 105.80                                    
## fit2 53 7512.0 7715.9 110.12     4.3273     0       5     0.5033

结构方程模型分析

model <- '
# measurement model 
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + y2 + y3 + y4
dem65 =~ y5 + y6 + y7 + y8 
# regressions 
dem60 ~ ind60 
dem65 ~ ind60 + dem60 
'
fit <- sem(model, data = PoliticalDemocracy)
summary(fit, fit.measures = TRUE, standardized = TRUE)
## lavaan 0.6.15 ended normally after 42 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        25
## 
##   Number of observations                            75
## 
## Model Test User Model:
##                                                       
##   Test statistic                                72.462
##   Degrees of freedom                                41
##   P-value (Chi-square)                           0.002
## 
## Model Test Baseline Model:
## 
##   Test statistic                               730.654
##   Degrees of freedom                                55
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.953
##   Tucker-Lewis Index (TLI)                       0.938
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1564.959
##   Loglikelihood unrestricted model (H1)      -1528.728
##                                                       
##   Akaike (AIC)                                3179.918
##   Bayesian (BIC)                              3237.855
##   Sample-size adjusted Bayesian (SABIC)       3159.062
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.101
##   90 Percent confidence interval - lower         0.061
##   90 Percent confidence interval - upper         0.139
##   P-value H_0: RMSEA <= 0.050                    0.021
##   P-value H_0: RMSEA >= 0.080                    0.827
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.055
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   ind60 =~                                                              
##     x1                1.000                               0.669    0.920
##     x2                2.182    0.139   15.714    0.000    1.461    0.973
##     x3                1.819    0.152   11.956    0.000    1.218    0.872
##   dem60 =~                                                              
##     y1                1.000                               2.201    0.845
##     y2                1.354    0.175    7.755    0.000    2.980    0.760
##     y3                1.044    0.150    6.961    0.000    2.298    0.705
##     y4                1.300    0.138    9.412    0.000    2.860    0.860
##   dem65 =~                                                              
##     y5                1.000                               2.084    0.803
##     y6                1.258    0.164    7.651    0.000    2.623    0.783
##     y7                1.282    0.158    8.137    0.000    2.673    0.819
##     y8                1.310    0.154    8.529    0.000    2.730    0.847
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   dem60 ~                                                               
##     ind60             1.474    0.392    3.763    0.000    0.448    0.448
##   dem65 ~                                                               
##     ind60             0.453    0.220    2.064    0.039    0.146    0.146
##     dem60             0.864    0.113    7.671    0.000    0.913    0.913
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .x1                0.082    0.020    4.180    0.000    0.082    0.154
##    .x2                0.118    0.070    1.689    0.091    0.118    0.053
##    .x3                0.467    0.090    5.174    0.000    0.467    0.240
##    .y1                1.942    0.395    4.910    0.000    1.942    0.286
##    .y2                6.490    1.185    5.479    0.000    6.490    0.422
##    .y3                5.340    0.943    5.662    0.000    5.340    0.503
##    .y4                2.887    0.610    4.731    0.000    2.887    0.261
##    .y5                2.390    0.447    5.351    0.000    2.390    0.355
##    .y6                4.343    0.796    5.456    0.000    4.343    0.387
##    .y7                3.510    0.668    5.252    0.000    3.510    0.329
##    .y8                2.940    0.586    5.019    0.000    2.940    0.283
##     ind60             0.448    0.087    5.169    0.000    1.000    1.000
##    .dem60             3.872    0.893    4.338    0.000    0.799    0.799
##    .dem65             0.115    0.200    0.575    0.565    0.026    0.026
# 查看修正指数

modindices(fit, minimum.value = 3.84, sort = TRUE)
##       lhs op rhs    mi    epc sepc.lv sepc.all sepc.nox
## 88     y2 ~~  y6 9.279  2.129   2.129    0.401    0.401
## 104    y6 ~~  y8 8.668  1.513   1.513    0.423    0.423
## 81     y1 ~~  y5 8.183  0.884   0.884    0.410    0.410
## 93     y3 ~~  y6 6.574 -1.590  -1.590   -0.330   -0.330
## 79     y1 ~~  y3 5.204  1.024   1.024    0.318    0.318
## 86     y2 ~~  y4 4.911  1.432   1.432    0.331    0.331
## 94     y3 ~~  y7 4.088  1.152   1.152    0.266    0.266
## 33  ind60 =~  y5 4.007  0.762   0.510    0.197    0.197

修改后的模型

# y1、y2、y3、y4分别与y5、y6、y7、y8重合,因此反映了共同成分,需要增加残差相关

model_r <- '
# measurement model 
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + y2 + y3 + y4
dem65 =~ y5 + y6 + y7 + y8 
# regressions 
dem60 ~ ind60 
dem65 ~ ind60 + dem60 

#residuals
y1~~y5
y2~~y6
y3~~y7
y4~~y8
'
fit_r <- sem(model_r, data = PoliticalDemocracy)
summary(fit_r, fit.measures = TRUE, standardized = TRUE)
## lavaan 0.6.15 ended normally after 58 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        29
## 
##   Number of observations                            75
## 
## Model Test User Model:
##                                                       
##   Test statistic                                50.835
##   Degrees of freedom                                37
##   P-value (Chi-square)                           0.064
## 
## Model Test Baseline Model:
## 
##   Test statistic                               730.654
##   Degrees of freedom                                55
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.980
##   Tucker-Lewis Index (TLI)                       0.970
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1554.146
##   Loglikelihood unrestricted model (H1)      -1528.728
##                                                       
##   Akaike (AIC)                                3166.292
##   Bayesian (BIC)                              3233.499
##   Sample-size adjusted Bayesian (SABIC)       3142.099
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.071
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.115
##   P-value H_0: RMSEA <= 0.050                    0.234
##   P-value H_0: RMSEA >= 0.080                    0.396
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.050
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   ind60 =~                                                              
##     x1                1.000                               0.670    0.920
##     x2                2.181    0.139   15.720    0.000    1.460    0.973
##     x3                1.819    0.152   11.966    0.000    1.218    0.872
##   dem60 =~                                                              
##     y1                1.000                               2.145    0.824
##     y2                1.388    0.188    7.401    0.000    2.977    0.760
##     y3                1.053    0.161    6.552    0.000    2.259    0.694
##     y4                1.368    0.153    8.928    0.000    2.933    0.881
##   dem65 =~                                                              
##     y5                1.000                               2.014    0.777
##     y6                1.317    0.180    7.314    0.000    2.654    0.790
##     y7                1.326    0.174    7.618    0.000    2.672    0.817
##     y8                1.391    0.171    8.118    0.000    2.803    0.870
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   dem60 ~                                                               
##     ind60             1.435    0.385    3.728    0.000    0.448    0.448
##   dem65 ~                                                               
##     ind60             0.507    0.209    2.425    0.015    0.168    0.168
##     dem60             0.816    0.100    8.156    0.000    0.869    0.869
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .y1 ~~                                                                 
##    .y5                0.892    0.366    2.433    0.015    0.892    0.370
##  .y2 ~~                                                                 
##    .y6                1.893    0.762    2.486    0.013    1.893    0.361
##  .y3 ~~                                                                 
##    .y7                1.268    0.623    2.035    0.042    1.268    0.287
##  .y4 ~~                                                                 
##    .y8                0.141    0.464    0.303    0.762    0.141    0.056
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .x1                0.082    0.020    4.177    0.000    0.082    0.154
##    .x2                0.120    0.070    1.708    0.088    0.120    0.053
##    .x3                0.467    0.090    5.172    0.000    0.467    0.239
##    .y1                2.181    0.456    4.779    0.000    2.181    0.322
##    .y2                6.490    1.231    5.271    0.000    6.490    0.423
##    .y3                5.490    0.991    5.538    0.000    5.490    0.518
##    .y4                2.470    0.660    3.741    0.000    2.470    0.223
##    .y5                2.662    0.506    5.260    0.000    2.662    0.396
##    .y6                4.249    0.817    5.201    0.000    4.249    0.376
##    .y7                3.560    0.712    4.999    0.000    3.560    0.333
##    .y8                2.531    0.609    4.159    0.000    2.531    0.244
##     ind60             0.448    0.087    5.171    0.000    1.000    1.000
##    .dem60             3.678    0.885    4.154    0.000    0.799    0.799
##    .dem65             0.350    0.187    1.865    0.062    0.086    0.086

使用lavaanPlot绘制结果图(不推荐论文中使用,建议直接用ppt绘制)

model_r <- '
# measurement model 
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + y2 + y3 + y4
dem65 =~ y5 + y6 + y7 + y8 
# regressions 
dem60 ~ ind60 
dem65 ~ ind60 + dem60 

#residuals
y1~~y5
y2~~y6
y3~~y7
y4~~y8
'
fit_r <- sem(model_r, data = PoliticalDemocracy)
summary(fit_r, fit.measures = TRUE, standardized = TRUE)
## lavaan 0.6.15 ended normally after 58 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        29
## 
##   Number of observations                            75
## 
## Model Test User Model:
##                                                       
##   Test statistic                                50.835
##   Degrees of freedom                                37
##   P-value (Chi-square)                           0.064
## 
## Model Test Baseline Model:
## 
##   Test statistic                               730.654
##   Degrees of freedom                                55
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.980
##   Tucker-Lewis Index (TLI)                       0.970
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1554.146
##   Loglikelihood unrestricted model (H1)      -1528.728
##                                                       
##   Akaike (AIC)                                3166.292
##   Bayesian (BIC)                              3233.499
##   Sample-size adjusted Bayesian (SABIC)       3142.099
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.071
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.115
##   P-value H_0: RMSEA <= 0.050                    0.234
##   P-value H_0: RMSEA >= 0.080                    0.396
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.050
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   ind60 =~                                                              
##     x1                1.000                               0.670    0.920
##     x2                2.181    0.139   15.720    0.000    1.460    0.973
##     x3                1.819    0.152   11.966    0.000    1.218    0.872
##   dem60 =~                                                              
##     y1                1.000                               2.145    0.824
##     y2                1.388    0.188    7.401    0.000    2.977    0.760
##     y3                1.053    0.161    6.552    0.000    2.259    0.694
##     y4                1.368    0.153    8.928    0.000    2.933    0.881
##   dem65 =~                                                              
##     y5                1.000                               2.014    0.777
##     y6                1.317    0.180    7.314    0.000    2.654    0.790
##     y7                1.326    0.174    7.618    0.000    2.672    0.817
##     y8                1.391    0.171    8.118    0.000    2.803    0.870
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   dem60 ~                                                               
##     ind60             1.435    0.385    3.728    0.000    0.448    0.448
##   dem65 ~                                                               
##     ind60             0.507    0.209    2.425    0.015    0.168    0.168
##     dem60             0.816    0.100    8.156    0.000    0.869    0.869
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .y1 ~~                                                                 
##    .y5                0.892    0.366    2.433    0.015    0.892    0.370
##  .y2 ~~                                                                 
##    .y6                1.893    0.762    2.486    0.013    1.893    0.361
##  .y3 ~~                                                                 
##    .y7                1.268    0.623    2.035    0.042    1.268    0.287
##  .y4 ~~                                                                 
##    .y8                0.141    0.464    0.303    0.762    0.141    0.056
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .x1                0.082    0.020    4.177    0.000    0.082    0.154
##    .x2                0.120    0.070    1.708    0.088    0.120    0.053
##    .x3                0.467    0.090    5.172    0.000    0.467    0.239
##    .y1                2.181    0.456    4.779    0.000    2.181    0.322
##    .y2                6.490    1.231    5.271    0.000    6.490    0.423
##    .y3                5.490    0.991    5.538    0.000    5.490    0.518
##    .y4                2.470    0.660    3.741    0.000    2.470    0.223
##    .y5                2.662    0.506    5.260    0.000    2.662    0.396
##    .y6                4.249    0.817    5.201    0.000    4.249    0.376
##    .y7                3.560    0.712    4.999    0.000    3.560    0.333
##    .y8                2.531    0.609    4.159    0.000    2.531    0.244
##     ind60             0.448    0.087    5.171    0.000    1.000    1.000
##    .dem60             3.678    0.885    4.154    0.000    0.799    0.799
##    .dem65             0.350    0.187    1.865    0.062    0.086    0.086
library(lavaanPlot)
## Warning: 程辑包'lavaanPlot'是用R版本4.3.2 来建造的
lavaanPlot(model = fit_r,coefs = TRUE, stand = TRUE,stars = c('regress','latent','covs','residual'))

潜变量增长模型

# 线性

model <- ' i =~ 1*t1 + 1*t2 + 1*t3 + 1*t4
           s =~ 0*t1 + 1*t2 + 2*t3 + 3*t4 '
fit <- growth(model, data=Demo.growth)
summary(fit,fit.measures = TRUE, standardized = TRUE)
## lavaan 0.6.15 ended normally after 29 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         9
## 
##   Number of observations                           400
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 8.069
##   Degrees of freedom                                 5
##   P-value (Chi-square)                           0.152
## 
## Model Test Baseline Model:
## 
##   Test statistic                              1780.035
##   Degrees of freedom                                 6
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.998
##   Tucker-Lewis Index (TLI)                       0.998
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -2755.052
##   Loglikelihood unrestricted model (H1)      -2751.018
##                                                       
##   Akaike (AIC)                                5528.104
##   Bayesian (BIC)                              5564.027
##   Sample-size adjusted Bayesian (SABIC)       5535.470
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.039
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.087
##   P-value H_0: RMSEA <= 0.050                    0.580
##   P-value H_0: RMSEA >= 0.080                    0.085
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.012
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   i =~                                                                  
##     t1                1.000                               1.390    0.874
##     t2                1.000                               1.390    0.660
##     t3                1.000                               1.390    0.511
##     t4                1.000                               1.390    0.411
##   s =~                                                                  
##     t1                0.000                               0.000    0.000
##     t2                1.000                               0.766    0.364
##     t3                2.000                               1.532    0.564
##     t4                3.000                               2.298    0.680
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   i ~~                                                                  
##     s                 0.618    0.071    8.686    0.000    0.580    0.580
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .t1                0.000                               0.000    0.000
##    .t2                0.000                               0.000    0.000
##    .t3                0.000                               0.000    0.000
##    .t4                0.000                               0.000    0.000
##     i                 0.615    0.077    8.007    0.000    0.442    0.442
##     s                 1.006    0.042   24.076    0.000    1.314    1.314
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .t1                0.595    0.086    6.944    0.000    0.595    0.236
##    .t2                0.676    0.061   11.061    0.000    0.676    0.153
##    .t3                0.635    0.072    8.761    0.000    0.635    0.086
##    .t4                0.508    0.124    4.090    0.000    0.508    0.044
##     i                 1.932    0.173   11.194    0.000    1.000    1.000
##     s                 0.587    0.052   11.336    0.000    1.000    1.000
# 二次曲线

model <- ' i =~ 1*t1 + 1*t2 + 1*t3 + 1*t4
s =~ 0*t1 + 1*t2 + 2*t3 + 3*t4 
q =~ 0*t1 + 1*t2 + 4*t3 + 9*t4 '
fit <- growth(model, data=Demo.growth)
summary(fit)
## lavaan 0.6.15 ended normally after 79 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        13
## 
##   Number of observations                           400
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 2.717
##   Degrees of freedom                                 1
##   P-value (Chi-square)                           0.099
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   i =~                                                
##     t1                1.000                           
##     t2                1.000                           
##     t3                1.000                           
##     t4                1.000                           
##   s =~                                                
##     t1                0.000                           
##     t2                1.000                           
##     t3                2.000                           
##     t4                3.000                           
##   q =~                                                
##     t1                0.000                           
##     t2                1.000                           
##     t3                4.000                           
##     t4                9.000                           
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   i ~~                                                
##     s                 0.217    0.312    0.696    0.486
##     q                 0.095    0.075    1.265    0.206
##   s ~~                                                
##     q                -0.155    0.066   -2.341    0.019
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .t1                0.000                           
##    .t2                0.000                           
##    .t3                0.000                           
##    .t4                0.000                           
##     i                 0.600    0.079    7.606    0.000
##     s                 1.032    0.075   13.773    0.000
##     q                -0.008    0.020   -0.378    0.705
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .t1                0.226    0.273    0.827    0.408
##    .t2                0.687    0.127    5.407    0.000
##    .t3                0.560    0.159    3.516    0.000
##    .t4                0.495    0.583    0.849    0.396
##     i                 2.268    0.316    7.171    0.000
##     s                 1.201    0.313    3.837    0.000
##     q                 0.039    0.028    1.387    0.166

交叉滞后模型

dat <- read.table("D:/Desktop/RICLPM.dat", 
                  col.names = c(
                    "x1", "x2", "x3", "x4", "x5", 
                    "y1", "y2", "y3", "y4", "y5")
                  ) 

# 传统的交叉滞后模型
CLPM<-'
#回归路径
x2+y2~x1+y1
x3+y3~x2+y2
x4+y4~x3+y3
x5+y5~x4+y4

#相关
x1~~y1
x2~~y2
x3~~y3
x4~~y4
x5~~y5'

CLPM.fit <- sem(CLPM,
  data = dat)
summary(CLPM.fit, fit.measures = T,standardized = T)
## lavaan 0.6.15 ended normally after 46 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        31
## 
##   Number of observations                          1189
## 
## Model Test User Model:
##                                                       
##   Test statistic                               202.225
##   Degrees of freedom                                24
##   P-value (Chi-square)                           0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                              3166.400
##   Degrees of freedom                                45
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.943
##   Tucker-Lewis Index (TLI)                       0.893
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)                443.540
##   Loglikelihood unrestricted model (H1)        544.652
##                                                       
##   Akaike (AIC)                                -825.080
##   Bayesian (BIC)                              -667.573
##   Sample-size adjusted Bayesian (SABIC)       -766.041
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.079
##   90 Percent confidence interval - lower         0.069
##   90 Percent confidence interval - upper         0.089
##   P-value H_0: RMSEA <= 0.050                    0.000
##   P-value H_0: RMSEA >= 0.080                    0.451
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.074
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   x2 ~                                                                  
##     x1                0.321    0.023   13.743    0.000    0.321    0.392
##     y1                0.061    0.019    3.269    0.001    0.061    0.093
##   y2 ~                                                                  
##     x1                0.151    0.038    3.965    0.000    0.151    0.117
##     y1                0.325    0.030   10.684    0.000    0.325    0.314
##   x3 ~                                                                  
##     x2                0.388    0.028   13.991    0.000    0.388    0.389
##     y2                0.055    0.017    3.135    0.002    0.055    0.087
##   y3 ~                                                                  
##     x2                0.200    0.043    4.605    0.000    0.200    0.122
##     y2                0.466    0.027   17.015    0.000    0.466    0.451
##   x4 ~                                                                  
##     x3                0.414    0.029   14.368    0.000    0.414    0.403
##     y3                0.047    0.018    2.666    0.008    0.047    0.075
##   y4 ~                                                                  
##     x3                0.197    0.044    4.438    0.000    0.197    0.118
##     y3                0.481    0.027   17.746    0.000    0.481    0.470
##   x5 ~                                                                  
##     x4                0.423    0.028   15.120    0.000    0.423    0.425
##     y4                0.025    0.017    1.450    0.147    0.025    0.041
##   y5 ~                                                                  
##     x4                0.153    0.040    3.795    0.000    0.153    0.096
##     y4                0.537    0.025   21.691    0.000    0.537    0.549
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   x1 ~~                                                                 
##     y1                0.031    0.002   13.022    0.000    0.031    0.408
##  .x2 ~~                                                                 
##    .y2                0.013    0.002    8.128    0.000    0.013    0.243
##  .x3 ~~                                                                 
##    .y3                0.014    0.002    9.163    0.000    0.014    0.276
##  .x4 ~~                                                                 
##    .y4                0.016    0.002    9.802    0.000    0.016    0.296
##  .x5 ~~                                                                 
##    .y5                0.009    0.001    6.326    0.000    0.009    0.187
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .x2                0.033    0.001   24.382    0.000    0.033    0.808
##    .y2                0.089    0.004   24.382    0.000    0.089    0.858
##    .x3                0.034    0.001   24.382    0.000    0.034    0.819
##    .y3                0.082    0.003   24.382    0.000    0.082    0.745
##    .x4                0.035    0.001   24.382    0.000    0.035    0.810
##    .y4                0.083    0.003   24.382    0.000    0.083    0.725
##    .x5                0.034    0.001   24.382    0.000    0.034    0.804
##    .y5                0.072    0.003   24.382    0.000    0.072    0.650
##     x1                0.062    0.003   24.382    0.000    0.062    1.000
##     y1                0.097    0.004   24.382    0.000    0.097    1.000
# 随机截距的交叉滞后模型
RICLPM <- '
  # Create between components (random intercepts)
  RIx =~ 1*x1 + 1*x2 + 1*x3 + 1*x4 + 1*x5
  RIy =~ 1*y1 + 1*y2 + 1*y3 + 1*y4 + 1*y5
  
  # Create within-person centered variables
  wx1 =~ 1*x1
  wx2 =~ 1*x2
  wx3 =~ 1*x3 
  wx4 =~ 1*x4
  wx5 =~ 1*x5
  wy1 =~ 1*y1
  wy2 =~ 1*y2
  wy3 =~ 1*y3
  wy4 =~ 1*y4
  wy5 =~ 1*y5

  # Estimate lagged effects between within-person centered variables
  wx2 + wy2 ~ wx1 + wy1
  wx3 + wy3 ~ wx2 + wy2
  wx4 + wy4 ~ wx3 + wy3
  wx5 + wy5 ~ wx4 + wy4

  # Estimate covariance between within-person centered variables at first wave
  wx1 ~~ wy1 # Covariance
  
  # Estimate covariances between residuals of within-person centered variables 
  # (i.e., innovations)
  wx2 ~~ wy2
  wx3 ~~ wy3
  wx4 ~~ wy4
  wx5 ~~ wy5
  
  # Estimate variance and covariance of random intercepts
  RIx ~~ RIx
  RIy ~~ RIy
  RIx ~~ RIy

  # Estimate (residual) variance of within-person centered variables
  wx1 ~~ wx1 # Variances
  wy1 ~~ wy1 
  wx2 ~~ wx2 # Residual variances
  wy2 ~~ wy2 
  wx3 ~~ wx3 
  wy3 ~~ wy3 
  wx4 ~~ wx4 
  wy4 ~~ wy4 
  wx5 ~~ wx5
  wy5 ~~ wy5
'
RICLPM.fit <- lavaan(RICLPM,
  data = dat, 
  missing = 'ML', 
  meanstructure = T, 
  int.ov.free = T
)
summary(RICLPM.fit,fit.measures = T, standardized = T)
## lavaan 0.6.15 ended normally after 116 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        44
## 
##   Number of observations                          1189
##   Number of missing patterns                         1
## 
## Model Test User Model:
##                                                       
##   Test statistic                                25.806
##   Degrees of freedom                                21
##   P-value (Chi-square)                           0.214
## 
## Model Test Baseline Model:
## 
##   Test statistic                              3166.400
##   Degrees of freedom                                45
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.998
##   Tucker-Lewis Index (TLI)                       0.997
##                                                       
##   Robust Comparative Fit Index (CFI)             0.998
##   Robust Tucker-Lewis Index (TLI)                0.997
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)                531.750
##   Loglikelihood unrestricted model (H1)        544.652
##                                                       
##   Akaike (AIC)                                -975.499
##   Bayesian (BIC)                              -751.941
##   Sample-size adjusted Bayesian (SABIC)       -891.701
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.014
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.030
##   P-value H_0: RMSEA <= 0.050                    1.000
##   P-value H_0: RMSEA >= 0.080                    0.000
##                                                       
##   Robust RMSEA                                   0.014
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.030
##   P-value H_0: Robust RMSEA <= 0.050             1.000
##   P-value H_0: Robust RMSEA >= 0.080             0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.014
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   RIx =~                                                                
##     x1                1.000                               0.096    0.390
##     x2                1.000                               0.096    0.473
##     x3                1.000                               0.096    0.475
##     x4                1.000                               0.096    0.461
##     x5                1.000                               0.096    0.465
##   RIy =~                                                                
##     y1                1.000                               0.178    0.569
##     y2                1.000                               0.178    0.558
##     y3                1.000                               0.178    0.535
##     y4                1.000                               0.178    0.525
##     y5                1.000                               0.178    0.533
##   wx1 =~                                                                
##     x1                1.000                               0.227    0.921
##   wx2 =~                                                                
##     x2                1.000                               0.179    0.881
##   wx3 =~                                                                
##     x3                1.000                               0.178    0.880
##   wx4 =~                                                                
##     x4                1.000                               0.185    0.887
##   wx5 =~                                                                
##     x5                1.000                               0.183    0.885
##   wy1 =~                                                                
##     y1                1.000                               0.257    0.822
##   wy2 =~                                                                
##     y2                1.000                               0.265    0.830
##   wy3 =~                                                                
##     y3                1.000                               0.281    0.845
##   wy4 =~                                                                
##     y4                1.000                               0.288    0.851
##   wy5 =~                                                                
##     y5                1.000                               0.282    0.846
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   wx2 ~                                                                 
##     wx1               0.232    0.028    8.314    0.000    0.294    0.294
##     wy1               0.009    0.026    0.329    0.742    0.012    0.012
##   wy2 ~                                                                 
##     wx1               0.174    0.045    3.888    0.000    0.149    0.149
##     wy1               0.004    0.046    0.092    0.927    0.004    0.004
##   wx3 ~                                                                 
##     wx2               0.241    0.037    6.509    0.000    0.242    0.242
##     wy2               0.026    0.024    1.082    0.279    0.039    0.039
##   wy3 ~                                                                 
##     wx2               0.156    0.054    2.871    0.004    0.099    0.099
##     wy2               0.262    0.039    6.747    0.000    0.247    0.247
##   wx4 ~                                                                 
##     wx3               0.279    0.038    7.267    0.000    0.269    0.269
##     wy3               0.010    0.023    0.431    0.666    0.015    0.015
##   wy4 ~                                                                 
##     wx3               0.185    0.055    3.367    0.001    0.114    0.114
##     wy3               0.296    0.035    8.362    0.000    0.288    0.288
##   wx5 ~                                                                 
##     wx4               0.290    0.035    8.244    0.000    0.293    0.293
##     wy4              -0.004    0.022   -0.186    0.852   -0.006   -0.006
##   wy5 ~                                                                 
##     wx4               0.124    0.048    2.612    0.009    0.082    0.082
##     wy4               0.392    0.031   12.644    0.000    0.400    0.400
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   wx1 ~~                                                                
##     wy1               0.021    0.002    9.372    0.000    0.364    0.364
##  .wx2 ~~                                                                
##    .wy2               0.009    0.002    5.168    0.000    0.196    0.196
##  .wx3 ~~                                                                
##    .wy3               0.013    0.002    7.837    0.000    0.274    0.274
##  .wx4 ~~                                                                
##    .wy4               0.013    0.002    8.177    0.000    0.277    0.277
##  .wx5 ~~                                                                
##    .wy5               0.007    0.001    4.916    0.000    0.160    0.160
##   RIx ~~                                                                
##     RIy               0.010    0.001    7.992    0.000    0.587    0.587
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .x1                0.241    0.007   33.687    0.000    0.241    0.977
##    .x2                0.173    0.006   29.331    0.000    0.173    0.851
##    .x3                0.186    0.006   31.646    0.000    0.186    0.918
##    .x4                0.117    0.006   19.288    0.000    0.117    0.559
##    .x5                0.111    0.006   18.427    0.000    0.111    0.534
##    .y1                0.336    0.009   37.099    0.000    0.336    1.076
##    .y2                0.348    0.009   37.686    0.000    0.348    1.093
##    .y3                0.319    0.010   33.098    0.000    0.319    0.960
##    .y4                0.384    0.010   39.097    0.000    0.384    1.134
##    .y5                0.388    0.010   40.056    0.000    0.388    1.162
##     RIx               0.000                               0.000    0.000
##     RIy               0.000                               0.000    0.000
##     wx1               0.000                               0.000    0.000
##    .wx2               0.000                               0.000    0.000
##    .wx3               0.000                               0.000    0.000
##    .wx4               0.000                               0.000    0.000
##    .wx5               0.000                               0.000    0.000
##     wy1               0.000                               0.000    0.000
##    .wy2               0.000                               0.000    0.000
##    .wy3               0.000                               0.000    0.000
##    .wy4               0.000                               0.000    0.000
##    .wy5               0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     RIx               0.009    0.001    8.722    0.000    1.000    1.000
##     RIy               0.032    0.003   12.351    0.000    1.000    1.000
##     wx1               0.052    0.002   21.067    0.000    1.000    1.000
##     wy1               0.066    0.004   17.985    0.000    1.000    1.000
##    .wx2               0.029    0.001   20.793    0.000    0.911    0.911
##    .wy2               0.068    0.004   17.503    0.000    0.977    0.977
##    .wx3               0.030    0.001   20.467    0.000    0.935    0.935
##    .wy3               0.072    0.003   21.324    0.000    0.918    0.918
##    .wx4               0.032    0.001   21.445    0.000    0.925    0.925
##    .wy4               0.074    0.003   21.876    0.000    0.884    0.884
##    .wx5               0.031    0.001   21.680    0.000    0.915    0.915
##    .wy5               0.065    0.003   22.446    0.000    0.813    0.813
##    .x1                0.000                               0.000    0.000
##    .x2                0.000                               0.000    0.000
##    .x3                0.000                               0.000    0.000
##    .x4                0.000                               0.000    0.000
##    .x5                0.000                               0.000    0.000
##    .y1                0.000                               0.000    0.000
##    .y2                0.000                               0.000    0.000
##    .y3                0.000                               0.000    0.000
##    .y4                0.000                               0.000    0.000
##    .y5                0.000                               0.000    0.000