写在前面:下面的某些模型报错,没有实际意义,只是为了用一个数据集来示例

df <- HolzingerSwineford1939 
df

先看结构部分,相当于线性回归,

lm(x2 ~ x3 + x8,data = df)
#> 
#> Call:
#> lm(formula = x2 ~ x3 + x8, data = df)
#> 
#> Coefficients:
#> (Intercept)           x3           x8  
#>     5.11146      0.34798      0.03501


regression <- 'x2 ~ x3 + x8'
fit <- sem(regression,data=df)#拟合函数,第一个参数是模型,第二个参数是数据集
summary(fit)# 再通过summary函数给出结果
#> lavaan 0.6-5 ended normally after 11 iterations
#> 
#>   Estimator                                         ML
#>   Optimization method                           NLMINB
#>   Number of free parameters                          3
#>                                                       
#>   Number of observations                           301
#>                                                       
#> Model Test User Model:
#>                                                       
#>   Test statistic                                 0.000
#>   Degrees of freedom                                 0
#> 
#> Parameter Estimates:
#> 
#>   Information                                 Expected
#>   Information saturated (h1) model          Structured
#>   Standard errors                             Standard
#> 
#> Regressions:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>   x2 ~                                                
#>     x3                0.348    0.057    6.061    0.000
#>     x8                0.035    0.064    0.546    0.585
#> 
#> Variances:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>    .x2                1.221    0.100   12.268    0.000
 semPaths(fit,whatLabels="std.all",style="lisrel",
          residuals=TRUE)

# 接着看因子分析 - 视觉因子x1,x2,x3 - 文本因子x4,x5,x6 - 速度因子x7,x8,x9

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

fit <- cfa(latent_variables, data = df)
summary(fit)
#> lavaan 0.6-5 ended normally after 35 iterations
#> 
#>   Estimator                                         ML
#>   Optimization method                           NLMINB
#>   Number of free parameters                         21
#>                                                       
#>   Number of observations                           301
#>                                                       
#> Model Test User Model:
#>                                                       
#>   Test statistic                                85.306
#>   Degrees of freedom                                24
#>   P-value (Chi-square)                           0.000
#> 
#> Parameter Estimates:
#> 
#>   Information                                 Expected
#>   Information saturated (h1) model          Structured
#>   Standard errors                             Standard
#> 
#> Latent Variables:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>   visual =~                                           
#>     x1                1.000                           
#>     x2                0.554    0.100    5.554    0.000
#>     x3                0.729    0.109    6.685    0.000
#>   textual =~                                          
#>     x4                1.000                           
#>     x5                1.113    0.065   17.014    0.000
#>     x6                0.926    0.055   16.703    0.000
#>   speed =~                                            
#>     x7                1.000                           
#>     x8                1.180    0.165    7.152    0.000
#>     x9                1.082    0.151    7.155    0.000
#> 
#> Covariances:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>   visual ~~                                           
#>     textual           0.408    0.074    5.552    0.000
#>     speed             0.262    0.056    4.660    0.000
#>   textual ~~                                          
#>     speed             0.173    0.049    3.518    0.000
#> 
#> Variances:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>    .x1                0.549    0.114    4.833    0.000
#>    .x2                1.134    0.102   11.146    0.000
#>    .x3                0.844    0.091    9.317    0.000
#>    .x4                0.371    0.048    7.779    0.000
#>    .x5                0.446    0.058    7.642    0.000
#>    .x6                0.356    0.043    8.277    0.000
#>    .x7                0.799    0.081    9.823    0.000
#>    .x8                0.488    0.074    6.573    0.000
#>    .x9                0.566    0.071    8.003    0.000
#>     visual            0.809    0.145    5.564    0.000
#>     textual           0.979    0.112    8.737    0.000
#>     speed             0.384    0.086    4.451    0.000
semPaths(fit,whatLabels="std.all",style="lisrel",
          residuals=TRUE)

# 然后咱们把他合起来成为结构方程模型 - summary():结果概览 - parameterEstimates(fit):参数估计 - standardizedSolution():标准化参数估计 - fitted():拟合协方差矩阵和均值向量 - resid():拟合模型残差,[观测协方差矩阵/均值向量]和[隐含协方差矩阵/均值向量]的差 - fitMeasures():返回所有拟合度量指标 - fitMeasures(fit, “cfi”) : 提取某个具体指标,例如cfi - inspect():查看拟合的lavaan对象具体信息

