SEM代码篇----R详细实现
1. step1:安装包
1 2 3 | # install.packages("lavaan") #install.packages("semPlot") #install.packages("semTools") |
1 | rm(list=ls()) #清除所有变量 |
1 2 3 | library(lavaan) library(semPlot) library(semTools) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | This is lavaan 0.6-7 lavaan is BETA software! Please report any bugs. Warning message: "package 'semPlot' was built under R version 3.5.3" ############################################################################### This is semTools 0.5-3 All users of R (or SEM) are invited to submit functions or ideas for functions. ############################################################################### |
2. lavvan 语法简介
主要公式类型和其他运算符

cfa()和sem()拟合函数的参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | sem(model = NULL, data = NULL, meanstructure = "default", fixed.x = "default", orthogonal = FALSE, std.lv = FALSE, parameterization = "default", std.ov = FALSE, missing = "default", ordered = NULL, sample.cov = NULL, sample.cov.rescale = "default", sample.mean = NULL, sample.nobs = NULL, ridge = 1e-05, group = NULL, group.label = NULL, group.equal = "", group.partial = "", group.w.free = FALSE, cluster = NULL, constraints = "", estimator = "default", likelihood = "default", link = "default", information = "default", se = "default", test = "default", bootstrap = 1000L, mimic = "default", representation = "default", do.fit = TRUE, control = list(), WLS.V = NULL, NACOV = NULL, zero.add = "default", zero.keep.margins = "default", start = "default", verbose = FALSE, warn = TRUE, debug = FALSE) |
lavvan()拟合函数的参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | lavaan(model = NULL, data = NULL, model.type = "sem", meanstructure = "default", int.ov.free = FALSE, int.lv.free = FALSE, fixed.x = "default", orthogonal = FALSE, std.lv = FALSE, parameterization = "default", auto.fix.first = FALSE, auto.fix.single = FALSE, auto.var = FALSE, auto.cov.lv.x = FALSE, auto.cov.y = FALSE, auto.th = FALSE, auto.delta = FALSE, std.ov = FALSE, missing = "default", ordered = NULL, sample.cov = NULL, sample.cov.rescale = "default", sample.mean = NULL, sample.nobs = NULL, ridge = 1e-05, group = NULL, group.label = NULL, group.equal = "", group.partial = "", group.w.free = FALSE, cluster = NULL, constraints = "", estimator = "default", likelihood = "default", link = "default", information = "default", se = "default", test = "default", bootstrap = 1000L, mimic = "default", representation = "default", do.fit = TRUE, control = list(), WLS.V = NULL, NACOV = NULL, zero.add = "default", zero.keep.margins = "default", start = "default", slotOptions = NULL, slotParTable = NULL, slotSampleStats = NULL, slotData = NULL, slotModel = NULL, verbose = FALSE, warn = TRUE, debug = FALSE) |
提取函数:检查合适的模型

3. SEM形式
示例数据生成

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | set.seed(1) n=100 x1 <- rnorm(n) * 10 x2 <- rnorm(n) * 10 x3 <- rnorm(n) * 10 x4 <- rnorm(n) * 10 y <- 100 + 5*x1 + (-2)*x2 + 1*x3 + 0.1*x4 + rnorm(n, sd=40) y1 <- 100 + 5*x1 + (-2)*x2 + 1*x3 + 0.1*x4 + rnorm(n, sd=40) y2 <- 10 + 0.5*x1 + (-2.5)*x2 + 0.1*x3 + 1*x4 + rnorm(n, sd=20) x5 <- rbeta(n,2,5) x6 <- rchisq(n,5) x7 <- rnorm(n,5) myData <- data.frame(y,x1,x2,x3,x4,y1,y2,x5,x6,x7) # write.csv(myData, file = "semData1.csv")#保存匹配数据 # myData <- read.csv("semData1.csv",header = TRUE) |
简单回归示例

回归模型:

1 2 3 4 | sem_Model1 <- ' y ~ x1+x2+x3+x4 ' #线性回归 fit1_1 <- lm(formula = sem_Model1, data = myData) summary(fit1_1) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | Call: lm(formula = sem_Model1, data = myData) Residuals: Min 1Q Median 3Q Max -102.372 -29.458 -3.658 27.275 148.404 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 97.7210 4.7200 20.704 <2e-16 *** x1 5.7733 0.5238 11.022 <2e-16 *** x2 -1.3214 0.4917 -2.688 0.0085 ** x3 1.1350 0.4575 2.481 0.0149 * x4 0.2707 0.4779 0.566 0.5724 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 46.74 on 95 degrees of freedom Multiple R-squared: 0.5911, Adjusted R-squared: 0.5738 F-statistic: 34.33 on 4 and 95 DF, p-value: < 2.2e-16 |
1 2 3 | # lavaan fit1_2 <- sem(model = sem_Model1,data = myData) summary(fit1_2) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | lavaan 0.6-7 ended normally after 32 iterations Estimator ML Optimization method NLMINB Number of free parameters 5 Number of observations 100 Model Test User Model: Test statistic 0.000 Degrees of freedom 0 Parameter Estimates: Standard errors Standard Information Expected Information saturated (h1) model Structured Regressions: Estimate Std.Err z-value P(>|z|) y ~ x1 5.773 0.511 11.309 0.000 x2 -1.321 0.479 -2.757 0.006 x3 1.135 0.446 2.545 0.011 x4 0.271 0.466 0.581 0.561 Variances: Estimate Std.Err z-value P(>|z|) .y 2075.100 293.463 7.071 0.000 |
注:fit1 和 fit2 Std.err存在微小差异的原因

多元因变量回归

1 2 3 4 | sem_Model2 <- 'y1 ~ x1 + x2 + x3 + x4 y2 ~ x1 + x2 + x3 + x4 ' fit2 <- sem(model = sem_Model2,data = myData) summary(fit2) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | lavaan 0.6-7 ended normally after 62 iterations Estimator ML Optimization method NLMINB Number of free parameters 11 Number of observations 100 Model Test User Model: Test statistic 0.000 Degrees of freedom 0 Parameter Estimates: Standard errors Standard Information Expected Information saturated (h1) model Structured Regressions: Estimate Std.Err z-value P(>|z|) y1 ~ x1 4.507 0.426 10.588 0.000 x2 -2.112 0.400 -5.284 0.000 x3 0.888 0.372 2.388 0.017 x4 -0.294 0.388 -0.757 0.449 y2 ~ x1 0.693 0.237 2.924 0.003 x2 -2.721 0.223 -12.230 0.000 x3 0.043 0.207 0.210 0.834 x4 0.724 0.216 3.345 0.001 Covariances: Estimate Std.Err z-value P(>|z|) .y1 ~~ .y2 0.906 80.345 0.011 0.991 Variances: Estimate Std.Err z-value P(>|z|) .y1 1442.879 204.054 7.071 0.000 .y2 447.393 63.271 7.071 0.000 |
递归路径分析

