用R进行路径分析
来自OBHRM百科
示意图
R脚本与注释
library(lavaan) # 调用lavaan包,如果没有安装,需要先安装 curwd=setwd("c:\\program files\\mplus\\mplus examples\\user's guide examples") # 设定数据文件所在的目录为工作目录,如果脚本与数据文件在同一目录,则不需要设定 pathdata <- read.table("ex3.11.dat") # 读取数据文件到pathdata,pathdata是自己命名,可以随便定 names(pathdata) <-c(paste("y", 1:3, sep=""),paste("x", 1:3,sep="")) # 给变量命名 pathmodel <- ' y1 ~ x1 +x2 +x3 # 设置模型,pathmodel还是自己根据情况命名。因为是路径分析,用的~ y2 ~ x1 +x2 +x3 y3 ~ x2 +y1 +y2' pathfit <- sem(pathmodel, pathdata) # 进行SEM,pathfit为自己的名称,方便后面调用 summary(pathfit,fit.measures="TRUE") # 显示SEM的总体结果 fitMeasures(pathfit,fit.measures="all", baseline.model=NULL) # 显示所有拟合指数 standardizedSolution(pathfit) # 显示标准化的结果
结果
> summary(pathfit,fit.measures="TRUE") # 显示SEM的总体结果 lavaan (0.5-23.1097) converged normally after 44 iterations Number of observations 500 Estimator ML Minimum Function Test Statistic 0.757 Degrees of freedom 3 P-value (Chi-square) 0.860 Model test baseline model: Minimum Function Test Statistic 4107.449 Degrees of freedom 12 P-value 0.000 User model versus baseline model: Comparative Fit Index (CFI) 1.000 Tucker-Lewis Index (TLI) 1.002 Loglikelihood and Information Criteria: Loglikelihood user model (H0) -4556.552 Loglikelihood unrestricted model (H1) -4556.174 Number of free parameters 12 Akaike (AIC) 9137.105 Bayesian (BIC) 9187.680 Sample-size adjusted Bayesian (BIC) 9149.591 Root Mean Square Error of Approximation: RMSEA 0.000 90 Percent Confidence Interval 0.000 0.040 P-value RMSEA <= 0.05 0.972 Standardized Root Mean Square Residual: SRMR 0.001 Parameter Estimates: Information Expected Standard Errors Standard Regressions: Estimate Std.Err z-value P(>|z|) y1 ~ x1 0.992 0.043 22.979 0.000 x2 2.001 0.045 44.618 0.000 x3 3.052 0.045 68.274 0.000 y2 ~ x1 2.935 0.050 59.002 0.000 x2 1.992 0.052 38.556 0.000 x3 1.023 0.051 19.869 0.000 y3 ~ x2 1.046 0.072 14.539 0.000 y1 0.507 0.020 25.494 0.000 y2 0.746 0.020 37.918 0.000 Variances: Estimate Std.Err z-value P(>|z|) .y1 1.061 0.067 15.811 0.000 .y2 1.408 0.089 15.811 0.000 .y3 1.717 0.109 15.811 0.000 > fitMeasures(pathfit,fit.measures="all", baseline.model=NULL) # 显示所有拟合指数 npar fmin chisq df pvalue 12.000 0.001 0.757 3.000 0.860 baseline.chisq baseline.df baseline.pvalue cfi tli 4107.449 12.000 0.000 1.000 1.002 nnfi rfi nfi pnfi ifi 1.002 0.999 1.000 0.250 1.001 rni logl unrestricted.logl aic bic 1.001 -4556.552 -4556.174 9137.105 9187.680 ntotal bic2 rmsea rmsea.ci.lower rmsea.ci.upper 500.000 9149.591 0.000 0.000 0.040 rmsea.pvalue rmr rmr_nomean srmr srmr_bentler 0.972 0.005 0.005 0.001 0.001 srmr_bentler_nomean srmr_bollen srmr_bollen_nomean srmr_mplus srmr_mplus_nomean 0.001 0.001 0.001 0.001 0.001 cn_05 cn_01 gfi agfi pgfi 5164.205 7496.574 1.000 1.000 0.143 mfi ecvi 1.002 0.050 > standardizedSolution(pathfit) # 显示标准化的结果 lhs op rhs est.std se z pvalue 1 y1 ~ x1 0.254 0.011 23.780 0 2 y1 ~ x2 0.495 0.010 51.513 0 3 y1 ~ x3 0.758 0.007 105.969 0 4 y2 ~ x1 0.759 0.008 92.913 0 5 y2 ~ x2 0.497 0.011 44.664 0 6 y2 ~ x3 0.256 0.012 20.578 0 7 y3 ~ x2 0.192 0.013 14.616 0 8 y3 ~ y1 0.375 0.015 25.561 0 9 y3 ~ y2 0.547 0.014 39.362 0 10 y1 ~~ y1 0.061 0.004 15.841 0 11 y2 ~~ y2 0.082 0.005 15.865 0 12 y3 ~~ y3 0.054 0.004 15.369 0 13 x1 ~~ x1 1.000 0.000 NA NA 14 x1 ~~ x2 0.035 0.000 NA NA 15 x1 ~~ x3 -0.052 0.000 NA NA 16 x2 ~~ x2 1.000 0.000 NA NA 17 x2 ~~ x3 0.089 0.000 NA NA 18 x3 ~~ x3 1.000 0.000 NA NA