用R进行二阶验证性因素分析

来自OBHRM百科
Lichaoping讨论 | 贡献2021年2月14日 (日) 21:19的版本

跳转至: 导航搜索

示意图

Mplus0506.jpg

脚本与注释

library(lavaan)                                                                 # 调用lavaan包,如果没有安装,需要先安装
cfadata <- read.table("http://www.statmodel.com/usersguide/chap5/ex5.6.dat")    # 从Mplus官网读数据到cfadata,cfadata是自己命名,可随便定。如网速慢,可下载到本地
names(cfadata) <-c(paste("y", 1:12, sep=""))                                    # 给变量命名
cfamodel <- ' f1 =~ y1 +y2 +y3                                                  # 设置模型,cfamodel还是自己命名
              f2 =~ y4 +y5 +y6 
              f3 =~ y7 +y8 +y9 
              f4 =~ y10 +y11 +y12 
              f5 =~ f1 +f2 +f3+f4  '                                            # 二阶因素
cfafit <- cfa(cfamodel, cfadata)                                                # 进行CFA,cfafit为自己的名称,方便后面调用
summary(cfafit,fit.measures="TRUE")                                             # 显示CFA的总体结果
fitMeasures(cfafit,fit.measures="all", baseline.model=NULL)                     # 显示所有拟合指数
standardizedSolution(cfafit)                                                    # 显示标准化的结果
library(semTools)                                                               # 调用semTools包,如果没有安装,需要先安装 
reliability(cfafit)                                                             # 计算一阶因素的AVE、CR(Composite Reliability)等值
reliabilityL2(cfafit, "f5")                                                     # 计算二阶因素的CR值
library(semPlot)                                                                # 调用semPlot包,如果没有安装,需要先安装 
semPaths(cfafit, 
         title=F, what = "std", residuals = FALSE,
         intercepts = FALSE, layout = "tree2",
         label.cex=1, edge.label.cex=0.9, font=2,  
         edge.color="black", fixedStyle = c("black",1),freeStyle = c("black",1),esize = 0.01,
         rotation=4)    

结果

> summary(cfafit,fit.measures="TRUE")                                             # 显示CFA的总体结果
lavaan (0.5-23.1097) converged normally after  46 iterations

  Number of observations                           500

  Estimator                                         ML
  Minimum Function Test Statistic               46.743
  Degrees of freedom                                50
  P-value (Chi-square)                           0.605

Model test baseline model:

  Minimum Function Test Statistic             4012.035
  Degrees of freedom                                66
  P-value                                        0.000

User model versus baseline model:

  Comparative Fit Index (CFI)                    1.000
  Tucker-Lewis Index (TLI)                       1.001

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)              -7211.373
  Loglikelihood unrestricted model (H1)      -7188.001

  Number of free parameters                         28
  Akaike (AIC)                               14478.746
  Bayesian (BIC)                             14596.755
  Sample-size adjusted Bayesian (BIC)        14507.881

Root Mean Square Error of Approximation:

  RMSEA                                          0.000
  90 Percent Confidence Interval          0.000  0.026
  P-value RMSEA <= 0.05                          1.000

Standardized Root Mean Square Residual:

  SRMR                                           0.020

Parameter Estimates:

  Information                                 Expected
  Standard Errors                             Standard

Latent Variables:
                   Estimate  Std.Err  z-value  P(>|z|)
  f1 =~                                               
    V1                1.000                           
    V2                0.760    0.031   24.516    0.000
    V3                0.669    0.030   22.392    0.000
  f2 =~                                               
    V4                1.000                           
    V5                0.718    0.030   23.998    0.000
    V6                0.703    0.031   22.946    0.000
  f3 =~                                               
    V7                1.000                           
    V8                0.702    0.026   26.895    0.000
    V9                0.691    0.026   26.829    0.000
  f4 =~                                               
    V10               1.000                           
    V11               0.742    0.029   25.274    0.000
    V12               0.669    0.028   23.496    0.000
  f5 =~                                               
    f1                1.000                           
    f2                0.944    0.146    6.459    0.000
    f3                1.168    0.175    6.670    0.000
    f4                0.854    0.141    6.070    0.000

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)
   .V1                0.348    0.042    8.252    0.000
   .V2                0.251    0.026    9.570    0.000
   .V3                0.320    0.026   12.265    0.000
   .V4                0.366    0.044    8.221    0.000
   .V5                0.268    0.026   10.298    0.000
   .V6                0.330    0.028   11.666    0.000
   .V7                0.272    0.038    7.096    0.000
   .V8                0.282    0.025   11.404    0.000
   .V9                0.276    0.024   11.462    0.000
   .V10               0.362    0.045    8.087    0.000
   .V11               0.266    0.027    9.812    0.000
   .V12               0.323    0.027   12.014    0.000
    f1                0.913    0.103    8.866    0.000
    f2                1.036    0.108    9.606    0.000
    f3                0.984    0.119    8.278    0.000
    f4                1.213    0.115   10.577    0.000
    f5                0.464    0.098    4.712    0.000