1 2 3 4 5 | sem_Model3 <- 'x5 ~ x1 + x2 + x3 x6 ~ x4 + x5 x7 ~ x6' fit3 <- sem(model = sem_Model3,data = myData) summary(fit3) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | lavaan 0.6-7 ended normally after 47 iterations Estimator ML Optimization method NLMINB Number of free parameters 9 Number of observations 100 Model Test User Model: Test statistic 7.447 Degrees of freedom 9 P-value (Chi-square) 0.591 Parameter Estimates: Standard errors Standard Information Expected Information saturated (h1) model Structured Regressions: Estimate Std.Err z-value P(>|z|) x5 ~ x1 -0.001 0.002 -0.269 0.788 x2 -0.001 0.002 -0.295 0.768 x3 -0.001 0.002 -0.445 0.657 x6 ~ x4 -0.006 0.033 -0.174 0.862 x5 0.217 1.789 0.121 0.903 x7 ~ x6 -0.014 0.034 -0.399 0.690 Variances: Estimate Std.Err z-value P(>|z|) .x5 0.033 0.005 7.071 0.000 .x6 10.714 1.515 7.071 0.000 .x7 1.242 0.176 7.071 0.000 |
中介分析

1 2 3 4 5 6 7 8 | sem_Model4 <- ' y ~ b*y1 + c*y2 y1 ~ a*y2 indirect := a*b total := c + (a*b) ' fit4 <- sem(model = sem_Model4,data = myData,se = "bootstrap") summary(fit4) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | lavaan 0.6-7 ended normally after 18 iterations Estimator ML Optimization method NLMINB Number of free parameters 5 Number of observations 100 Model Test User Model: Test statistic 0.000 Degrees of freedom 0 Parameter Estimates: Standard errors Bootstrap Number of requested bootstrap draws 1000 Number of successful bootstrap draws 1000 Regressions: Estimate Std.Err z-value P(>|z|) y ~ y1 (b) 0.602 0.104 5.788 0.000 y2 (c) 0.240 0.167 1.435 0.151 y1 ~ y2 (a) 0.625 0.166 3.759 0.000 Variances: Estimate Std.Err z-value P(>|z|) .y 3477.433 629.078 5.528 0.000 .y1 3114.961 406.549 7.662 0.000 Defined Parameters: Estimate Std.Err z-value P(>|z|) indirect 0.376 0.111 3.405 0.001 total 0.617 0.171 3.608 0.000 |
路径图绘制
1 2 3 4 5 6 7 | pdf("./img/SEM形式.pdf") p = semPaths(fit1_2) p1=semPaths(object = fit1_2,whatLabels = "std", edge.label.cex = 1) p2 = semPaths(fit2) p3 = semPaths(fit3) p4 = semPaths(fit4) dev.off() |
png: 2
4. 实例分析1

读取数据
该示例数据为lavvan包自带
数据集HolzingerSwineford1939,该数据集内嵌在lavaan包中
构建模型
使用CFA
1 2 3 4 5 6 | HSmodel_1 <- 'visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit5_1 <- cfa(model = HSmodel_1,data = HolzingerSwineford1939) summary(fit5_1, fit.measures = TRUE,standardized = TRUE) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | lavaan 0.6-7 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 Model Test Baseline Model: Test statistic 918.852 Degrees of freedom 36 P-value 0.000 User Model versus Baseline Model: Comparative Fit Index (CFI) 0.931 Tucker-Lewis Index (TLI) 0.896 Loglikelihood and Information Criteria: Loglikelihood user model (H0) -3737.745 Loglikelihood unrestricted model (H1) -3695.092 Akaike (AIC) 7517.490 Bayesian (BIC) 7595.339 Sample-size adjusted Bayesian (BIC) 7528.739 Root Mean Square Error of Approximation: RMSEA 0.092 90 Percent confidence interval - lower 0.071 90 Percent confidence interval - upper 0.114 P-value RMSEA <= 0.05 0.001 Standardized Root Mean Square Residual: SRMR 0.065 Parameter Estimates: Standard errors Standard Information Expected Information saturated (h1) model Structured Latent Variables: Estimate Std.Err z-value P(>|z|) Std.lv Std.all visual =~ x1 1.000 0.900 0.772 x2 0.554 0.100 5.554 0.000 0.498 0.424 x3 0.729 0.109 6.685 0.000 0.656 0.581 textual =~ x4 1.000 0.990 0.852 x5 1.113 0.065 17.014 0.000 1.102 0.855 x6 0.926 0.055 16.703 0.000 0.917 0.838 speed =~ x7 1.000 0.619 0.570 x8 1.180 0.165 7.152 0.000 0.731 0.723 x9 1.082 0.151 7.155 0.000 0.670 0.665 Covariances: Estimate Std.Err z-value P(>|z|) Std.lv Std.all visual ~~ textual 0.408 0.074 5.552 0.000 0.459 0.459 speed 0.262 0.056 4.660 0.000 0.471 0.471 textual ~~ speed 0.173 0.049 3.518 0.000 0.283 0.283 Variances: Estimate Std.Err z-value P(>|z|) Std.lv Std.all .x1 0.549 0.114 4.833 0.000 0.549 0.404 .x2 1.134 0.102 11.146 0.000 1.134 0.821 .x3 0.844 0.091 9.317 0.000 0.844 0.662 .x4 0.371 0.048 7.779 0.000 0.371 0.275 .x5 0.446 0.058 7.642 0.000 0.446 0.269 .x6 0.356 0.043 8.277 0.000 0.356 0.298 .x7 0.799 0.081 9.823 0.000 0.799 0.676 .x8 0.488 0.074 6.573 0.000 0.488 0.477 .x9 0.566 0.071 8.003 0.000 0.566 0.558 visual 0.809 0.145 5.564 0.000 1.000 1.000 textual 0.979 0.112 8.737 0.000 1.000 1.000 speed 0.384 0.086 4.451 0.000 1.000 1.000 |
1 2 3 | #当均值结构参数设置为真时meanstructure=TRUE,均值结构被添加到模型中 fit5_11 <- cfa(model = HSmodel_1, data=HolzingerSwineford1939, meanstructure=TRUE) summary(fit5_11) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | 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 |
使用SEM
1 2 | fit5_2 <- cfa(model = HSmodel_1,data = HolzingerSwineford1939) summary(fit5_2, fit.measures = TRUE,standardized = TRUE) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | lavaan 0.6-7 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 Model Test Baseline Model: Test statistic 918.852 Degrees of freedom 36 P-value 0.000 User Model versus Baseline Model: Comparative Fit Index (CFI) 0.931 Tucker-Lewis Index (TLI) 0.896 Loglikelihood and Information Criteria: Loglikelihood user model (H0) -3737.745 Loglikelihood unrestricted model (H1) -3695.092 Akaike (AIC) 7517.490 Bayesian (BIC) 7595.339 Sample-size adjusted Bayesian (BIC) 7528.739 Root Mean Square Error of Approximation: RMSEA 0.092 90 Percent confidence interval - lower 0.071 90 Percent confidence interval - upper 0.114 P-value RMSEA <= 0.05 0.001 Standardized Root Mean Square Residual: SRMR 0.065 Parameter Estimates: Standard errors Standard Information Expected Information saturated (h1) model Structured Latent Variables: Estimate Std.Err z-value P(>|z|) Std.lv Std.all visual =~ x1 1.000 0.900 0.772 x2 0.554 0.100 5.554 0.000 0.498 0.424 x3 0.729 0.109 6.685 0.000 0.656 0.581 textual =~ x4 1.000 0.990 0.852 x5 1.113 0.065 17.014 0.000 1.102 0.855 x6 0.926 0.055 16.703 0.000 0.917 0.838 speed =~ x7 1.000 0.619 0.570 x8 1.180 0.165 7.152 0.000 0.731 0.723 x9 1.082 0.151 7.155 0.000 0.670 0.665 Covariances: Estimate Std.Err z-value P(>|z|) Std.lv Std.all visual ~~ textual 0.408 0.074 5.552 0.000 0.459 0.459 speed 0.262 0.056 4.660 0.000 0.471 0.471 textual ~~ speed 0.173 0.049 3.518 0.000 0.283 0.283 Variances: Estimate Std.Err z-value P(>|z|) Std.lv Std.all .x1 0.549 0.114 4.833 0.000 0.549 0.404 .x2 1.134 0.102 11.146 0.000 1.134 0.821 .x3 0.844 0.091 9.317 0.000 0.844 0.662 .x4 0.371 0.048 7.779 0.000 0.371 0.275 .x5 0.446 0.058 7.642 0.000 0.446 0.269 .x6 0.356 0.043 8.277 0.000 0.356 0.298 .x7 0.799 0.081 9.823 0.000 0.799 0.676 .x8 0.488 0.074 6.573 0.000 0.488 0.477 .x9 0.566 0.071 8.003 0.000 0.566 0.558 visual 0.809 0.145 5.564 0.000 1.000 1.000 textual 0.979 0.112 8.737 0.000 1.000 1.000 speed 0.384 0.086 4.451 0.000 1.000 1.000 |
使用lavvan
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | HSmodel_3 <- ' # latent variables visual =~ 1*x1 + x2 + x3 textual =~ 1*x4 + x5 + x6 speed =~ 1*x7 + x8 + x9 # factor (co)variances visual ~~ visual; visual ~~ textual; visual ~~ speed textual ~~ textual; textual ~~ speed speed ~~ speed # residual variances x1 ~~ x1; x2 ~~ x2; x3 ~~ x3 x4 ~~ x4; x5 ~~ x5; x6 ~~ x6 x7 ~~ x7; x8 ~~ x8; x9 ~~ x9 ' fit5_3 <- lavaan(model = HSmodel_3, data = HolzingerSwineford1939) summary(fit5_3, fit.measures=TRUE,standardized=TRUE) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | lavaan 0.6-7 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 Model Test Baseline Model: Test statistic 918.852 Degrees of freedom 36 P-value 0.000 User Model versus Baseline Model: Comparative Fit Index (CFI) 0.931 Tucker-Lewis Index (TLI) 0.896 Loglikelihood and Information Criteria: Loglikelihood user model (H0) -3737.745 Loglikelihood unrestricted model (H1) -3695.092 Akaike (AIC) 7517.490 Bayesian (BIC) 7595.339 Sample-size adjusted Bayesian (BIC) 7528.739 Root Mean Square Error of Approximation: RMSEA 0.092 90 Percent confidence interval - lower 0.071 90 Percent confidence interval - upper 0.114 P-value RMSEA <= 0.05 0.001 Standardized Root Mean Square Residual: SRMR 0.065 Parameter Estimates: Standard errors Standard Information Expected Information saturated (h1) model Structured Latent Variables: Estimate Std.Err z-value P(>|z|) Std.lv Std.all visual =~ x1 1.000 0.900 0.772 x2 0.554 0.100 5.554 0.000 0.498 0.424 x3 0.729 0.109 6.685 0.000 0.656 0.581 textual =~ x4 1.000 0.990 0.852 x5 1.113 0.065 17.014 0.000 1.102 0.855 x6 0.926 0.055 16.703 0.000 0.917 0.838 speed =~ x7 1.000 0.619 0.570 x8 1.180 0.165 7.152 0.000 0.731 0.723 x9 1.082 0.151 7.155 0.000 0.670 0.665 Covariances: Estimate Std.Err z-value P(>|z|) Std.lv Std.all visual ~~ textual 0.408 0.074 5.552 0.000 0.459 0.459 speed 0.262 0.056 4.660 0.000 0.471 0.471 textual ~~ speed 0.173 0.049 3.518 0.000 0.283 0.283 Variances: Estimate Std.Err z-value P(>|z|) Std.lv Std.all visual 0.809 0.145 5.564 0.000 1.000 1.000 textual 0.979 0.112 8.737 0.000 1.000 1.000 speed 0.384 0.086 4.451 0.000 1.000 1.000 .x1 0.549 0.114 4.833 0.000 0.549 0.404 .x2 1.134 0.102 11.146 0.000 1.134 0.821 .x3 0.844 0.091 9.317 0.000 0.844 0.662 .x4 0.371 0.048 7.779 0.000 0.371 0.275 .x5 0.446 0.058 7.642 0.000 0.446 0.269 .x6 0.356 0.043 8.277 0.000 0.356 0.298 .x7 0.799 0.081 9.823 0.000 0.799 0.676 .x8 0.488 0.074 6.573 0.000 0.488 0.477 .x9 0.566 0.071 8.003 0.000 0.566 0.558 |
多组别分析

