資料與管理

#讀檔案,這是 CSV文字檔,可以用 EXCEL 開啟
dta <- read.csv("BinaryM.csv", header = TRUE)

中介效果

一個獨變項,一個中介變項

#Baron & Kenny(1986) 的四步驟,檢驗被霸凌是體指數到憂鬱的中介
m1 <- lm(Y ~ X, data=dta)
m2 <- glm(M ~ X, data=dta) 
m3 <- lm(Y ~ M, data=dta) 
m4 <- lm(Y ~ X+M, data=dta)
options(huxtable.knitr_output_format="md")
jtools::export_summs(m1,m2,m3,m4,
                     model.names = c("Y", "M","Y","Y"),
                     error_format = "[{conf.low},{conf.high}]")
## Registered S3 methods overwritten by 'broom':
##   method            from  
##   tidy.glht         jtools
##   tidy.summary.glht jtools
## Warning in to_md.huxtable(structure(list(names = c("", "(Intercept)", "", :
## Markdown cannot handle cells with colspan/rowspan > 1
## Warning in to_md.huxtable(structure(list(names = c("", "(Intercept)", "", :
## Can't vary column alignment in markdown; using first row
Y M Y Y
(Intercept) -0.02 0.46 *** -0.12 *** -0.12 ***
[-0.07,0.03] [0.43,0.49] [-0.19,-0.06] [-0.18,-0.05]
X 0.06 * 0.13 *** 0.03
[0.01,0.10] [0.10,0.16] [-0.02,0.08]
M 0.22 *** 0.21 ***
[0.13,0.32] [0.11,0.31]
N 1000 1000 1000 1000
R2 0.01 0.02 0.02
AIC 2329.21 1375.81 2313.73 2314.49
BIC 2343.94 1390.53 2328.45 2334.12
Pseudo R2 0.09
** * p < 0.001; ** p < 0.01; * p < 0.05.
## 以 SEM 中的徑路分析方式,分析中介
model1 <- '
  Y ~ c*X+b*M
  M ~ a*X
  indirect := a*b  
  total := c + (a*b)
  proportion := indirect/total
'
#徑路分析報表
fit <- lavaan::sem(model1, data=dta,ordered=c('M'))
summary(fit)
## lavaan 0.6.15 ended normally after 9 iterations
## 
##   Estimator                                       DWLS
##   Optimization method                           NLMINB
##   Number of model parameters                         6
## 
##   Number of observations                          1000
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                 0.000       0.000
##   Degrees of freedom                                 0           0
## 
## Parameter Estimates:
## 
##   Standard errors                           Robust.sem
##   Information                                 Expected
##   Information saturated (h1) model        Unstructured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   Y ~                                                 
##     X          (c)    0.010    0.027    0.386    0.699
##     M          (b)    0.129    0.032    4.035    0.000
##   M ~                                                 
##     X          (a)    0.352    0.042    8.477    0.000
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .Y                -0.022    0.025   -0.901    0.368
##    .M                 0.000                           
## 
## Thresholds:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     M|t1              0.117    0.041    2.887    0.004
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .Y                 0.581    0.027   21.410    0.000
##    .M                 1.000                           
## 
## Scales y*:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     M                 1.000                           
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     indirect          0.045    0.012    3.644    0.000
##     total             0.056    0.024    2.278    0.023
##     proportion        0.813    0.412    1.975    0.048
#畫圖看模型與估計值
lavaanPlot::lavaanPlot(model = fit,
                       edge_options = list(color = "grey"), 
                       coefs = TRUE,
                       stand = TRUE)