model <- '# measurement model 测量模型
          visual  =~ x1 + x2 + x3
          textual =~ x4 + x5 + x6
          speed   =~ x7 + x8 + x9
          
          # regressions 回归
          visual ~ textual
          speed ~ textual + visual

          # residual correlations 残余相关
          x1 ~~ x2
          x1 ~~ x3
         '

fit <- sem(model, data = df)
summary(fit, standardized = TRUE) 
#> lavaan 0.6-5 ended normally after 46 iterations
#> 
#>   Estimator                                         ML
#>   Optimization method                           NLMINB
#>   Number of free parameters                         23
#>                                                       
#>   Number of observations                           301
#>                                                       
#> Model Test User Model:
#>                                                       
#>   Test statistic                                76.817
#>   Degrees of freedom                                22
#>   P-value (Chi-square)                           0.000
#> 
#> Parameter Estimates:
#> 
#>   Information                                 Expected
#>   Information saturated (h1) model          Structured
#>   Standard errors                             Standard
#> 
#> Latent Variables:
#>                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
#>   visual =~                                                             
#>     x1                1.000                               1.356    1.163
#>     x2                0.436    0.128    3.404    0.001    0.592    0.503
#>     x3                0.563    0.116    4.849    0.000    0.763    0.675
#>   textual =~                                                            
#>     x4                1.000                               0.991    0.852
#>     x5                1.112    0.065   17.052    0.000    1.102    0.855
#>     x6                0.924    0.055   16.713    0.000    0.915    0.837
#>   speed =~                                                              
#>     x7                1.000                               0.628    0.577
#>     x8                1.179    0.164    7.180    0.000    0.740    0.732
#>     x9                1.047    0.146    7.191    0.000    0.658    0.653
#> 
#> Regressions:
#>                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
#>   visual ~                                                              
#>     textual           0.463    0.069    6.744    0.000    0.338    0.338
#>   speed ~                                                               
#>     textual           0.123    0.052    2.363    0.018    0.195    0.195
#>     visual            0.116    0.056    2.086    0.037    0.250    0.250
#> 
#> Covariances:
#>                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
#>  .x1 ~~                                                                 
#>    .x2               -0.394    0.188   -2.097    0.036   -0.394   -0.561
#>    .x3               -0.454    0.309   -1.469    0.142   -0.454   -0.787
#> 
#> Variances:
#>                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
#>    .x1               -0.479    0.704   -0.680    0.496   -0.479   -0.353
#>    .x2                1.032    0.137    7.538    0.000    1.032    0.747
#>    .x3                0.693    0.188    3.685    0.000    0.693    0.544
#>    .x4                0.369    0.048    7.761    0.000    0.369    0.273
#>    .x5                0.446    0.058    7.654    0.000    0.446    0.269
#>    .x6                0.358    0.043    8.326    0.000    0.358    0.299
#>    .x7                0.789    0.081    9.699    0.000    0.789    0.667
#>    .x8                0.474    0.075    6.305    0.000    0.474    0.464
#>    .x9                0.583    0.071    8.263    0.000    0.583    0.574
#>    .visual            1.627    0.706    2.303    0.021    0.886    0.886
#>     textual           0.981    0.112    8.753    0.000    1.000    1.000
#>    .speed             0.342    0.077    4.448    0.000    0.867    0.867
semPaths(fit,whatLabels="std.all",style="lisrel",
          residuals=TRUE)

# 基本上做上面的就差不多了,下面是做一些设置啥的 ## 固定参数

#一般情况
model <- '# measurement model 测量模型
          visual  =~ x1 + x2 + x3
          textual =~ x4 + x5 + x6
          
          # regressions 回归
          visual ~ textual
         '
fit <- sem(model, data = df)
parameterEstimates(fit)
semPaths(fit,whatLabels="std.all",style="lisrel",
          residuals=TRUE)

#使第一个因子载荷自由且未知
model <- '# measurement model 测量模型
          visual  =~ NA*x1 + x2 + x3
          textual =~ NA*x4 + x5 + x6
           speed  =~ NA*x7 + x8 + x9
          # regressions 回归
          visual ~ textual
           speed ~ textual + visual
         '
fit <- sem(model, data = df)
parameterEstimates(fit)
semPaths(fit,whatLabels="std.all",style="lisrel",
          residuals=TRUE)