1 2 3 4 5 6 7 8 | # model 1: configural invariance fit5_41 <- cfa(HSmodel_1, data=HolzingerSwineford1939, group="school") # model 2: weak invariance fit5_42 <- cfa(HSmodel_1, data=HolzingerSwineford1939, group="school",group.equal="loadings") # model 3: strong invariance fit5_43 <- cfa(HSmodel_1, data=HolzingerSwineford1939, group="school",group.equal=c("loadings", "intercepts")) # model 4: equal loadings + intercepts + means fit5_44 <- cfa(HSmodel_1, data=HolzingerSwineford1939, group="school",group.equal=c("loadings", "intercepts", "means")) |
两模型比较(anova)
1 | anova(fit5_41, fit5_42) |
| Df | AIC | BIC | Chisq | Chisq diff | Df diff | Pr(>Chisq) | |
|---|---|---|---|---|---|---|---|
| fit5_41 | 48 | 7484.395 | 7706.822 | 115.8513 | NA | NA | NA |
| fit5_42 | 54 | 7480.587 | 7680.771 | 124.0435 | 8.1922 | 6 | 0.2243577 |
测量不变性检验(semTools -> measurementInvariance)
两两比较
1 | measurementInvariance(HSmodel_1, data=HolzingerSwineford1939,group="school", strict=FALSE) |
1 2 3 4 5 6 7 | Error in library(semTools): there is no package called 'semTools' Traceback: 1. library(semTools) 2. stop(txt, domain = NA) |
缺失数据处理 FIML ( full informationmaximum likelihood ) estimation
1) 随机缺失(lavvan中可解决)
MCAR (missing completely at random):数据缺失完全随机,估计结果无偏
MAR (missing at random):缺失数据不依赖于缺失数据本身,可能与未缺失数据有关
2)非随机缺失
NMAR(not missing at random): 一般来说,几乎所有的标准统计方法都不再有效。我们只能试着了解其缺失机制,并在建模数据时考虑到这一点。
1 | fit5_5 <- cfa(HSmodel_1, data = HSmissing,missing = "ml") # or missing = "fiml" |
绘制路径图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | dev.new() pdf("./img/实例分析1.pdf") #初始图 fitname = fit5_3 p = semPaths(fitname) #含系数的图 p1=semPaths(object = fitname,whatLabels = "std", edge.label.cex = 1) #不同形状的图 p2=semPaths(object = fitname,layout = "circle") p3=semPaths(object = fitname,layout = "tree2") p4=semPaths(object = fitname,layout = "circle2") p5=semPaths(object = fitname,layout = "spring") #旋转图 p6 = semPaths(fitname,rotation=2) #线条颜色设置 p7 = semPaths(fitname,rotation=2,edge.color = "blue") #变量颜色设置 p8 = semPaths(fitname,rotation=2,edge.color = "blue",color = "lightblue",whatLabels = "std", edge.label.cex = 1) #分组 p9 = semPaths(fit5_41,rotation=2,edge.color = "blue",color = "lightblue",whatLabels = "std", edge.label.cex = 1 ) p10 = semPaths(fit5_41,rotation=2,whatLabels = "std", edge.label.cex = 1,groups="latents" ) dev.off() |
png: 2
5. 实例分析2(带约束的SEM)

