Overview

Although mediation is extremely difficult to implement with the desired inferences, there is some potential for using experience sampling to examine within-person mediation which may provide for more sound inferences than the usual cross-sectional (between-person) mediation. In this tutorial we work through a few examples and ways of implementing the within-person model. Generally, we follow the example in Bolger and Laurenceau (2013) Chapter 9: Within-subject Mediation Analysis, but also make use of other resources.

Mediation is said to occur when the effect of one variable (x) on another (y) is transmitted through an intervening variable (m).

There are many resources on mediation, but not so many R-based examples and implementations of wihtin-person mediation. We’ve done our best to compile these together into workable examples. A good set of general resources is here (note also their caution about one section of code at the beginning) https://stats.idre.ucla.edu/r/faq/how-can-i-perform-mediation-with-multilevel-data-method-2/, and also on the sites of researchers studying mediation, e.g., http://quantpsy.org/, http://afhayes.com/index.html, http://davidakenny.net/cm/mediate.htm, https://psychology.clas.asu.edu/research/labs/research-prevention-laboratory-mackinnon.

Outline

  1. Introduction to The Within-Person Mediation Model
  2. An Example using Multivariate Multilevel Model
  3. An Example using Multi-Step Estimation and Bootstrapping
  4. Conclusion

Prelim - Loading libraries used in this script.

library(psych)
library(ggplot2)
library(reshape2)
library(nlme)
library(lme4)

#install.packages("devtools")
#library(devtools)
#install_github("marklhc/bootmlm")  m 
library(bootmlm) #to extract asymptotic covariance matrix of random effects

1. Introduction to the Within-Person Mediation Model

In basic form, mediation occurs when the effect of an independent variable (x) on a dependent variable (y) is transmitted via a mediator variable (m). This “mediation effect” is often referred to as the indirect effect of X on Y through M. Mediation models allow researchers to test simple hypotheses about “causal processes”. Mediation models often involve parsing the total effect (c) of X on Y into a direct effect (c’) and an indirect effect (a × b). These coefficients can be derived by fitting a set of simultaneous equations to sample data using linear regression or path analysis. Specifically, in the within-person world,

