您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 黔南分类信息网,免费分类信息发布

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

2019/12/22 16:05:20发布222次查看
摘要:对于熟悉线性回归拟合结构方程模型的分析师来说,在r环境中,拟合结构方程模型涉及学习新的建模语法,新的绘图语法以及通常是新的数据输入方法。然而,拟合结构方程模型可以成为分析师工具箱中的强大工具。
 一   设置环境
在r中实现sem有许多不同的包,lavaan软件包为大多数sem用户提供了全面的功能集,并且具有易于学习的语法来描述sem模型。要安装lavaan,我们只需运行:
# main versioninstall.packages("lavaan")
 二   读入数据
加载lavaan包后,我们需要读入数据集。我们读取方差 - 协方差矩阵并运行路径分析模型。
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
现在我们在我们的环境中命名了一个方差 - 协方差矩阵。
有了这些数据,我们可以构建两种模型
抑郁症(dep)影响免疫系统(imm)影响疾病(ill)
imm影响ill影响dep
使用sem我们可以评估哪个模型最能解释我们在上面的数据中观察到的协方差。拟合模型lavaan是一个两步过程。首先,我们创建一个文本字符串作为lavaan模型 。接下来,我们给出lavaan如何拟合这个模型。
mod1fit<-sem(mod1, sample.cov=mat1, sample.nobs=500)# specify model 2mod2fit<-sem(mod2, sample.cov=mat1, sample.nobs=500)
现在,我们在环境中为每个模型存储了两个对象。我们有模型字符串和modelfit对象。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模型的最佳方法之一是使用路径图直观地检查模型。
接下来,我们加载库并制作一些路径图。
‍‍
这两个简单的路径模型哪个更好?我们可以运行卡方检验。
anova## 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的一些属性。
# goodness of fit measures
##              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##   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##    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
介绍:我们是数据挖掘团队,分享结合实际业务的理论应用和心得体会。欢迎大家关注我们的微信公众号,关注数据挖掘文章;您也可以在公众号中直接发送想说的话,与我们联系交流。
长按二维码即可关注!也请随手推荐我们给你的小伙伴 ↓↓↓↓
更多内容,请点击左下角“阅读原文”查看

黔南分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录