#有理由让所有的因子载荷都为1
model <- '# measurement model 测量模型
          visual  =~ x1 + 1*x2 + 1*x3
          textual =~ x4 + x5 + x6
          # regressions 回归
          visual ~ textual
         '
fit <- sem(model, data = df)
parameterEstimates(fit)
semPaths(fit,whatLabels="std.all",style="lisrel",
          residuals=TRUE)

#想给参数命个名
model <- '# measurement model 测量模型
          visual  =~ a*x1 + b*x2 + c*x3
          textual =~ x4 + x5 + x6
          # regressions 回归
          visual ~ textual
         '
fit <- sem(model, data = df)
parameterEstimates(fit)
semPaths(fit,whatLabels="std.all",style="lisrel",
          residuals=TRUE)

#参数是自动生成初值,可以start()设定初值,但是结果不一定是这个哈
model <- '# measurement model 测量模型
          visual  =~ x1 + start(0.8)*x2 + start(1.2)*x3
          textual =~ x4 + start(0.5)*x5 + start(1.0)*x6
          # regressions 回归
          visual ~ textual
         '
fit <- sem(model, data = df)
parameterEstimates(fit)
semPaths(fit,whatLabels="std.all",style="lisrel",
          residuals=TRUE)

#固定他的系数
model <- '# measurement model 测量模型
          visual  =~ x1 + 0.8*x2 + 1.2*x3
          textual =~ x4 + 0.5*x5 + 1.0*x6
          # regressions 回归
          visual ~ textual
         '
fit <- sem(model, data = df)
parameterEstimates(fit)
semPaths(fit,whatLabels="est",style="lisrel",
          residuals=TRUE)

model <- '# measurement model 测量模型
          visual  =~ x1 + b* x2 + b*x3
          textual =~ x4 + x5 + x6
          
          # regressions 回归
          visual ~ textual
         '
fit <- sem(model, data = df)
parameterEstimates(fit)

#对系数进行约束
model <- '# measurement model 测量模型
          visual  =~ a*x1 + b* x2 + c*x3
          textual =~ x4 + x5 + x6
          
          # regressions 回归
          visual ~ textual
          a == b + c
          
         '
fit <- sem(model, data = df)
parameterEstimates(fit)

默认潜变量之间是相关的,可以将其设置为0

#他们之间还是相关的
model <- '# measurement model 测量模型
          visual  =~ x1 + x2 + x3
          textual =~ x4 + x5 + x6
          
          # regressions 回归
          visual ~ textual
         '
fit <- sem(model, data = df)
parameterEstimates(fit)
semPaths(fit,whatLabels="std.all",style="lisrel",
          residuals=TRUE)
#他们之间不相关了,看结果图里面都是两者之间连线都是0
model <- '# measurement model 测量模型
          visual  =~ x1 + x2 + x3
          textual =~ x4 + x5 + x6
          
          # regressions 回归
          visual ~ 0*textual
         '
fit <- sem(model, data = df)
parameterEstimates(fit)
semPaths(fit,whatLabels="std.all",style="lisrel",
          residuals=TRUE)

## 增加截距

#默认截距为0,,均值结构模型中引入9个观测变量的均值,也同时加上了9个额外的结截距参数
model <- '
# three-factor model
  visual  =~ x1 + x2 + x3
  textual =~ x4 + x5 + x6
  speed   =~ x7 + x8 + x9
# intercepts
  x1 ~ 1
  x2 ~ 1 
  x3 ~ 1#可以写完,也可以只写一个,截距就会都给出来
         '