模型构建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | PDmodel <- ' # latent variable definitions ind60 =~ x1 + x2 + x3 dem60 =~ y1 + a*y2 + b*y3 + c*y4 dem65 =~ y5 + a*y6 + b*y7 + c*y8 # regressions dem60 ~ ind60 dem65 ~ ind60 + dem60 # residual covariances y1 ~~ y5 y2 ~~ y4 + y6 y3 ~~ y7 y4 ~~ y8 y6 ~~ y8 ' |
结果输出
1 2 | fit6 <- sem(PDmodel, data=PoliticalDemocracy) summary(fit6, standardized=TRUE) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | lavaan 0.6-7 ended normally after 66 iterations Estimator ML Optimization method NLMINB Number of free parameters 31 Number of equality constraints 3 Number of observations 75 Model Test User Model: Test statistic 40.179 Degrees of freedom 38 P-value (Chi-square) 0.374 Parameter Estimates: Standard errors Standard Information Expected Information saturated (h1) model Structured Latent Variables: Estimate Std.Err z-value P(>|z|) Std.lv Std.all ind60 =~ x1 1.000 0.670 0.920 x2 2.180 0.138 15.751 0.000 1.460 0.973 x3 1.818 0.152 11.971 0.000 1.218 0.872 dem60 =~ y1 1.000 2.201 0.850 y2 (a) 1.191 0.139 8.551 0.000 2.621 0.690 y3 (b) 1.175 0.120 9.755 0.000 2.586 0.758 y4 (c) 1.251 0.117 10.712 0.000 2.754 0.838 dem65 =~ y5 1.000 2.154 0.817 y6 (a) 1.191 0.139 8.551 0.000 2.565 0.755 y7 (b) 1.175 0.120 9.755 0.000 2.530 0.802 y8 (c) 1.251 0.117 10.712 0.000 2.694 0.829 Regressions: Estimate Std.Err z-value P(>|z|) Std.lv Std.all dem60 ~ ind60 1.471 0.392 3.750 0.000 0.448 0.448 dem65 ~ ind60 0.600 0.226 2.661 0.008 0.187 0.187 dem60 0.865 0.075 11.554 0.000 0.884 0.884 Covariances: Estimate Std.Err z-value P(>|z|) Std.lv Std.all .y1 ~~ .y5 0.583 0.356 1.637 0.102 0.583 0.281 .y2 ~~ .y4 1.440 0.689 2.092 0.036 1.440 0.291 .y6 2.183 0.737 2.960 0.003 2.183 0.356 .y3 ~~ .y7 0.712 0.611 1.165 0.244 0.712 0.169 .y4 ~~ .y8 0.363 0.444 0.817 0.414 0.363 0.111 .y6 ~~ .y8 1.372 0.577 2.378 0.017 1.372 0.338 Variances: Estimate Std.Err z-value P(>|z|) Std.lv Std.all .x1 0.081 0.019 4.182 0.000 0.081 0.154 .x2 0.120 0.070 1.729 0.084 0.120 0.053 .x3 0.467 0.090 5.177 0.000 0.467 0.239 .y1 1.855 0.433 4.279 0.000 1.855 0.277 .y2 7.581 1.366 5.549 0.000 7.581 0.525 .y3 4.956 0.956 5.182 0.000 4.956 0.426 .y4 3.225 0.723 4.458 0.000 3.225 0.298 .y5 2.313 0.479 4.831 0.000 2.313 0.333 .y6 4.968 0.921 5.393 0.000 4.968 0.430 .y7 3.560 0.710 5.018 0.000 3.560 0.357 .y8 3.308 0.704 4.701 0.000 3.308 0.313 ind60 0.449 0.087 5.175 0.000 1.000 1.000 .dem60 3.875 0.866 4.477 0.000 0.800 0.800 .dem65 0.164 0.227 0.725 0.469 0.035 0.035 |
绘制路径图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | dev.new() pdf("./img/实例分析2.pdf") #初始图 p = semPaths(fit6) #含系数的图 p1=semPaths(object = fit6,whatLabels = "std", edge.label.cex = 1) #不同形状的图 p2=semPaths(object = fit6,layout = "circle") p3=semPaths(object = fit6,layout = "tree2") p4=semPaths(object = fit6,layout = "circle2") p5=semPaths(object = fit6,layout = "spring") #旋转图 p6 = semPaths(fit6,rotation=2) #线条颜色设置 p7 = semPaths(fit6,rotation=2,edge.color = "blue") #变量颜色设置 p8 = semPaths(fit6,rotation=2,edge.color = "blue",color = "lightblue",whatLabels = "std", edge.label.cex = 1) dev.off() # dev.new() # pdf("./img/实例分析2.pdf") # #初始图 # title("Regression of MPG on Weight") # p = semPaths(fit6,title=TRUE,"图1 实例分析2(初始tree)", title.color = "black") # #含系数的图 # p1=semPaths(object = fit6,title="图2 实例分析2路径系数", title.color = "black",whatLabels = "std", edge.label.cex = 1) # #不同形状的图 # p2=semPaths(object = fit6,title="图3 实例分析2(初始circle)", title.color = "black",layout = "circle") # spring;tree2;circle2 # p3=semPaths(object = fit6,title="图4 实例分析2(初始tree2)", title.color = "black",layout = "tree2") # p4=semPaths(object = fit6,title="图5 实例分析2(初始circle2)",title.color = "black",layout = "circle2") # p5=semPaths(object = fit6,title="图6 实例分析2(初始spring)", title.color = "black",layout = "spring") # #旋转图 # p6 = semPaths(fit6,title="图7 实例分析2(初始tree旋转)", title.color = "black",rotation=2) # #线条颜色设置 # p7 = semPaths(fit6,title="图8 实例分析2(初始tree旋转蓝线条)", title.color = "black",rotation=2,edge.color = "blue") # #变量颜色设置 # p8 = semPaths(fit6,title="图9 实例分析2(初始tree旋转蓝)", title.color = "black",rotation=2,edge.color = "blue",color = "blue") # dev.off() |
png: 2
6. 连续非正态数据和替代估计量
连续非正态数据:左偏、右偏、尖峰、厚尾等
常见三种处理方法:
- ADF(asymptotically distribution-free) estimation
2)ML(maximum likelihood)
3)bootstrapping
方法1:ADF
ADF估计量(Browne, 1984)无正态性假设,称为加权最小二乘(WLS)估计
研究表明,只有样本量很大(例如,N > 5000)时效果较好,否则ADF方法会失败
1 | fit5_6 <- cfa(HSmodel_1, data = HolzingerSwineford1939,estimator = "WLS") |
方法2:ML
1 2 | # robust standard errors fit5_71 <- cfa(HSmodel_1, data = HolzingerSwineford1939,,se = "robust") |
1 2 | #Satorra-Bentler scaled test statistic fit5_72 <- cfa(HSmodel_1, data = HolzingerSwineford1939,test = "Satorra-Bentler") |
1 2 | # Robust standard errors + scaled test statistic fit5_73 <- cfa(HSmodel_1, data = HolzingerSwineford1939,se = "robust", test = "Satorra-Bentler") |
1 2 | # estimator MLM = robust standard errors + scaled test statistic fit5_74 <- cfa(HSmodel_1, data = HolzingerSwineford1939,estimator = "MLM") |
1 2 | # alternative: estimator MLR (同时含缺失数据) fit5_75 <- cfa(HSmodel_1, data = HolzingerSwineford1939,estimator = "MLR", missing = "ml") |
方法3:bootstrapping
1 2 3 4 | # bootstrapping standard errors:(可输出置信区间) fit5_81 <- cfa(HSmodel_1, data = HolzingerSwineford1939,se = "bootstrap", verbose = TRUE, bootstrap = 1000) # bootstrapping the test statistic fit5_82 <- cfa(HSmodel_1, data = HolzingerSwineford1939,test = "bootstrap", verbose = TRUE, bootstrap = 1000) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | Quasi-Newton steps using NLMINB: Objective function = 2.0640106626333683 Objective function = 0.5481724334022804 Objective function = 0.4744000434739597 Objective function = 0.3116651572650557 Objective function = 0.2935347771542274 Objective function = 0.2091927643393836 Objective function = 0.1986590305865628 Objective function = 0.1549912702721032 Objective function = 0.1519382593582579 Objective function = 0.1472224332311631 。。。。。。。部分结果略。。。。。。 ... bootstrap draw number: 999 OK -- niter = 26 fx = 0.158705135 ... bootstrap draw number: 1000 OK -- niter = 26 fx = 0.244148094 Number of successful bootstrap draws: 1000 done. Computing TEST for test(s) = standard ... done. Fitting baseline model ... done. Quasi-Newton steps using NLMINB: Objective function = 2.0640106626333683 Objective function = 0.5481724334022804 Objective function = 0.4744000434739597 Objective function = 0.3116651572650557 Objective function = 0.2935347771542274 |
。。。。。。。部分结果略。。。。。。
… bootstrap draw number: 999 OK – niter = 25 fx = 0.041584423
… bootstrap draw number: 1000 OK – niter = 27 fx = 0.045263513
Number of successful bootstrap draws: 1000
done.
Fitting baseline model … done.
1 2 3 4 5 6 7 8 | # 使用bootstrapLavaan()计算Bolen-Stine p值(可配置) fit5_83 <- cfa(HSmodel_1, data=HolzingerSwineford1939, se="none") T.orig <- fitMeasures(fit5_83, "chisq") T.boot <- bootstrapLavaan(fit5_83,R = 10,type = "bollen.stine",FUN = fitMeasures,fit5_83.measures = "chisq") pvalue.boot <- length(which(T.boot > T.orig))/length(T.boot) |
7. 分类内生变量
二分类数据、多分类数据、定序数据、计数数据、生存数据
常见三种处理方法:
WLSMV , logit/probit
方法1:阈值法连续化
观察到的变量y通常可以看作是对潜在的连续响应y*的部分观察。
例如:K =4 的有序响应变量类别:

