9 中介效果分析

本單元介紹如何以R語言做中介效果分析。

9.1 讀入資料並畫圖檢視

以JASP提供的mediation.csv為例,以R做分析。

此資料為一項假設的研究資料。在研究中以量表詢問了父母對孩子的養育採取自由放任態度的程度(laissez_faire)、孩子的獨立程度(independence)、以及孩子的探索行為量(exploration)。研究假設為:強烈的自由放任態度對孩子的獨立性有正向的效果。其可能的機制為,自由放任的態度會影響孩子的探索行為量,進而影響其獨立性。意即,探索是態度對獨立性影響的中介變項。

以read.table()獨入資料。

mData <- read.table("mediation.csv", header=TRUE, sep=",")
laissez_faire exploration independence disobedience
7 4 8 2
6 2 6 3
12 3 7 4
14 4 7 5
17 6 11 3
19 6 10 5

9.2 以R內建的lm函數來做分析

9.2.1 總效果檢驗

以laissez_faire預測independence,模型解釋力顯著。

fit <- lm(independence~laissez_faire, data=mData)
summary(fit)
## 
## Call:
## lm(formula = independence ~ laissez_faire, data = mData)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.6202 -1.5732 -0.0493  1.4424  6.5051 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    5.21426    0.61960   8.415 3.27e-13 ***
## laissez_faire  0.23433    0.05151   4.549 1.54e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.363 on 98 degrees of freedom
## Multiple R-squared:  0.1744, Adjusted R-squared:  0.1659 
## F-statistic:  20.7 on 1 and 98 DF,  p-value: 1.544e-05

9.2.2 以預測項來預測中介變項

以laissez_faire預測exploration,效果亦顯著。

fita <- lm(exploration ~ laissez_faire, data=mData)
summary(fita)
## 
## Call:
## lm(formula = exploration ~ laissez_faire, data = mData)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.12610 -0.76207 -0.08926  0.60196  2.56513 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    2.18840    0.27134   8.065 1.84e-12 ***
## laissez_faire  0.17281    0.02256   7.661 1.33e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.035 on 98 degrees of freedom
## Multiple R-squared:  0.3746, Adjusted R-squared:  0.3682 
## F-statistic: 58.69 on 1 and 98 DF,  p-value: 1.333e-11

9.2.3 中介變項與預測項共同預測依變項

fitb <- lm(independence ~ exploration + laissez_faire, data=mData)
summary(fitb)
## 
## Call:
## lm(formula = independence ~ exploration + laissez_faire, data = mData)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.1713 -0.7227 -0.0016  0.8303  2.8300 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    0.84015    0.38843   2.163 0.033003 *  
## exploration    1.99877    0.11211  17.829  < 2e-16 ***
## laissez_faire -0.11107    0.03165  -3.509 0.000684 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.148 on 97 degrees of freedom
## Multiple R-squared:  0.807,  Adjusted R-squared:  0.803 
## F-statistic: 202.7 on 2 and 97 DF,  p-value: < 2.2e-16

9.2.4 中介效果檢驗

以stargazer套件的stargazer()函數來看三變項之間的關係。

library(stargazer)
stargazer(fit, fita, fitb, type = "text", title = "Baron and Kenny Method")
## 
## Baron and Kenny Method
## =========================================================================================
##                                              Dependent variable:                         
##                     ---------------------------------------------------------------------
##                          independence           exploration            independence      
##                              (1)                    (2)                     (3)          
## -----------------------------------------------------------------------------------------
## exploration                                                              1.999***        
##                                                                           (0.112)        
##                                                                                          
## laissez_faire              0.234***               0.173***               -0.111***       
##                            (0.052)                (0.023)                 (0.032)        
##                                                                                          
## Constant                   5.214***               2.188***                0.840**        
##                            (0.620)                (0.271)                 (0.388)        
##                                                                                          
## -----------------------------------------------------------------------------------------
## Observations                 100                    100                     100          
## R2                          0.174                  0.375                   0.807         
## Adjusted R2                 0.166                  0.368                   0.803         
## Residual Std. Error    2.363 (df = 98)        1.035 (df = 98)         1.148 (df = 97)    
## F Statistic         20.696*** (df = 1; 98) 58.688*** (df = 1; 98) 202.746*** (df = 2; 97)
## =========================================================================================
## Note:                                                         *p<0.1; **p<0.05; ***p<0.01

以multilevel套件的sobel()函數來做Sobel test檢驗中介效果,看indirect effect是否顯著。

library(multilevel)
sobel(mData$laissez_faire, mData$exploration, mData$independence)
## $`Mod1: Y~X`
##              Estimate Std. Error  t value     Pr(>|t|)
## (Intercept) 5.2142586 0.61960212 8.415495 3.267909e-13
## pred        0.2343293 0.05150847 4.549335 1.543558e-05
## 
## $`Mod2: Y~X+M`
##               Estimate Std. Error   t value     Pr(>|t|)
## (Intercept)  0.8401457 0.38842542  2.162952 3.300257e-02
## pred        -0.1110699 0.03165459 -3.508809 6.841546e-04
## med          1.9987717 0.11210741 17.829078 2.255937e-32
## 
## $`Mod3: M~X`
##              Estimate Std. Error  t value     Pr(>|t|)
## (Intercept) 2.1884004  0.2713435 8.065056 1.842647e-12
## pred        0.1728057  0.0225572 7.660779 1.333137e-11
## 
## $Indirect.Effect
## [1] 0.3453992
## 
## $SE
## [1] 0.04907255
## 
## $z.value
## [1] 7.038542
## 
## $N
## [1] 100

