載入必要的套件
library(pacman)
## Warning: 套件 'pacman' 是用 R 版本 4.3.1 來建造的
p_load(lavaan,semTools)
讀檔
dta <- read.csv('LatentMed.csv',header=T)
head(dta)
## X01 X02 X03 X04 M01 M02 M03
## 1 10.578595 11.521146 11.876476 13.205155 11.089552 10.232617 8.679013
## 2 8.764749 7.877980 8.431885 8.578961 6.754060 6.943212 5.730063
## 3 9.498978 7.109612 11.612876 13.944336 9.978468 9.940967 11.726377
## 4 7.371567 7.515754 8.904867 11.072044 11.182636 9.103500 12.410240
## 5 10.494179 12.618862 9.074768 11.123216 11.607614 10.660417 6.699480
## 6 7.579023 9.659427 7.589318 7.900881 8.751867 9.994859 8.101259
## M04 M05 M06 M07 M08 Y01 Y02
## 1 10.019351 9.510973 10.132180 12.861896 12.630673 10.776108 13.205223
## 2 7.012082 6.769046 10.230968 8.268063 9.787301 10.325640 9.259789
## 3 8.404845 8.157372 9.923232 10.269432 12.303265 13.542720 13.383072
## 4 10.695016 11.220277 11.145610 11.360847 7.558937 15.137296 14.417776
## 5 11.212047 8.970076 10.311769 11.101241 10.879921 17.930082 15.449201
## 6 8.090666 7.767457 7.968739 9.391322 9.427371 9.358574 10.007686
## Y03 Y04
## 1 12.432496 11.936793
## 2 9.919234 9.598946
## 3 14.494831 11.729645
## 4 12.838387 10.738926
## 5 17.882568 15.450493
## 6 10.139331 13.641535
模型寫法
- 利用 :=
定義中介效果,左方是效果的稱呼(自己命名),右邊就是要利用係數名稱乘出來了
- 執行對標準誤的拔靴法,bootstrap 表示拔靴次數
model<-'
Y=~Y01+Y02+Y03+Y04
X=~X01+X02+X03+X04
M1=~M01+M02+M03+M04
M2=~M05+M06+M07+M08
M1~a1*X
M2~a2*X
Y~c*X+b1*M1+b2*M2
med_XM1Y := a1*b1
med_XM2Y := a2*b2
tot_XY := a1*b1+a2*b2+c
'
result <- sem(model, data=dta,std.ov=T, std.lv=T,se = "bootstrap", bootstrap=5000)
summary(result, fit.measure=T)
## lavaan 0.6.15 ended normally after 26 iterations
##
## Estimator ML
## Optimization method NLMINB
## Number of model parameters 37
##
## Number of observations 300
##
## Model Test User Model:
##
## Test statistic 102.317
## Degrees of freedom 99
## P-value (Chi-square) 0.390
##
## Model Test Baseline Model:
##
## Test statistic 1800.725
## Degrees of freedom 120
## P-value 0.000
##
## User Model versus Baseline Model:
##
## Comparative Fit Index (CFI) 0.998
## Tucker-Lewis Index (TLI) 0.998
##
## Loglikelihood and Information Criteria:
##
## Loglikelihood user model (H0) -5953.688
## Loglikelihood unrestricted model (H1) -5902.529
##
## Akaike (AIC) 11981.375
## Bayesian (BIC) 12118.415
## Sample-size adjusted Bayesian (SABIC) 12001.073
##
## Root Mean Square Error of Approximation:
##
## RMSEA 0.011
## 90 Percent confidence interval - lower 0.000
## 90 Percent confidence interval - upper 0.033
## P-value H_0: RMSEA <= 0.050 1.000
## P-value H_0: RMSEA >= 0.080 0.000
##
## Standardized Root Mean Square Residual:
##
## SRMR 0.039
##
## Parameter Estimates:
##
## Standard errors Bootstrap
## Number of requested bootstrap draws 5000
## Number of successful bootstrap draws 5000
##
## Latent Variables:
## Estimate Std.Err z-value P(>|z|)
## Y =~
## Y01 0.515 0.042 12.299 0.000
## Y02 0.527 0.041 12.771 0.000
## Y03 0.509 0.044 11.562 0.000
## Y04 0.530 0.040 13.096 0.000
## X =~
## X01 0.771 0.055 13.981 0.000
## X02 0.738 0.055 13.414 0.000
## X03 0.768 0.058 13.168 0.000
## X04 0.750 0.058 12.941 0.000
## M1 =~
## M01 0.553 0.058 9.582 0.000
## M02 0.504 0.051 9.863 0.000
## M03 0.468 0.055 8.475 0.000
## M04 0.526 0.056 9.406 0.000
## M2 =~
## M05 0.523 0.051 10.284 0.000
## M06 0.541 0.051 10.634 0.000
## M07 0.581 0.052 11.145 0.000
## M08 0.518 0.046 11.214 0.000
##
## Regressions:
## Estimate Std.Err z-value P(>|z|)
## M1 ~
## X (a1) 0.718 0.109 6.595 0.000
## M2 ~
## X (a2) 0.733 0.103 7.110 0.000
## Y ~
## X (c) 0.141 0.144 0.978 0.328
## M1 (b1) 0.315 0.108 2.927 0.003
## M2 (b2) 0.584 0.105 5.537 0.000
##
## Variances:
## Estimate Std.Err z-value P(>|z|)
## .Y01 0.454 0.050 9.115 0.000
## .Y02 0.428 0.042 10.290 0.000
## .Y03 0.466 0.052 9.030 0.000
## .Y04 0.422 0.044 9.640 0.000
## .X01 0.402 0.049 8.169 0.000
## .X02 0.452 0.051 8.829 0.000
## .X03 0.407 0.046 8.872 0.000
## .X04 0.434 0.042 10.353 0.000
## .M01 0.533 0.062 8.602 0.000
## .M02 0.612 0.062 9.796 0.000
## .M03 0.665 0.062 10.783 0.000
## .M04 0.578 0.057 10.083 0.000
## .M05 0.577 0.052 11.013 0.000
## .M06 0.547 0.059 9.249 0.000
## .M07 0.477 0.052 9.160 0.000
## .M08 0.584 0.058 10.001 0.000
## .Y 1.000
## X 1.000
## .M1 1.000
## .M2 1.000
##
## Defined Parameters:
## Estimate Std.Err z-value P(>|z|)
## med_XM1Y 0.226 0.083 2.729 0.006
## med_XM2Y 0.428 0.095 4.515 0.000
## tot_XY 0.794 0.098 8.118 0.000
輸出
- 拔靴法結果,估計值,信賴區間下限、信賴區間上限。
- level 設定信賴區間對應機率。
rsttmp <- parameterestimates(result, boot.ci.type = "bca.simple", level = 0.95)
rsttmp[rsttmp$op==':=',c(4,5,9,10)]
## label est ci.lower ci.upper
## 42 med_XM1Y 0.226 0.081 0.407
## 43 med_XM2Y 0.428 0.278 0.655
## 44 tot_XY 0.794 0.603 0.986