Mediation
Mediation
Mediation
In statistics, a mediation model is one that seeks to identify and explain the mechanism or process that underlies an observed relationship between an independent variable and a dependent variable via the inclusion of a third hypothetical variable, known as a mediator variable (also a mediating variable, intermediary variable, or intervening variable).
Source: WIKIPEDIA
Example
Does the speed of recovery after sickness improve with the use of alternative medicine or is this effect mediated by a healthy lifestyle?
Mediaton paths
Simulate data
Create predictor variable
set.seed(1976)
## Set parameters for simulation
= 100
n = 10
mu = 2
sigma ## Predictor
= rnorm(n, mu, sigma) use.homeopathic.remedies
Mediator
Create mediator
= 2
b0 = 1.2
b1 = rnorm(n,0,.7)
error = b0 + b1*use.homeopathic.remedies + error healthy.lifestyle
Specify model
Create outcome variable
= 6
b0 = 1.2
b1 = 3
b2 = rnorm(n,0,1.4)
error = b0 + b1*use.homeopathic.remedies + b2*healthy.lifestyle + error
speed.of.healing
<- data.frame(use.homeopathic.remedies,
data
healthy.lifestyle,
speed.of.healing)<- round(data, 4) data
The data
Apply 3 models
1.out.pre <- lm(speed.of.healing ~ use.homeopathic.remedies)
m.2.med.pre <- lm(healthy.lifestyle ~ use.homeopathic.remedies)
m.3.out.pre.med <- lm(speed.of.healing ~ use.homeopathic.remedies + healthy.lifestyle) m.
Extract beta coëfficients
= m.2.med.pre$coefficients[2]
b.a = m.3.out.pre.med$coefficients[3]
b.b = m.1.out.pre$coefficients[2]
b.c = m.3.out.pre.med$coefficients[2] b.c.accent
View beta coëfficients
b.a
use.homeopathic.remedies
1.210308
b.b
healthy.lifestyle
2.859761
View beta coëfficients
b.c
use.homeopathic.remedies
4.762102
b.c.accent
use.homeopathic.remedies
1.30091
Visual
plot(data$use.homeopathic.remedies, data$speed.of.healing, col = 'red', xlab="alter", ylab="rocov")
.1 <- lm(speed.of.healing ~ use.homeopathic.remedies, data)
fit
abline(fit.1, col = 'green')
3D Visual
Interactive, give it a spin.
Calculate indirect effect
\(a \times b = b_a \times b_b\)
*b.b b.a
use.homeopathic.remedies
3.461192
- b.c.accent b.c
use.homeopathic.remedies
3.461192
Calculate indirect effect (partially standardized)
\(\frac{ab}{s_{Outcome}} = \frac{b_a b_b}{s_{Outcome}}\)
*b.b/sd(speed.of.healing) b.a
use.homeopathic.remedies
0.3833868
Calculate indirect effect (standardized)
\(\frac{ab}{s_{Outcome}} \times s_{Predictor} = \frac{b_a b_b}{s_{Outcome}} \times s_{Predictor}\)
*b.b/sd(speed.of.healing)*sd(use.homeopathic.remedies) b.a
use.homeopathic.remedies
0.6979258
Calculate \(P_M\)
\(\frac{ab}{c} = \frac{b_a b_b}{b_c}\)
*b.b/b.c b.a
use.homeopathic.remedies
0.7268202
Calculate \(R_M\)
\(\frac{ab}{c`} = \frac{b_a b_b}{b_{c`}}\)
*b.b/b.c.accent b.a
use.homeopathic.remedies
2.660593
Calculate \(R^2_M\)
\(R^2_{out,med} − (R^2_{out,pre \times med} − R^2_{out,pre})\)
4.out.med <- lm(speed.of.healing ~ healthy.lifestyle)
m.= cor(m.4.out.med$fitted.values, speed.of.healing)^2
R2_out.med = cor(m.3.out.pre.med$fitted.values, speed.of.healing)^2
R2_out.pre.med = cor(m.1.out.pre$fitted.values, speed.of.healing)^2
R2_out.pre
- (R2_out.pre.med - R2_out.pre) R2_out.med
[1] 0.9161054