R语言代写:结构方程模型、潜变量分析

原文链接:http://tecdat.cn/?p=3071

结构方程模型入门 

介绍

对于熟悉线性回归拟合结构方程模型的分析师来说,首先会感到奇怪。在R环境中,拟合结构方程模型涉及学习新的建模语法,新的绘图语法以及通常是新的数据输入方法。然而,快速重新定位并且很快用户会接触到差异,拟合结构方程模型可以成为分析师工具箱中的强大工具。

  •  构造潜在变量
  • 比较替代模型
  • 对较大数据集的多组分析。

设置 环境

在R中开始使用结构方程建模(SEM)可能是令人生畏的。在R中实现SEM有许多不同的包,并且用户可能有兴趣实现SEM的不同特征。您可能会遇到的一些软件包可以在CRAN Psychometrics Task View中找到

对于那些想要lavaan深入了解该软件包的人来说,它似乎为大多数SEM用户提供了最全面的功能集,并且具有深思熟虑且易于学习的语法来描述SEM模型。要安装lavaan,我们只需运行:

# Main version
install.packages("lavaan")

# Or to install the dev version
library(devtools)
install_github("lavaan", "yrosseel")

读入数据

加载lavaan包后,我们需要读入数据集。lavaan接受两种不同类型的数据,标准R数据帧或方差 - 协方差矩阵。由于后者对我们来说不熟悉来自lmR中的标准线性建模框架,我们将首先阅读最简单的方差 - 协方差矩阵并运行路径分析模型。

 mat1 <- matrix(c(1, 0, 0, 0.6, 1, 0, 0.33, 0.63, 1), 3, 3, byrow = TRUE)

 
 print(mat1)
##      ILL  IMM DEP
## ILL 1.00 0.00   0
## IMM 0.60 1.00   0
## DEP 0.33 0.63   1
# Note that we only input the lower triangle of the matrix. This is
# sufficient though we could put the whole matrix in if we like

现在我们在我们的环境中命名了一个方差 - 协方差矩阵,mat1并且该变量myN对应于我们数据集中的观察数量。另外,我们可以提供R中的全部数据集,并可以得到mat1myN本身。

有了这些数据,我们可以构建两种可能的模型

  1. 抑郁症(DEP)影响免疫系统(IMM)影响疾病(ILL)
  2. IMM影响ILL影响DEP

使用SEM我们可以评估哪个模型最能解释我们在上面的数据中观察到的协方差。拟合模型lavaan是一个两步过程。首先,我们创建一个文本字符串作为lavaan模型,并遵循lavaan 模型语法。接下来,我们给出lavaan了如何使用适合要么这个模型与数据的说明 cfalavaansem功能。在这里我们将使用该sem功能。其他功能将在以后的文章中介绍。

# Specify the model

 
# Give lavaan the command to fit the model
mod1fit <- sem(mod1, sample.cov = mat1, sample.nobs = 500)

# Specify model 2

 
mod2fit <- sem(mod2, sample.cov = mat1, sample.nobs = 500)

现在,我们在环境中为每个模型存储了两个对象。我们有模型字符串和modelfit对象。模型拟合对象(mod1fitmod2fit)是lavaan类对象。这些是具有许多支持方法的S4对象,包括summary提供大量有用输出的方法:

# Summarize the model fit ## lavaan (0.5-14) converged normally after  12 iterations
## 
##   Number of observations                           500
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic                2.994
##   Degrees of freedom                                 1
##   P-value (Chi-square)                           0.084
## 
## Parameter estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
##                    Estimate  Std.err  Z-value  P(>|z|)
## Regressions:
##   ILL ~
##     IMM               0.600    0.036   16.771    0.000
##   IMM ~
##     DEP               0.630    0.035   18.140    0.000
## 
## Variances:
##     ILL               0.639    0.040
##     IMM               0.602    0.038
 ## lavaan (0.5-14) converged normally after  11 iterations
## 
##   Number of observations                           500
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic              198.180
##   Degrees of freedom                                 1
##   P-value (Chi-square)                           0.000
## 
## Parameter estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
##                    Estimate  Std.err  Z-value  P(>|z|)
## Regressions:
##   DEP ~
##     ILL               0.330    0.042    7.817    0.000
##   ILL ~
##     IMM               0.600    0.036   16.771    0.000
## 
## Variances:
##     DEP               0.889    0.056
##     ILL               0.639    0.040