fit <- sem(model, data = df)
summary(fit)
#> lavaan 0.6-5 ended normally after 35 iterations
#> 
#>   Estimator                                         ML
#>   Optimization method                           NLMINB
#>   Number of free parameters                         30
#>                                                       
#>   Number of observations                           301
#>                                                       
#> Model Test User Model:
#>                                                       
#>   Test statistic                                85.306
#>   Degrees of freedom                                24
#>   P-value (Chi-square)                           0.000
#> 
#> Parameter Estimates:
#> 
#>   Information                                 Expected
#>   Information saturated (h1) model          Structured
#>   Standard errors                             Standard
#> 
#> Latent Variables:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>   visual =~                                           
#>     x1                1.000                           
#>     x2                0.554    0.100    5.554    0.000
#>     x3                0.729    0.109    6.685    0.000
#>   textual =~                                          
#>     x4                1.000                           
#>     x5                1.113    0.065   17.014    0.000
#>     x6                0.926    0.055   16.703    0.000
#>   speed =~                                            
#>     x7                1.000                           
#>     x8                1.180    0.165    7.152    0.000
#>     x9                1.082    0.151    7.155    0.000
#> 
#> Covariances:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>   visual ~~                                           
#>     textual           0.408    0.074    5.552    0.000
#>     speed             0.262    0.056    4.660    0.000
#>   textual ~~                                          
#>     speed             0.173    0.049    3.518    0.000
#> 
#> Intercepts:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>    .x1                4.936    0.067   73.473    0.000
#>    .x2                6.088    0.068   89.855    0.000
#>    .x3                2.250    0.065   34.579    0.000
#>    .x4                3.061    0.067   45.694    0.000
#>    .x5                4.341    0.074   58.452    0.000
#>    .x6                2.186    0.063   34.667    0.000
#>    .x7                4.186    0.063   66.766    0.000
#>    .x8                5.527    0.058   94.854    0.000
#>    .x9                5.374    0.058   92.546    0.000
#>     visual            0.000                           
#>     textual           0.000                           
#>     speed             0.000                           
#> 
#> Variances:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>    .x1                0.549    0.114    4.833    0.000
#>    .x2                1.134    0.102   11.146    0.000
#>    .x3                0.844    0.091    9.317    0.000
#>    .x4                0.371    0.048    7.779    0.000
#>    .x5                0.446    0.058    7.642    0.000
#>    .x6                0.356    0.043    8.277    0.000
#>    .x7                0.799    0.081    9.823    0.000
#>    .x8                0.488    0.074    6.573    0.000
#>    .x9                0.566    0.071    8.003    0.000
#>     visual            0.809    0.145    5.564    0.000
#>     textual           0.979    0.112    8.737    0.000
#>     speed             0.384    0.086    4.451    0.000
semPaths(fit,whatLabels="std.all",style="lisrel",
          residuals=TRUE)

#作用是:某些关系要求截距为某个值时
model <- '
# three-factor model
  visual  =~ x1 + x2 + x3
  textual =~ x4 + x5 + x6
  speed   =~ x7 + x8 + x9
# intercepts
  x1 ~ 0.5 * 1 #记得要*1
  x2 ~ 0.3 * 1

         '
fit <- sem(model, data = df)
summary(fit)
#> lavaan 0.6-5 ended normally after 105 iterations
#> 
#>   Estimator                                         ML
#>   Optimization method                           NLMINB
#>   Number of free parameters                         28
#>                                                       
#>   Number of observations                           301
#>                                                       
#> Model Test User Model:
#>                                                       
#>   Test statistic                              1118.245
#>   Degrees of freedom                                26
#>   P-value (Chi-square)                           0.000
#> 
#> Parameter Estimates:
#> 
#>   Information                                 Expected
#>   Information saturated (h1) model          Structured
#>   Standard errors                             Standard
#> 
#> Latent Variables:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>   visual =~                                           
#>     x1                1.000                           
#>     x2                1.270    0.021   60.651    0.000
#>     x3                0.590    0.013   44.736    0.000
#>   textual =~                                          
#>     x4                1.000                           
#>     x5                1.113    0.028   40.396    0.000
#>     x6                0.926    0.024   39.063    0.000
#>   speed =~                                            
#>     x7                1.000                           
#>     x8                1.183    0.061   19.338    0.000
#>     x9                1.066    0.058   18.238    0.000
#> 
#> Covariances:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>   visual ~~                                           
#>     textual           8.426    0.745   11.310    0.000
#>     speed             5.144    0.500   10.293    0.000
#>   textual ~~                                          
#>     speed             2.163    0.221    9.776    0.000
#> 
#> Intercepts:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>    .x1                0.500                           
#>    .x2                0.300                           
#>    .x3               -0.344    0.057   -6.028    0.000
#>    .x4                1.256    0.063   19.808    0.000
#>    .x5                2.331    0.070   33.184    0.000
#>    .x6                0.514    0.060    8.594    0.000
#>    .x7                3.084    0.061   50.320    0.000
#>    .x8                4.223    0.056   75.237    0.000
#>    .x9                4.199    0.056   74.541    0.000
#>     visual            0.000                           
#>     textual           0.000                           
#>     speed             0.000                           
#> 
#> Variances:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>    .x1                0.491    0.102    4.830    0.000
#>    .x2                1.746    0.211    8.290    0.000
#>    .x3                0.868    0.080   10.786    0.000
#>    .x4                0.371    0.045    8.289    0.000
#>    .x5                0.446    0.055    8.161    0.000
#>    .x6                0.356    0.041    8.725    0.000
#>    .x7                0.796    0.076   10.416    0.000
#>    .x8                0.479    0.061    7.843    0.000
#>    .x9                0.574    0.062    9.292    0.000
#>     visual           20.543    1.717   11.967    0.000
#>     textual           4.238    0.376   11.273    0.000
#>     speed             1.602    0.189    8.488    0.000
semPaths(fit,what = "est",style="lisrel",
          residuals=TRUE,layout = "tree2")#设置what值将线的颜色、粗细、透明度根据参数估计值的大小和显著性做出改变