以bda套件的mediation.test()函數來做Sobel mediation test,變項的順序依序為mv、iv、dv。

library(bda)
mediation.test(mData$exploration, mData$laissez_faire, mData$independence)
##                Sobel       Aroian      Goodman
## z.value 7.038542e+00 7.029215e+00 7.047906e+00
## p.value 1.942620e-12 2.076993e-12 1.816300e-12

9.3 以psych套件來分析

用psych套件的mediate()函數來做中介效果的分析。

library(psych)
m1 <- mediate(independence ~ laissez_faire + (exploration), data=mData, n.iter = 10000) 
## Call: mediate(y = independence ~ laissez_faire + (exploration), data = mData, 
##     n.iter = 10000)
## 
## Direct effect estimates (traditional regression)    (c') X + M on Y 
##               independence   se     t df     Prob
## Intercept             0.84 0.39  2.16 97 3.30e-02
## laissez_faire        -0.11 0.03 -3.51 97 6.84e-04
## exploration           2.00 0.11 17.83 97 2.26e-32
## 
## R = 0.9 R2 = 0.81   F = 202.75 on 2 and 97 DF   p-value:  2.26e-35 
## 
##  Total effect estimates (c) (X on Y) 
##               independence   se    t df     Prob
## Intercept             5.21 0.62 8.42 98 3.27e-13
## laissez_faire         0.23 0.05 4.55 98 1.54e-05
## 
##  'a'  effect estimates (X on M) 
##               exploration   se    t df     Prob
## Intercept            2.19 0.27 8.07 98 1.84e-12
## laissez_faire        0.17 0.02 7.66 98 1.33e-11
## 
##  'b'  effect estimates (M on Y controlling for X) 
##             independence   se     t df     Prob
## exploration            2 0.11 17.83 97 2.26e-32
## 
##  'ab'  effect estimates (through all  mediators)
##               independence boot   sd lower upper
## laissez_faire         0.35 0.35 0.05  0.26  0.44

畫圖呈現。

9.4 以mediation套件來做分析

以mediation套件來做中介效果的分析。

library(mediation)
fitM <- lm(exploration ~ laissez_faire, data=mData)
fitY <- lm(independence ~ laissez_faire + exploration, data=mData)
summary(fitM)
## 
## Call:
## lm(formula = exploration ~ laissez_faire, data = mData)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.12610 -0.76207 -0.08926  0.60196  2.56513 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    2.18840    0.27134   8.065 1.84e-12 ***
## laissez_faire  0.17281    0.02256   7.661 1.33e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.035 on 98 degrees of freedom
## Multiple R-squared:  0.3746, Adjusted R-squared:  0.3682 
## F-statistic: 58.69 on 1 and 98 DF,  p-value: 1.333e-11
summary(fitY)
## 
## Call:
## lm(formula = independence ~ laissez_faire + exploration, data = mData)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.1713 -0.7227 -0.0016  0.8303  2.8300 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    0.84015    0.38843   2.163 0.033003 *  
## laissez_faire -0.11107    0.03165  -3.509 0.000684 ***
## exploration    1.99877    0.11211  17.829  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.148 on 97 degrees of freedom
## Multiple R-squared:  0.807,  Adjusted R-squared:  0.803 
## F-statistic: 202.7 on 2 and 97 DF,  p-value: < 2.2e-16

中介分析的結果如下表。其中,ACME為Average Causal Mediation Effect (indirect effects),ADE為Average Direct Effect,Prop. Mediated為ACME/Total Effect的比率。由結果可知,laissez_faire可預測independence (b = 0.233, p < .001),而exploration在此關係中具有中介效果(b = 0.344, p < .001), laissez_faire有直接效果(b = -0.111, p < .001)。

fitMed <- mediate(fitM, fitY, treat="laissez_faire", mediator="exploration")
summary(fitMed)
## 
## Causal Mediation Analysis 
## 
## Quasi-Bayesian Confidence Intervals
## 
##                Estimate 95% CI Lower 95% CI Upper p-value    
## ACME              0.346        0.256         0.44  <2e-16 ***
## ADE              -0.111       -0.172        -0.05  <2e-16 ***
## Total Effect      0.235        0.130         0.33  <2e-16 ***
## Prop. Mediated    1.480        1.163         2.10  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Sample Size Used: 100 
## 
## 
## Simulations: 1000

效果圖示。

plot(fitMed)

9.4.1 Bootstrapping法

若為小樣本、資料違反常態,可用Bootstrapping法。

fitMedBoot <- mediate(fitM, fitY, boot=TRUE, sims=999, treat="laissez_faire", mediator="exploration")
summary(fitMedBoot)
## 
## Causal Mediation Analysis 
## 
## Nonparametric Bootstrap Confidence Intervals with the Percentile Method
## 
##                Estimate 95% CI Lower 95% CI Upper p-value    
## ACME              0.345        0.262         0.44  <2e-16 ***
## ADE              -0.111       -0.172        -0.05  <2e-16 ***
## Total Effect      0.234        0.150         0.33  <2e-16 ***
## Prop. Mediated    1.474        1.202         1.95  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Sample Size Used: 100 
## 
## 
## Simulations: 999
plot(fitMedBoot)