理解SEM模型的最佳方法之一是使用路径图直观地检查模型。感谢这个semPlot包,这在R中很容易做到.2首先,安装semPlot

# Official version
 
# Or to install the dev version
 install_github("semPlot", "SachaEpskamp")

接下来,我们加载库并制作一些路径图。

 semPaths(mod1fit, what = "est",   tree", title   , style = "LISREL")

semPaths(mod2fit,  "est",  "tree",  style = "LISREL")

这两个简单的路径模型看起来很棒。但哪个更好?我们可以运行在一个简单的卡方检验lavaan的对象mod1fitmod2fit

anova(mod1fit, mod2fit)
## Chi Square Difference Test
## 
##         Df  AIC  BIC  Chisq Chisq diff Df diff Pr(>Chisq)    
## mod1fit  1 3786 3803   2.99                                  
## mod2fit  1 3981 3998 198.18        195       0     <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

我们可以非常清楚地看到我们更喜欢模型2.让我们看一下模型2的一些属性,我们可以通过lavaan带有便利函数的对象来访问它们。

# Goodness of fit measures
fitMeasures(mod2fit)
##              fmin             chisq                df            pvalue 
##             0.198           198.180             1.000             0.000 
##    baseline.chisq       baseline.df   baseline.pvalue               cfi 
##           478.973             3.000             0.000             0.586 
##               tli              nnfi               rfi               nfi 
##            -0.243            -0.243             1.000             0.586 
##              pnfi               ifi               rni              logl 
##             0.195             0.587             0.586         -1986.510 
## unrestricted.logl              npar               aic               bic 
##         -1887.420             4.000          3981.020          3997.878 
##            ntotal              bic2             rmsea    rmsea.ci.lower 
##           500.000          3985.182             0.628             0.556 
##    rmsea.ci.upper      rmsea.pvalue               rmr        rmr_nomean 
##             0.703             0.000             0.176             0.176 
##              srmr       srmr_nomean             cn_05             cn_01 
##             0.176             0.176            10.692            17.740 
##               gfi              agfi              pgfi               mfi 
##             0.821            -0.075             0.137             0.821 
##              ecvi 
##             0.412

# Estimates of the model parameters
parameterEstimates(mod2fit, ci = TRUE, boot.ci.type = "norm")
##   lhs op rhs   est    se      z pvalue ci.lower ci.upper
## 1 DEP  ~ ILL 0.330 0.042  7.817      0    0.247    0.413
## 2 ILL  ~ IMM 0.600 0.036 16.771      0    0.530    0.670
## 3 DEP ~~ DEP 0.889 0.056 15.811      0    0.779    1.000
## 4 ILL ~~ ILL 0.639 0.040 15.811      0    0.560    0.718
## 5 IMM ~~ IMM 0.998 0.000     NA     NA    0.998    0.998

# Modification indices
modindices(mod2fit, standardized = TRUE)
##    lhs op rhs    mi    epc sepc.lv sepc.all sepc.nox
## 1  DEP ~~ DEP   0.0  0.000   0.000    0.000    0.000
## 2  DEP ~~ ILL 163.6 -0.719  -0.719   -0.720   -0.720
## 3  DEP ~~ IMM 163.6  0.674   0.674    0.675    0.674
## 4  ILL ~~ ILL   0.0  0.000   0.000    0.000    0.000
## 5  ILL ~~ IMM    NA     NA      NA       NA       NA
## 6  IMM ~~ IMM   0.0  0.000   0.000    0.000    0.000
## 7  DEP  ~ ILL   0.0  0.000   0.000    0.000    0.000
## 8  DEP  ~ IMM 163.6  0.675   0.675    0.675    0.676
## 9  ILL  ~ DEP 163.6 -0.808  -0.808   -0.808   -0.808
## 10 ILL  ~ IMM   0.0  0.000   0.000    0.000    0.000
## 11 IMM  ~ DEP 143.8  0.666   0.666    0.666    0.666
## 12 IMM  ~ ILL   0.0  0.000   0.000    0.000    0.000

而已。从输入方差 - 协方差矩阵到拟合模型,绘制路径图,与备用模型进行比较,最后检查首选模型的参数。 

 

如果您有任何疑问,请在下面发表评论。

原文地址:https://www.cnblogs.com/tecdat/p/10684583.html