## 多组分析


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

fit <- cfa(model, data = HolzingerSwineford1939, group = "school")#group那分组,默认所有组会拟合相同的模型
summary(fit)
#> lavaan 0.6-5 ended normally after 57 iterations
#> 
#>   Estimator                                         ML
#>   Optimization method                           NLMINB
#>   Number of free parameters                         60
#>                                                       
#>   Number of observations per group:                   
#>     Pasteur                                        156
#>     Grant-White                                    145
#>                                                       
#> Model Test User Model:
#>                                                       
#>   Test statistic                               115.851
#>   Degrees of freedom                                48
#>   P-value (Chi-square)                           0.000
#>   Test statistic for each group:
#>     Pasteur                                     64.309
#>     Grant-White                                 51.542
#> 
#> Parameter Estimates:
#> 
#>   Information                                 Expected
#>   Information saturated (h1) model          Structured
#>   Standard errors                             Standard
#> 
#> 
#> Group 1 [Pasteur]:
#> 
#> Latent Variables:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>   visual =~                                           
#>     x1                1.000                           
#>     x2                0.394    0.122    3.220    0.001
#>     x3                0.570    0.140    4.076    0.000
#>   textual =~                                          
#>     x4                1.000                           
#>     x5                1.183    0.102   11.613    0.000
#>     x6                0.875    0.077   11.421    0.000
#>   speed =~                                            
#>     x7                1.000                           
#>     x8                1.125    0.277    4.057    0.000
#>     x9                0.922    0.225    4.104    0.000
#> 
#> Covariances:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>   visual ~~                                           
#>     textual           0.479    0.106    4.531    0.000
#>     speed             0.185    0.077    2.397    0.017
#>   textual ~~                                          
#>     speed             0.182    0.069    2.628    0.009
#> 
#> Intercepts:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>    .x1                4.941    0.095   52.249    0.000
#>    .x2                5.984    0.098   60.949    0.000
#>    .x3                2.487    0.093   26.778    0.000
#>    .x4                2.823    0.092   30.689    0.000
#>    .x5                3.995    0.105   38.183    0.000
#>    .x6                1.922    0.079   24.321    0.000
#>    .x7                4.432    0.087   51.181    0.000
#>    .x8                5.563    0.078   71.214    0.000
#>    .x9                5.418    0.079   68.440    0.000
#>     visual            0.000                           
#>     textual           0.000                           
#>     speed             0.000                           
#> 
#> Variances:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>    .x1                0.298    0.232    1.286    0.198
#>    .x2                1.334    0.158    8.464    0.000
#>    .x3                0.989    0.136    7.271    0.000
#>    .x4                0.425    0.069    6.138    0.000
#>    .x5                0.456    0.086    5.292    0.000
#>    .x6                0.290    0.050    5.780    0.000
#>    .x7                0.820    0.125    6.580    0.000
#>    .x8                0.510    0.116    4.406    0.000
#>    .x9                0.680    0.104    6.516    0.000
#>     visual            1.097    0.276    3.967    0.000
#>     textual           0.894    0.150    5.963    0.000
#>     speed             0.350    0.126    2.778    0.005
#> 
#> 
#> Group 2 [Grant-White]:
#> 
#> Latent Variables:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>   visual =~                                           
#>     x1                1.000                           
#>     x2                0.736    0.155    4.760    0.000
#>     x3                0.925    0.166    5.583    0.000
#>   textual =~                                          
#>     x4                1.000                           
#>     x5                0.990    0.087   11.418    0.000
#>     x6                0.963    0.085   11.377    0.000
#>   speed =~                                            
#>     x7                1.000                           
#>     x8                1.226    0.187    6.569    0.000
#>     x9                1.058    0.165    6.429    0.000
#> 
#> Covariances:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>   visual ~~                                           
#>     textual           0.408    0.098    4.153    0.000
#>     speed             0.276    0.076    3.639    0.000
#>   textual ~~                                          
#>     speed             0.222    0.073    3.022    0.003
#> 
#> Intercepts:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>    .x1                4.930    0.095   51.696    0.000
#>    .x2                6.200    0.092   67.416    0.000
#>    .x3                1.996    0.086   23.195    0.000
#>    .x4                3.317    0.093   35.625    0.000
#>    .x5                4.712    0.096   48.986    0.000
#>    .x6                2.469    0.094   26.277    0.000
#>    .x7                3.921    0.086   45.819    0.000
#>    .x8                5.488    0.087   63.174    0.000
#>    .x9                5.327    0.085   62.571    0.000
#>     visual            0.000                           
#>     textual           0.000                           
#>     speed             0.000                           
#> 
#> Variances:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>    .x1                0.715    0.126    5.676    0.000
#>    .x2                0.899    0.123    7.339    0.000
#>    .x3                0.557    0.103    5.409    0.000
#>    .x4                0.315    0.065    4.870    0.000
#>    .x5                0.419    0.072    5.812    0.000
#>    .x6                0.406    0.069    5.880    0.000
#>    .x7                0.600    0.091    6.584    0.000
#>    .x8                0.401    0.094    4.249    0.000
#>    .x9                0.535    0.089    6.010    0.000
#>     visual            0.604    0.160    3.762    0.000
#>     textual           0.942    0.152    6.177    0.000
#>     speed             0.461    0.118    3.910    0.000