\[M_{jt} = a0_{j} + a_{j}X_{jt} + e_{Mjt}\] \[Y_{jt} = b0_{j} + b_{j}M_{jt} + c'_{i}X_{jt} + e_{Yjt}\]

2. An Example using Multivariate Multilevel Model

We follow the example from Bolger & Laurenceau (2013) Chapter 9.

Preliminaries -

Read in the data

#Daily data
#set filepath for data file
filepath <- "https://quantdev.ssri.psu.edu/sites/qdev/files/B&Lmediation.csv"
#read in the .csv file using the url() function
data <- read.csv(file=url(filepath),header=TRUE)

# Examine first few rows of the data set
head(data,10)
##     id time timec  freldis   fwkdis fwkstr fwkstrc    fwkdisc    freldisc
## 1  101    1   -10 3.034483 5.590119      3    0.03  0.3601189 -1.60551724
## 2  101    2    -9 4.620690 5.535224      3    0.03  0.3052242 -0.01931034
## 3  101    3    -8 2.850575 3.888381      3    0.03 -1.3416194 -1.78942529
## 4  101    4    -7 6.398467 5.352242      4    1.03  0.1222415  1.75846743
## 5  101    5    -6 2.544061 4.483074      1   -1.97 -0.7469259 -2.09593870
## 6  101    6    -5 5.164751 3.339433      2   -0.97 -1.8905672  0.52475096
## 7  101    7    -4 2.704981 4.135407      3    0.03 -1.0945929 -1.93501916
## 8  101    8    -3 5.003831 5.800549      4    1.03  0.5705489  0.36383142
## 9  101    9    -2 4.099617 5.434584      3    0.03  0.2045837 -0.54038314
## 10 101   10    -1 5.471264 4.830741      2   -0.97 -0.3992589  0.83126437
##      fwkstrcb   fwkdiscb  freldiscb   fwkstrcw   fwkdiscw  freldiscw
## 1  -0.3033333 -0.6227591 -0.1634446  0.3333333  0.9828781 -1.4420726
## 2  -0.3033333 -0.6227591 -0.1634446  0.3333333  0.9279833  0.1441343
## 3  -0.3033333 -0.6227591 -0.1634446  0.3333333 -0.7188603 -1.6259807
## 4  -0.3033333 -0.6227591 -0.1634446  1.3333333  0.7450007  1.9219121
## 5  -0.3033333 -0.6227591 -0.1634446 -1.6666667 -0.1241668 -1.9324941
## 6  -0.3033333 -0.6227591 -0.1634446 -0.6666667 -1.2678081  0.6881956
## 7  -0.3033333 -0.6227591 -0.1634446  0.3333333 -0.4718337 -1.7715745
## 8  -0.3033333 -0.6227591 -0.1634446  1.3333333  1.1933081  0.5272760
## 9  -0.3033333 -0.6227591 -0.1634446  0.3333333  0.8273428 -0.3769385
## 10 -0.3033333 -0.6227591 -0.1634446 -0.6666667  0.2235002  0.9947090
##             x          m          y
## 1   0.3333333  0.9828781 -1.4420726
## 2   0.3333333  0.9279833  0.1441343
## 3   0.3333333 -0.7188603 -1.6259807
## 4   1.3333333  0.7450007  1.9219121
## 5  -1.6666667 -0.1241668 -1.9324941
## 6  -0.6666667 -1.2678081  0.6881956
## 7   0.3333333 -0.4718337 -1.7715745
## 8   1.3333333  1.1933081  0.5272760
## 9   0.3333333  0.8273428 -0.3769385
## 10 -0.6666667  0.2235002  0.9947090

The variables of interest in this example are fwkstrcw (the predictor: female work stress centered within),fwkdiscw (the mediator: female work dissatisfaction centered within),freldiscw (the outcome: female relationship dissatisfaction centered within). Note that these variables are state variables. They have already been person-mean centered. We are not currently interested in between-person differences, so they have been separated and set aside. (It may take some steps to get your data to this form).

Copies of the three variables of interest have been made and conveniently labeled as x,m, and y for easy conceptualization and operationalization of the mediation model.

Descriptives of Within-person Variables

Lets look at the descriptives.

#variables of interest
vars <- c("fwkstrcw","fwkdiscw","freldiscw","x","m","y")
#descriptives
describe(data[,vars])
##           vars    n mean   sd median trimmed  mad   min  max range skew
## fwkstrcw     1 2100    0 1.00   0.00       0 1.20 -2.90 2.95  5.86 0.05
## fwkdiscw     2 2100    0 1.13  -0.01       0 1.15 -3.81 3.81  7.63 0.02
## freldiscw    3 2100    0 0.99   0.00       0 0.98 -3.52 3.22  6.74 0.02
## x            4 2100    0 1.00   0.00       0 1.20 -2.90 2.95  5.86 0.05
## m            5 2100    0 1.13  -0.01       0 1.15 -3.81 3.81  7.63 0.02
## y            6 2100    0 0.99   0.00       0 0.98 -3.52 3.22  6.74 0.02
##           kurtosis   se
## fwkstrcw     -0.25 0.02
## fwkdiscw     -0.11 0.02
## freldiscw     0.07 0.02
## x            -0.25 0.02
## m            -0.11 0.02
## y             0.07 0.02

As expected, the variables all have mean = 0, as they have been person-centered. Standard deviations near one are not purposeful, just coincidence.

Plots of Within-person Associations

We plot each leg of the model for a few individuals.

x –> y fwkstrcw –> freldiscw

#within-person regressions, x --> y
ggplot(data=data[which(data$id <= 106),], aes(x=fwkstrcw, y=freldiscw, group=id)) +
  geom_point(color="black") + 
  geom_smooth(method=lm, se=TRUE, fullrange=TRUE, color="black") + 
  xlab("x = Work Stress (within)") +
  ylab("y = Relationship Dissatisfaction (within)") +
  facet_wrap( ~ id)

x –> m fwkstrcw –> fwkdiscw

#within-person regressions, x --> m
ggplot(data=data[which(data$id <= 106),], aes(x=fwkstrcw, y=fwkdiscw, group=id)) +
  geom_point(color="blue") + 
  geom_smooth(method=lm, se=TRUE, fullrange=TRUE, color="blue") + 
  xlab("x = Work Stress (within)") +
  ylab("m = Work Dissatisfaction (within)") +
  facet_wrap( ~ id)