Code for this demo adapted from Nick Michalak. Also see https://nickmichalak.com/post/2019-02-14-testing-conditional-indirect-effects-mediation-in-r/testing-conditional-indirect-effects-mediation-in-r/
The dataset for this example contains standardized test scores on reading, writing, math, science, and social studies. There are also binary indicators (e.g., hisci
) that encode whether someone received a high score on a given test. We also want to compute centered versions of each variable to aid interpretation in moderation-related models.
Following Hayes, I will use the convention \(X\) for independent variable/predictor, \(M\) for mediator, \(W\) for moderator, and \(Y\) for outcome.
df <- read_csv("mediation_data.csv")
## Parsed with column specification:
## cols(
## id = col_double(),
## female = col_double(),
## ses = col_double(),
## prog = col_double(),
## read = col_double(),
## write = col_double(),
## math = col_double(),
## science = col_double(),
## socst = col_double(),
## honors = col_double(),
## awards = col_double(),
## cid = col_double(),
## hiread = col_double(),
## hiwrite = col_double(),
## hisci = col_double(),
## himath = col_double()
## )
df <- df %>% mutate_at(vars(read, write, math, science, socst), funs(c=scale))
## Warning: funs() is soft deprecated as of dplyr 0.8.0
## please use list() instead
##
## # Before:
## funs(name = f(.)
##
## # After:
## list(name = ~f(.))
## This warning is displayed once per session.
Hayes Process model 1
Does the relationship between performance on reading (\(Y\)) and social studies (\(X\)) tests depend on math ability (\(W\))?