#还是可以固定参数(如果只给了一个值的话,那分组的模型都是这个值)
model_2 <- 'visual  =~ x1 + 0.5*x2 + c(0.6, 0.8)*x3
            textual =~ x4 + start(c(1.2, 0.6))*x5 + a*x6
            speed   =~ x7 + x8 + x9'
fit2 <- cfa(model_2, data = HolzingerSwineford1939, group = "school")
summary(fit2)
#> lavaan 0.6-5 ended normally after 45 iterations
#> 
#>   Estimator                                         ML
#>   Optimization method                           NLMINB
#>   Number of free parameters                         56
#>                                                       
#>   Number of observations per group:                   
#>     Pasteur                                        156
#>     Grant-White                                    145
#>                                                       
#> Model Test User Model:
#>                                                       
#>   Test statistic                               118.976
#>   Degrees of freedom                                52
#>   P-value (Chi-square)                           0.000
#>   Test statistic for each group:
#>     Pasteur                                     64.901
#>     Grant-White                                 54.075
#> 
#> Parameter Estimates:
#> 
#>   Information                                 Expected
#>   Information saturated (h1) model          Structured
#>   Standard errors                             Standard
#> 
#> 
#> Group 1 [Pasteur]:
#> 
#> Latent Variables:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>   visual =~                                           
#>     x1                1.000                           
#>     x2                0.500                           
#>     x3                0.600                           
#>   textual =~                                          
#>     x4                1.000                           
#>     x5                1.185    0.102   11.598    0.000
#>     x6         (a)    0.876    0.077   11.409    0.000
#>   speed =~                                            
#>     x7                1.000                           
#>     x8                1.129    0.279    4.055    0.000
#>     x9                0.931    0.227    4.103    0.000
#> 
#> Covariances:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>   visual ~~                                           
#>     textual           0.460    0.103    4.479    0.000
#>     speed             0.182    0.076    2.408    0.016
#>   textual ~~                                          
#>     speed             0.181    0.069    2.625    0.009
#> 
#> Intercepts:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>    .x1                4.941    0.094   52.379    0.000
#>    .x2                5.984    0.100   59.945    0.000
#>    .x3                2.487    0.092   26.983    0.000
#>    .x4                2.823    0.092   30.689    0.000
#>    .x5                3.995    0.105   38.183    0.000
#>    .x6                1.922    0.079   24.320    0.000
#>    .x7                4.432    0.087   51.181    0.000
#>    .x8                5.563    0.078   71.214    0.000
#>    .x9                5.418    0.079   68.440    0.000
#>     visual            0.000                           
#>     textual           0.000                           
#>     speed             0.000                           
#> 
#> Variances:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>    .x1                0.388    0.129    3.005    0.003
#>    .x2                1.304    0.155    8.432    0.000
#>    .x3                0.965    0.120    8.016    0.000
#>    .x4                0.427    0.069    6.153    0.000
#>    .x5                0.454    0.086    5.270    0.000
#>    .x6                0.289    0.050    5.763    0.000
#>    .x7                0.824    0.124    6.617    0.000
#>    .x8                0.510    0.116    4.417    0.000
#>    .x9                0.677    0.105    6.479    0.000
#>     visual            1.001    0.172    5.803    0.000
#>     textual           0.892    0.150    5.953    0.000
#>     speed             0.346    0.125    2.768    0.006
#> 
#> 
#> Group 2 [Grant-White]:
#> 
#> Latent Variables:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>   visual =~                                           
#>     x1                1.000                           
#>     x2                0.500                           
#>     x3                0.800                           
#>   textual =~                                          
#>     x4                1.000                           
#>     x5                0.990    0.087   11.425    0.000
#>     x6                0.963    0.085   11.374    0.000
#>   speed =~                                            
#>     x7                1.000                           
#>     x8                1.228    0.188    6.539    0.000
#>     x9                1.081    0.168    6.417    0.000
#> 
#> Covariances:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>   visual ~~                                           
#>     textual           0.454    0.099    4.585    0.000
#>     speed             0.315    0.079    4.004    0.000
#>   textual ~~                                          
#>     speed             0.222    0.073    3.049    0.002
#> 
#> Intercepts:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>    .x1                4.930    0.097   50.688    0.000
#>    .x2                6.200    0.089   69.616    0.000
#>    .x3                1.996    0.086   23.223    0.000
#>    .x4                3.317    0.093   35.625    0.000
#>    .x5                4.712    0.096   48.986    0.000
#>    .x6                2.469    0.094   26.277    0.000
#>    .x7                3.921    0.086   45.819    0.000
#>    .x8                5.488    0.087   63.174    0.000
#>    .x9                5.327    0.085   62.571    0.000
#>     visual            0.000                           
#>     textual           0.000                           
#>     speed             0.000                           
#> 
#> Variances:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>    .x1                0.637    0.115    5.539    0.000
#>    .x2                0.966    0.120    8.076    0.000
#>    .x3                0.601    0.091    6.591    0.000
#>    .x4                0.316    0.065    4.877    0.000
#>    .x5                0.418    0.072    5.805    0.000
#>    .x6                0.407    0.069    5.887    0.000
#>    .x7                0.609    0.091    6.658    0.000
#>    .x8                0.411    0.094    4.385    0.000
#>    .x9                0.522    0.089    5.887    0.000
#>     visual            0.735    0.132    5.544    0.000
#>     textual           0.942    0.152    6.177    0.000
#>     speed             0.453    0.117    3.871    0.000