1 2 3 4 5 | # generate ‘ordered’ data with 4 categories y_ord4 <- sample(1:4, size = 100, replace = TRUE) prop <- table(Y)/sum(table(Y)) cprop <- c(0, cumsum(prop)) th <- qnorm(cprop) |
方法2 :probit
存在外生协变量时,可直接利用probit回归
– tetrachoric (binary – binary)
– polychoric (ordered – ordered)
– polyserial (ordered – numeric)
– biserial (binary – numeric)
– pearson (numeric – numeric)
ordered=c(“item1”,“item2”,“item3”,“item4”)可将多个变量有序化
1 2 3 4 5 6 | library(MASS) X1 <- rnorm(100) X2 <- rnorm(100) X3 <- rnorm(100) fit7_1 <- polr(ordered(y_ord4) ? X1 + X2 + X3, method = "probit") fit7_1$zeta |
示例:实例1数据
1 2 3 4 5 | HS9 <- HolzingerSwineford1939[,c("x1","x2","x3","x4","x5","x6","x7","x8","x9")] HSbinary <- as.data.frame( lapply(HS9, cut, 2, labels=FALSE) ) # binary CFA fit7_2 <- cfa(HSmodel_1, data=HSbinary, ordered=names(HSbinary)) summary(fit7_2,fit7_2.measures=TRUE) |
1 | inspect(fit7_2, "sampstat") |
8. 提取函数应用示例
1 2 3 4 5 | #见实例分析1 HSmodel_1 <- 'visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' |
1 | fit8 <- cfa(HSmodel_1, data=HolzingerSwineford1939) |
1 | fitted(fit8) |
$cov =
| x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8 | x9 | |
|---|---|---|---|---|---|---|---|---|---|
| x1 | 1.3583700 | 0.4479566 | 0.5902910 | 0.4082324 | 0.4543940 | 0.3780829 | 0.2622246 | 0.3094121 | 0.2836038 |
| x2 | 0.4479566 | 1.3817831 | 0.3267262 | 0.2259568 | 0.2515072 | 0.2092690 | 0.1451414 | 0.1712597 | 0.1569748 |
| x3 | 0.5902910 | 0.3267262 | 1.2748647 | 0.2977526 | 0.3314214 | 0.2757624 | 0.1912588 | 0.2256760 | 0.2068522 |
| x4 | 0.4082324 | 0.2259568 | 0.2977526 | 1.3506644 | 1.0902489 | 0.9071522 | 0.1734947 | 0.2047152 | 0.1876397 |
| x5 | 0.4543940 | 0.2515072 | 0.3314214 | 1.0902489 | 1.6597856 | 1.0097299 | 0.1931129 | 0.2278637 | 0.2088574 |
| x6 | 0.3780829 | 0.2092690 | 0.2757624 | 0.9071522 | 1.0097299 | 1.1963583 | 0.1606814 | 0.1895962 | 0.1737818 |
| x7 | 0.2622246 | 0.1451414 | 0.1912588 | 0.1734947 | 0.1931129 | 0.1606814 | 1.1831393 | 0.4528034 | 0.4150347 |
| x8 | 0.3094121 | 0.1712597 | 0.2256760 | 0.2047152 | 0.2278637 | 0.1895962 | 0.4528034 | 1.0219828 | 0.4897205 |
| x9 | 0.2836038 | 0.1569748 | 0.2068522 | 0.1876397 | 0.2088574 | 0.1737818 | 0.4150347 | 0.4897205 | 1.0150038 |
1 | predict(fit8) |
| visual | textual | speed |
|---|---|---|
| -0.81767524 | -0.13754501 | 0.06150726 |
| 0.04951940 | -1.01272402 | 0.62549360 |
| -0.76139670 | -1.87228634 | -0.84057276 |
| 0.41934153 | 0.01848569 | -0.27133710 |
| -0.41590481 | -0.12225009 | 0.19432951 |
| 0.02325632 | -1.32981727 | 0.70885348 |
| -0.98496789 | 0.65761381 | 0.04370647 |
| 0.10684376 | -0.10512788 | -0.53230194 |
| -0.25397131 | 0.40433106 | 0.20036715 |
| -1.05421886 | 0.09291512 | -0.56666789 |
| -0.75848238 | -0.76939476 | -0.32272938 |
| 0.39808636 | 0.09621166 | -0.36351841 |
| 0.51898256 | -0.14038719 | 0.28709094 |
| 0.24108494 | 0.21574804 | -0.24259754 |
| 0.70257424 | 1.15797443 | -0.12973764 |
| -0.42473596 | -0.59549971 | -0.55369318 |
| -0.58761600 | -0.69464666 | 0.12080778 |
| -0.05083838 | -0.95018913 | -0.10375995 |
| 0.66490790 | 1.09597201 | -0.04174159 |
| 0.97772373 | 0.18795244 | -0.12304567 |
| 0.23952816 | -1.35930395 | -0.43925580 |
| 1.33320492 | -0.52348581 | 0.47433613 |
| -0.01872902 | 0.72510961 | -0.39098728 |
| -0.65236812 | -0.85126858 | 0.35389317 |
| -0.14193523 | -0.64671143 | -0.44254826 |
| 0.07459743 | -0.84369449 | 0.17047103 |
| 0.35906264 | -1.08215625 | -0.26763982 |
| 0.06073595 | -1.07208831 | -0.37789768 |
| 0.36284640 | -0.68722561 | 0.87878736 |
| -0.53281609 | -0.54291209 | -0.16260680 |
| ... | ... | ... |
| 0.36579463 | 0.286998384 | 0.16020380 |
| -0.11301304 | -0.008594389 | 0.78275251 |
| -0.31322664 | -0.117325314 | 0.02863657 |
| 0.43165862 | -0.552461192 | 0.39482293 |
| -1.05019439 | -0.817906236 | -0.76617073 |
| -0.17358766 | 0.021497247 | -0.40875089 |
| -0.05252645 | 0.367863096 | -0.17593087 |
| 0.15839234 | -0.792699519 | 0.50487360 |
| 0.57832663 | -0.121716586 | 0.30412862 |
| -0.88456464 | -0.452929413 | 0.11865986 |
| 0.30021737 | 1.335667006 | -0.78516652 |
| -0.61851705 | -0.260155389 | -0.71446297 |
| 0.72163334 | 0.713018089 | -0.32928632 |
| -0.14855811 | -0.477111209 | 0.51598547 |
| 0.14577715 | -0.219169373 | -0.08770218 |
| 0.56806351 | 1.606779375 | 0.09576261 |
| -0.46039523 | -0.501810492 | 0.41742089 |
| -0.49595506 | 0.380804720 | 0.41931919 |
| 0.39787129 | -0.001286655 | -0.20616710 |
| -1.00453500 | -0.370879351 | 0.37667244 |
| -0.49694338 | -1.079965387 | -0.12580818 |
| 0.14006996 | 0.014458282 | 0.12929032 |
| 0.24014891 | 0.086748659 | 0.01910399 |
| 0.18492002 | -0.477628434 | -0.08199923 |
| 0.62226627 | 0.181525443 | -0.34620461 |
| -0.49495815 | -0.507343016 | 0.05023989 |
| -0.93999794 | -0.698817917 | 0.21812239 |
| 0.03677903 | 1.169004482 | 0.56918416 |
| -0.47087734 | 0.138702247 | 0.04466651 |
| 0.14776003 | 0.848046807 | 0.33771119 |
1 | inspect(fit8) # matrix representation |
-
A lavaan.matrix: 9 × 3 of type dbl
visual textual speed x1 0 0 0 x2 1 0 0 x3 2 0 0 x4 0 0 0 x5 0 3 0 x6 0 4 0 x7 0 0 0 x8 0 0 5 x9 0 0 6 -
A lavaan.matrix.symmetric: 9 × 9 of type dbl
x1 x2 x3 x4 x5 x6 x7 x8 x9 x1 7 0 0 0 0 0 0 0 0 x2 0 8 0 0 0 0 0 0 0 x3 0 0 9 0 0 0 0 0 0 x4 0 0 0 10 0 0 0 0 0 x5 0 0 0 0 11 0 0 0 0 x6 0 0 0 0 0 12 0 0 0 x7 0 0 0 0 0 0 13 0 0 x8 0 0 0 0 0 0 0 14 0 x9 0 0 0 0 0 0 0 0 15 -
A lavaan.matrix.symmetric: 3 × 3 of type dbl
visual textual speed visual 16 19 20 textual 19 17 21 speed 20 21 18
$lambda
$theta
$psi
1 | inspect(fit8, "sampstat") |
$cov =
| x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8 | x9 | |
|---|---|---|---|---|---|---|---|---|---|
| x1 | 1.35836985 | 0.40737133 | 0.57989932 | 0.5048350 | 0.4406155 | 0.4548081 | 0.08476543 | 0.2638377 | 0.4583385 |
| x2 | 0.40737133 | 1.38178387 | 0.45106394 | 0.2089234 | 0.2110911 | 0.2475446 | -0.09675102 | 0.1096594 | 0.2440095 |
| x3 | 0.57989932 | 0.45106394 | 1.27486486 | 0.2081696 | 0.1122963 | 0.2441090 | 0.08834184 | 0.2123226 | 0.3738526 |
| x4 | 0.50483500 | 0.20892337 | 0.20816961 | 1.3506645 | 1.0977528 | 0.8955157 | 0.21974259 | 0.1255933 | 0.2433627 |
| x5 | 0.44061554 | 0.21109108 | 0.11229629 | 1.0977528 | 1.6597858 | 1.0145240 | 0.14299955 | 0.1806052 | 0.2952413 |
| x6 | 0.45480813 | 0.24754457 | 0.24410898 | 0.8955157 | 1.0145240 | 1.1963584 | 0.14407839 | 0.1654310 | 0.2359969 |
| x7 | 0.08476543 | -0.09675102 | 0.08834184 | 0.2197426 | 0.1429995 | 0.1440784 | 1.18313946 | 0.5352452 | 0.3732972 |
| x8 | 0.26383768 | 0.10965939 | 0.21232264 | 0.1255933 | 0.1806052 | 0.1654310 | 0.53524522 | 1.0219828 | 0.4573166 |
| x9 | 0.45833855 | 0.24400945 | 0.37385257 | 0.2433627 | 0.2952413 | 0.2359969 | 0.37329722 | 0.4573166 | 1.0150039 |
1 | fitMeasures(fit8) |
- 21
- 0.141703524534838
- 85.3055217699727
- 24
- 8.50255321704907e-09
- 918.851589292384
- 36
- 0
- 0.930559651799336
- 0.895839477699004
- 0.895839477699004
- 0.86074107707263
- 0.90716071804842
- 0.60477381203228
- 0.931490849987258
- 0.930559651799336
- -3737.7449266262
- -3695.09216574121
- 7517.48985325239
- 7595.33916881212
- 301
- 7528.73911173078
- 0.0921214845101686
- 0.0714184904399382
- 0.113678016811963
- 0.000661236778051855
- 0.0821843333342268
- 0.0821843333342268
- 0.0652050571843865
- 0.0652050571843865
- 0.0652050571843865
- 0.0729014727869723
- 0.0729014727869723
- 0.0652050595246683
- 0.0652050595246683
- 129.490200301456
- 152.654026533352
- 0.943332061258476
- 0.893747614859642
- 0.503110432671187
- 0.903177285931363
- 0.422941932790607
npar
fmin
chisq
df
pvalue
baseline.chisq
baseline.df
baseline.pvalue
cfi
tli
nnfi
rfi
nfi
pnfi
ifi
rni
logl
unrestricted.logl
aic
bic
ntotal
bic2
rmsea
rmsea.ci.lower
rmsea.ci.upper
rmsea.pvalue
rmr
rmr_nomean
srmr
srmr_bentler
srmr_bentler_nomean
crmr
crmr_nomean
srmr_mplus
srmr_mplus_nomean
cn_05
cn_01
gfi
agfi
pgfi
mfi
ecvi
1 | parameterTable(fit8) |
| id | lhs | op | rhs | user | block | group | free | ustart | exo | label | plabel | start | est | se |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | visual | =~ | x1 | 1 | 1 | 1 | 0 | 1 | 0 | .p1. | 1.0000000 | 1.0000000 | 0.00000000 | |
| 2 | visual | =~ | x2 | 1 | 1 | 1 | 1 | NA | 0 | .p2. | 0.7778315 | 0.5535003 | 0.09966512 | |
| 3 | visual | =~ | x3 | 1 | 1 | 1 | 2 | NA | 0 | .p3. | 1.1072550 | 0.7293702 | 0.10910970 | |
| 4 | textual | =~ | x4 | 1 | 1 | 1 | 0 | 1 | 0 | .p4. | 1.0000000 | 1.0000000 | 0.00000000 | |
| 5 | textual | =~ | x5 | 1 | 1 | 1 | 3 | NA | 0 | .p5. | 1.1328936 | 1.1130766 | 0.06542011 | |
| 6 | textual | =~ | x6 | 1 | 1 | 1 | 4 | NA | 0 | .p6. | 0.9241826 | 0.9261462 | 0.05544886 | |
| 7 | speed | =~ | x7 | 1 | 1 | 1 | 0 | 1 | 0 | .p7. | 1.0000000 | 1.0000000 | 0.00000000 | |
| 8 | speed | =~ | x8 | 1 | 1 | 1 | 5 | NA | 0 | .p8. | 1.2250736 | 1.1799508 | 0.16498657 | |
| 9 | speed | =~ | x9 | 1 | 1 | 1 | 6 | NA | 0 | .p9. | 0.8544057 | 1.0815302 | 0.15116744 | |
| 10 | x1 | ~~ | x1 | 0 | 1 | 1 | 7 | NA | 0 | .p10. | 0.6791849 | 0.5490540 | 0.11360092 | |
| 11 | x2 | ~~ | x2 | 0 | 1 | 1 | 8 | NA | 0 | .p11. | 0.6908919 | 1.1338390 | 0.10172337 | |
| 12 | x3 | ~~ | x3 | 0 | 1 | 1 | 9 | NA | 0 | .p12. | 0.6374324 | 0.8443240 | 0.09062318 | |
| 13 | x4 | ~~ | x4 | 0 | 1 | 1 | 10 | NA | 0 | .p13. | 0.6753323 | 0.3711730 | 0.04771779 | |
| 14 | x5 | ~~ | x5 | 0 | 1 | 1 | 11 | NA | 0 | .p14. | 0.8298929 | 0.4462551 | 0.05839278 | |
| 15 | x6 | ~~ | x6 | 0 | 1 | 1 | 12 | NA | 0 | .p15. | 0.5981792 | 0.3562027 | 0.04303497 | |
| 16 | x7 | ~~ | x7 | 0 | 1 | 1 | 13 | NA | 0 | .p16. | 0.5915697 | 0.7993916 | 0.08138156 | |
| 17 | x8 | ~~ | x8 | 0 | 1 | 1 | 14 | NA | 0 | .p17. | 0.5109914 | 0.4876971 | 0.07419409 | |
| 18 | x9 | ~~ | x9 | 0 | 1 | 1 | 15 | NA | 0 | .p18. | 0.5075019 | 0.5661313 | 0.07073694 | |
| 19 | visual | ~~ | visual | 0 | 1 | 1 | 16 | NA | 0 | .p19. | 0.0500000 | 0.8093160 | 0.14546241 | |
| 20 | textual | ~~ | textual | 0 | 1 | 1 | 17 | NA | 0 | .p20. | 0.0500000 | 0.9794914 | 0.11210585 | |
| 21 | speed | ~~ | speed | 0 | 1 | 1 | 18 | NA | 0 | .p21. | 0.0500000 | 0.3837476 | 0.08620919 | |
| 22 | visual | ~~ | textual | 0 | 1 | 1 | 19 | NA | 0 | .p22. | 0.0000000 | 0.4082324 | 0.07352388 | |
| 23 | visual | ~~ | speed | 0 | 1 | 1 | 20 | NA | 0 | .p23. | 0.0000000 | 0.2622246 | 0.05627640 | |
| 24 | textual | ~~ | speed | 0 | 1 | 1 | 21 | NA | 0 | .p24. | 0.0000000 | 0.1734947 | 0.04931466 |
1 | parameterEstimates(fit8) |
| lhs | op | rhs | est | se | z | pvalue | ci.lower | ci.upper |
|---|---|---|---|---|---|---|---|---|
| visual | =~ | x1 | 1.0000000 | 0.00000000 | NA | NA | 1.00000000 | 1.0000000 |
| visual | =~ | x2 | 0.5535003 | 0.09966512 | 5.553601 | 2.798440e-08 | 0.35816025 | 0.7488403 |
| visual | =~ | x3 | 0.7293702 | 0.10910970 | 6.684742 | 2.313327e-11 | 0.51551912 | 0.9432213 |
| textual | =~ | x4 | 1.0000000 | 0.00000000 | NA | NA | 1.00000000 | 1.0000000 |
| textual | =~ | x5 | 1.1130766 | 0.06542011 | 17.014288 | 0.000000e+00 | 0.98485552 | 1.2412976 |
| textual | =~ | x6 | 0.9261462 | 0.05544886 | 16.702711 | 0.000000e+00 | 0.81746848 | 1.0348240 |
| speed | =~ | x7 | 1.0000000 | 0.00000000 | NA | NA | 1.00000000 | 1.0000000 |
| speed | =~ | x8 | 1.1799508 | 0.16498657 | 7.151799 | 8.564260e-13 | 0.85658310 | 1.5033186 |
| speed | =~ | x9 | 1.0815302 | 0.15116744 | 7.154518 | 8.397727e-13 | 0.78524742 | 1.3778129 |
| x1 | ~~ | x1 | 0.5490540 | 0.11360092 | 4.833182 | 1.343676e-06 | 0.32640025 | 0.7717077 |
| x2 | ~~ | x2 | 1.1338390 | 0.10172337 | 11.146298 | 0.000000e+00 | 0.93446487 | 1.3332132 |
| x3 | ~~ | x3 | 0.8443240 | 0.09062318 | 9.316866 | 0.000000e+00 | 0.66670587 | 1.0219422 |
| x4 | ~~ | x4 | 0.3711730 | 0.04771779 | 7.778503 | 7.327472e-15 | 0.27764784 | 0.4646981 |
| x5 | ~~ | x5 | 0.4462551 | 0.05839278 | 7.642299 | 2.131628e-14 | 0.33180733 | 0.5607028 |
| x6 | ~~ | x6 | 0.3562027 | 0.04303497 | 8.277052 | 2.220446e-16 | 0.27185568 | 0.4405497 |
| x7 | ~~ | x7 | 0.7993916 | 0.08138156 | 9.822761 | 0.000000e+00 | 0.63988671 | 0.9588966 |
| x8 | ~~ | x8 | 0.4876971 | 0.07419409 | 6.573260 | 4.922529e-11 | 0.34227934 | 0.6331148 |
| x9 | ~~ | x9 | 0.5661313 | 0.07073694 | 8.003333 | 1.110223e-15 | 0.42748944 | 0.7047731 |
| visual | ~~ | visual | 0.8093160 | 0.14546241 | 5.563747 | 2.640432e-08 | 0.52421490 | 1.0944171 |
| textual | ~~ | textual | 0.9794914 | 0.11210585 | 8.737201 | 0.000000e+00 | 0.75976795 | 1.1992148 |
| speed | ~~ | speed | 0.3837476 | 0.08620919 | 4.451354 | 8.533051e-06 | 0.21478073 | 0.5527146 |
| visual | ~~ | textual | 0.4082324 | 0.07352388 | 5.552379 | 2.818082e-08 | 0.26412828 | 0.5523366 |
| visual | ~~ | speed | 0.2622246 | 0.05627640 | 4.659584 | 3.168493e-06 | 0.15192489 | 0.3725243 |
| textual | ~~ | speed | 0.1734947 | 0.04931466 | 3.518116 | 4.346227e-04 | 0.07683973 | 0.2701496 |
1 | varTable(fit8) |
| name | idx | nobs | type | exo | user | mean | var | nlev | lnam |
|---|---|---|---|---|---|---|---|---|---|
| x1 | 7 | 301 | numeric | 0 | 0 | 4.935770 | 1.362898 | 0 | |
| x2 | 8 | 301 | numeric | 0 | 0 | 6.088040 | 1.386390 | 0 | |
| x3 | 9 | 301 | numeric | 0 | 0 | 2.250415 | 1.279114 | 0 | |
| x4 | 10 | 301 | numeric | 0 | 0 | 3.060908 | 1.355167 | 0 | |
| x5 | 11 | 301 | numeric | 0 | 0 | 4.340532 | 1.665318 | 0 | |
| x6 | 12 | 301 | numeric | 0 | 0 | 2.185572 | 1.200346 | 0 | |
| x7 | 13 | 301 | numeric | 0 | 0 | 4.185902 | 1.187083 | 0 | |
| x8 | 14 | 301 | numeric | 0 | 0 | 5.527076 | 1.025389 | 0 | |
| x9 | 15 | 301 | numeric | 0 | 0 | 5.374123 | 1.018387 | 0 |
1 | subset(modindices(fit8), mi > 5) |
| lhs | op | rhs | mi | epc | sepc.lv | sepc.all | sepc.nox | |
|---|---|---|---|---|---|---|---|---|
| 26 | visual | =~ | x5 | 7.440646 | -0.20989801 | -0.18882843 | -0.1465688 | -0.1465688 |
| 28 | visual | =~ | x7 | 18.630638 | -0.42186239 | -0.37951580 | -0.3489088 | -0.3489088 |
| 30 | visual | =~ | x9 | 36.411031 | 0.57702154 | 0.51910006 | 0.5152491 | 0.5152491 |
| 31 | textual | =~ | x1 | 8.902732 | 0.35033114 | 0.34672012 | 0.2974884 | 0.2974884 |
| 33 | textual | =~ | x3 | 9.150895 | -0.27163761 | -0.26883773 | -0.2380993 | -0.2380993 |
| 48 | x1 | ~~ | x7 | 5.419590 | -0.12911266 | -0.12911266 | -0.1948864 | -0.1948864 |
| 50 | x1 | ~~ | x9 | 7.334930 | 0.13789383 | 0.13789383 | 0.2473311 | 0.2473311 |
| 51 | x2 | ~~ | x3 | 8.531827 | 0.21823928 | 0.21823928 | 0.2230502 | 0.2230502 |
| 55 | x2 | ~~ | x7 | 8.918022 | -0.18272538 | -0.18272538 | -0.1919302 | -0.1919302 |
| 59 | x3 | ~~ | x5 | 7.858085 | -0.13009466 | -0.13009466 | -0.2119402 | -0.2119402 |
| 65 | x4 | ~~ | x6 | 6.220497 | -0.23480293 | -0.23480293 | -0.6457535 | -0.6457535 |
| 66 | x4 | ~~ | x7 | 5.919707 | 0.09818085 | 0.09818085 | 0.1802432 | 0.1802432 |
| 76 | x7 | ~~ | x8 | 34.145089 | 0.53644397 | 0.53644397 | 0.8591510 | 0.8591510 |
| 77 | x7 | ~~ | x9 | 5.182955 | -0.18670689 | -0.18670689 | -0.2775377 | -0.2775377 |
| 78 | x8 | ~~ | x9 | 14.946392 | -0.42309592 | -0.42309592 | -0.8052026 | -0.8052026 |
9. 加载SEM绘图包(semPlot)
参考资料:
Structural Equation Modeling with lavaan in R
Sacha Epskamp and with contributions from Simon Stuber (2017). semPlot: Path Diagrams and Visual Analysis of Various SEM Packages’
Output. R package version 1.1. https://CRAN.R-project.org/package=semPlot
1 能画出结构方程模型图的软件有很多,比如Amos和SmartPLS,这两个软件在可视化方面做的非常好,Mplus和前两个软件有所不同,它是通过语法输入,从diagrammer生成图形,而Amos和SmartPLS是用户直接绘制图形。
2 R语言也可以绘制结构方程模型图,其优势在于用户可以对SEM图中的变量、线条、形状和颜色进行DIY。本文使用semPlot包中的semPaths函数进行模型图的绘制。
3 使用参数layout来改变图形样式,其可选值包括5种:tree;circle;spring;tree2;circle2
4 大多数人在做CFA的时候会把因子画在左侧,指标画在右侧,这样的图比较适合放在论文中。其实只需要将之前图片样式为tree状的图旋转90°即可。旋转可选的值有:1,2,3,4,分别是默认、逆时针旋转90°、180°以及270°。rotation = 2
5 给线条上色使用参数edge.color,如我们想要深绿色的图,在该参数后写入darkgreen即可。edge.color = “darkgreen”
6 还可以对潜变量或指标配色,直接用color,而非edge.color。 color = “lightblue”
7 如果出现负载荷,图中带有负载荷的线条会变成红色。我们也可以使用posCol,negCol自定义正负载荷的配色
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | semPaths(object, what = "paths", whatLabels, style, layout = "tree", intercepts = TRUE, residuals = TRUE, thresholds = TRUE, intStyle = "multi", rotation = 1, curve, curvature = 1, nCharNodes = 3, nCharEdges = 3, sizeMan = 5, sizeLat = 8, sizeInt = 2, sizeMan2, sizeLat2, sizeInt2, shapeMan, shapeLat, shapeInt = "triangle", ask, mar, title, title.color = "black", title.adj = 0.1, title.line = -1, title.cex = 0.8, include, combineGroups = FALSE, manifests, latents, groups, color, residScale, gui = FALSE, allVars = FALSE, edge.color, reorder = TRUE, structural = FALSE, ThreshAtSide = FALSE, thresholdColor, thresholdSize = 0.5, fixedStyle = 2, freeStyle = 1, as.expression = character(0), optimizeLatRes = FALSE, inheritColor = TRUE, levels, nodeLabels, edgeLabels, pastel = FALSE, rainbowStart = 0, intAtSide, springLevels = FALSE, nDigits = 2, exoVar, exoCov = TRUE, centerLevels = TRUE, panelGroups = FALSE, layoutSplit = FALSE, measurementLayout = "tree", subScale, subScale2, subRes = 4, subLinks, modelOpts = list(mplusStd = "std"), curveAdjacent = '<->', edge.label.cex = 0.6, cardinal = "none", equalizeManifests = FALSE, covAtResiduals = TRUE, bifactor, optimPoints = 1:8 * (pi/4), ...) |