> fitMeasures(cfafit,fit.measures="all", baseline.model=NULL)                     # 显示所有拟合指数
               npar                fmin               chisq 
             28.000               0.047              46.743 
                 df              pvalue      baseline.chisq 
             50.000               0.605            4012.035 
        baseline.df     baseline.pvalue                 cfi 
             66.000               0.000               1.000 
                tli                nnfi                 rfi 
              1.001               1.001               0.985 
                nfi                pnfi                 ifi 
              0.988               0.749               1.001 
                rni                logl   unrestricted.logl 
              1.001           -7211.373           -7188.001 
                aic                 bic              ntotal 
          14478.746           14596.755             500.000 
               bic2               rmsea      rmsea.ci.lower 
          14507.881               0.000               0.000 
     rmsea.ci.upper        rmsea.pvalue                 rmr 
              0.026               1.000               0.024 
         rmr_nomean                srmr        srmr_bentler 
              0.024               0.020               0.020 
srmr_bentler_nomean         srmr_bollen  srmr_bollen_nomean 
              0.020               0.020               0.020 
         srmr_mplus   srmr_mplus_nomean               cn_05 
              0.020               0.020             723.081 
              cn_01                 gfi                agfi 
            815.598               0.985               0.976 
               pgfi                 mfi                ecvi 
              0.631               1.003               0.205 
> standardizedSolution(cfafit)                                                    # 显示标准化的结果
   lhs op rhs est.std    se      z pvalue
1   f1 =~  V1   0.894 0.015 60.634      0
2   f1 =~  V2   0.872 0.016 55.670      0
3   f1 =~  V3   0.811 0.019 43.261      0
4   f2 =~  V4   0.894 0.015 60.491      0
5   f2 =~  V5   0.858 0.016 52.492      0
6   f2 =~  V6   0.828 0.018 46.269      0
7   f3 =~  V7   0.925 0.012 79.024      0
8   f3 =~  V8   0.859 0.015 57.711      0
9   f3 =~  V9   0.858 0.015 57.360      0
10  f4 =~ V10   0.900 0.014 64.229      0
11  f4 =~ V11   0.873 0.015 57.427      0
12  f4 =~ V12   0.826 0.018 46.955      0
13  f5 =~  f1   0.580 0.053 10.964      0
14  f5 =~  f2   0.534 0.053 10.066      0
15  f5 =~  f3   0.626 0.053 11.852      0
16  f5 =~  f4   0.467 0.054  8.678      0
17  V1 ~~  V1   0.202 0.026  7.654      0
18  V2 ~~  V2   0.240 0.027  8.770      0
19  V3 ~~  V3   0.342 0.030 11.243      0
20  V4 ~~  V4   0.201 0.026  7.628      0
21  V5 ~~  V5   0.264 0.028  9.399      0
22  V6 ~~  V6   0.315 0.030 10.644      0
23  V7 ~~  V7   0.144 0.022  6.636      0
24  V8 ~~  V8   0.261 0.026 10.207      0
25  V9 ~~  V9   0.263 0.026 10.257      0
26 V10 ~~ V10   0.189 0.025  7.501      0
27 V11 ~~ V11   0.238 0.027  8.949      0
28 V12 ~~ V12   0.317 0.029 10.918      0
29  f1 ~~  f1   0.663 0.061 10.788      0
30  f2 ~~  f2   0.714 0.057 12.592      0
31  f3 ~~  f3   0.609 0.066  9.216      0
32  f4 ~~  f4   0.782 0.050 15.553      0
33  f5 ~~  f5   1.000 0.000     NA     NA
> library(semTools)                                                               # 调用semTools包,如果没有安装,需要先安装 
> reliability(cfafit)                                                             # 计算一阶因素的AVE、CR(Composite Reliability)等值
              f1        f2        f3        f4     total
alpha  0.8852175 0.8853690 0.9030259 0.8903863 0.8477020
omega  0.8984342 0.8982057 0.9177715 0.9045555 0.9478994
omega2 0.8984342 0.8982057 0.9177715 0.9045555 0.9478994
omega3 0.8981943 0.8981957 0.9177694 0.9046450 0.9492241
avevar 0.7522571 0.7515751 0.7933872 0.7651284 0.7659754
> reliabilityL2(cfafit, "f5")                                                     # 计算二阶因素的CR值
       omegaL1        omegaL2 partialOmegaL1 
     0.6044520      0.6377869      0.9206452