#关于画这个结构图 ##semPaths(object, what = “paths”, whatLabels, layout = “tree”, ……) - object:是拟合的对象,就是上文中的“fit” - what:设定图中线的属性, 默认为paths,图中所有的线都为灰色,不显示参数估计值;

semPaths(fit)

  • 若what设定为est、par,则展示估计值,并将线的颜色、粗细、透明度根据参数估计值的大小和显著性做出改变,若设置为stand、std,则展示标准参数估计,若设置为eq、cons,则与默认path相同,如果有限制等式,被限制的相同参数会打上相同的颜色;
semPaths(fit,what = "est")

  • whatLabels:设定图中线的标签,
  • name、label、path、diagram:将边名作为展示的标签,est、par:参数估计值作为边的标签,stand、std:标准参数估计值作为边的标签,eq、cons:参数号作为标签,0表示固定参数,被限制相同的参数编号相同,no、omit、hide、invisible:隐藏标签
  • layout:布局
  • 主要有树状和环状两种布局,每种布局又分别有两种风格。,默认为“tree”,树状的第二种风格如下图,比第一种看起来舒服都了
 semPaths(fit,layout = "tree2")
 semPaths(fit,layout = "circle")
  semPaths(fit,layout = "circle2")

- spring的布局

semPaths(fit,layout = "spring")