Description Usage Arguments Details Value References See Also Examples
View source: R/bruceR_stats_05_advance.R
To perform mediation, moderation, and conditional process (moderated mediation) analyses,
people may use software like
Mplus,
SPSS "PROCESS" macro,
and SPSS "MLmed" macro.
Some R packages can also perform such analyses separately and in a complex way, including
R package "mediation",
R package "interactions",
and R package "lavaan".
Some other R packages or scripts/modules have been further developed to improve the convenience, including
jamovi module "jAMM" (by Marcello Gallucci, based on the lavaan
package),
R package "processR" (by KeonWoong Moon, not official, also based on the lavaan
package),
and R script file "process.R"
(the official PROCESS R code by Andrew F. Hayes, but it is not yet an R package and has some bugs and limitations).
Here, the bruceR::PROCESS()
function provides
an alternative to performing mediation/moderation analyses in R.
This function supports a total of 24 kinds of SPSS PROCESS models (Hayes, 2018)
and also supports multilevel mediation/moderation analyses.
Overall, it supports the most frequently used types of mediation, moderation,
moderated moderation (3way interaction), and moderated mediation (conditional indirect effect) analyses
for (generalized) linear or linear mixed models.
Specifically, the bruceR::PROCESS()
function
first builds regression models according to the data, variable names, and a few other parameters
that users input (with no need to specify the PROCESS model number and no need to manually meancenter the variables).
The function can automatically judge the model number/type and also automatically conduct meancentering before model building.
Then, it uses:
the interactions::sim_slopes()
function to
estimate simple slopes (and conditional direct effects) in moderation, moderated moderation, and moderated mediation models
(PROCESS Models 1, 2, 3, 5, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 58, 59, 72, 73, 75, 76).
the mediation::mediate()
function to
estimate (conditional) indirect effects in (moderated) mediation models
(PROCESS Models 4, 5, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 58, 59, 72, 73, 75, 76).
the lavaan::sem()
function to perform serial multiple mediation analysis (PROCESS Model 6).
If you use this function in your research and report its results in your paper, please cite not only bruceR
but also
the other R packages it uses internally (mediation
, interactions
, and/or lavaan
).
Two parts of results are printed:
(1) regression model summary (using bruceR::model_summary()
to summarize the models)
and (2) mediation/moderation effect estimates (using one or a combination of the above packages and functions to estimate the effects).
To organize the Part 2 output, the results of Simple Slopes are titled in green,
whereas the results of Indirect Path are titled in blue.
Disclaimer:
Although this function is named after PROCESS
, Andrew F. Hayes has no role in its design, and
its development is independent from the official SPSS PROCESS macro and "process.R" script.
Any error or limitation should be attributed to the three R packages/functions that bruceR::PROCESS()
uses internally.
Moreover, as mediation analyses include random processes (i.e., bootstrap resampling or Monte Carlo simulation),
the results of mediation analyses are unlikely to be exactly the same across different software
(even if you set the same random seed in different software).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25  PROCESS(
data,
y = "",
x = "",
meds = c(),
mods = c(),
covs = c(),
clusters = c(),
hlm.re.m = "",
hlm.re.y = "",
hlm.type = c("111", "211", "221"),
med.type = c("parallel", "serial"),
mod.type = c("2way", "3way"),
mod.path = c("xy", "xm", "my", "all"),
cov.path = c("y", "m", "both"),
mod1.val = NULL,
mod2.val = NULL,
ci = c("boot", "bc.boot", "bca.boot", "mcmc"),
nsim = 100,
seed = NULL,
std = FALSE,
digits = 3,
nsmall = digits,
file = NULL
)

data 
Data frame. 
y, x 
Variable name of outcome (Y) and predictor (X). It supports both continuous (numeric) and dichotomous (factor) variables. 
meds 
Variable name(s) of mediator(s) (M).
Use It supports both continuous (numeric) and dichotomous (factor) variables. It allows an infinite number of mediators in parallel or 2~4 mediators in serial. * Order matters when 
mods 
Variable name(s) of 0~2 moderator(s) (W).
Use It supports all types of variables: continuous (numeric), dichotomous (factor), and multicategorical (factor). * Order matters when ** Do not set this parameter when 
covs 
Variable name(s) of covariate(s) (i.e., control variables).
Use 
clusters 
HLM (multilevel) level2 cluster(s):
e.g., 
hlm.re.m, hlm.re.y 
HLM (multilevel) random effect term of M model and Y model.
By default, it converts 
hlm.type 
HLM (multilevel) mediation type (levels of "XMY"):

med.type 
Type of mediator:

mod.type 
Type of moderator:

mod.path 
Which path(s) do the moderator(s) influence?

cov.path 
Which path(s) do the control variable(s) influence?

mod1.val, mod2.val 
By default ( 
ci 
Method for estimating the standard error (SE) and
95% confidence interval (CI) of indirect effect(s).
Default is
* Note that these methods never apply to the estimates of simple slopes. You should not report the 95% CIs of simple slopes as Bootstrap or Monte Carlo CIs, because they are just standard CIs without any resampling method. 
nsim 
Number of simulation samples (bootstrap resampling or Monte Carlo simulation)
for estimating SE and 95% CI. Default is 
seed 
Random seed for obtaining reproducible results.
Default is * Note that all mediation models include random processes
(i.e., bootstrap resampling or Monte Carlo simulation).
To get exactly the same results between runs, you have to set a random seed.
However, even if you set the same seed number, it is unlikely to
get exactly the same results across different R packages
(e.g., 
std 
Standardized coefficients? Default is 
digits, nsmall 
Number of decimal places of output. Default is 
file 
File name of MS Word ( 
For more details and illustrations, see PROCESSbruceRSPSS (PDF and Markdown files).
Invisibly return a list of results:
process.id
PROCESS model number.
process.type
PROCESS model type.
model.m
"Mediator" (M) models (a list of multiple models).
model.y
"Outcome" (Y) model.
results
Effect estimates and other results (unnamed list object).
Hayes, A. F. (2018). Introduction to mediation, moderation, and conditional process analysis (second edition): A regressionbased approach. Guilford Press.
Yzerbyt, V., Muller, D., Batailler, C., & Judd, C. M. (2018). New recommendations for testing indirect effects in mediational models: The need to report and test component paths. Journal of Personality and Social Psychology, 115(6), 929943. doi: 10.1037/pspa0000132
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93  #### NOTE ####
## In the following examples, I set nsim=100 to save time.
## In formal analyses, nsim=1000 (or larger) is suggested!
#### Demo Data ####
# ?mediation::student
data=mediation::student %>%
dplyr::select(SCH_ID, free, smorale, pared, income,
gender, work, attachment, fight, late, score)
names(data)[2:3]=c("SCH_free", "SCH_morale")
names(data)[4:7]=c("parent_edu", "family_inc", "gender", "partjob")
data$gender01=1data$gender # 0 = female, 1 = male
# dichotomous X: as.factor()
data$gender=factor(data$gender01, levels=0:1, labels=c("Female", "Male"))
# dichotomous Y: as.factor()
data$pass=as.factor(ifelse(data$score>=50, 1, 0))
#### Descriptive Statistics and Correlation Analyses ####
Freq(data$gender)
Freq(data$pass)
Describe(data) # file="xxx.doc"
Corr(data[,4:11]) # file="xxx.doc"
#### PROCESS Analyses ####
## Model 1 ##
PROCESS(data, y="score", x="late", mods="gender") # continuous Y
PROCESS(data, y="pass", x="late", mods="gender") # dichotomous Y
# (multilevel moderation)
PROCESS(data, y="score", x="late", mods="gender", # continuous Y (LMM)
clusters="SCH_ID")
PROCESS(data, y="pass", x="late", mods="gender", # dichotomous Y (GLMM)
clusters="SCH_ID")
# (JohnsonNeyman (JN) interval and plot)
PROCESS(data, y="score", x="gender", mods="late")>P
P$results[[1]]$jn[[1]] # JohnsonNeyman interval
P$results[[1]]$jn[[1]]$plot # JohnsonNeyman plot (ggplot object)
GLM_summary(P$model.y) # detailed results of regression
# (allows multicategorical moderator)
d=airquality
d$Month=as.factor(d$Month) # moderator: factor with levels "5"~"9"
PROCESS(d, y="Temp", x="Solar.R", mods="Month")
## Model 2 ##
PROCESS(data, y="score", x="late",
mods=c("gender", "family_inc"),
mod.type="2way") # or omit "mod.type", default is "2way"
## Model 3 ##
PROCESS(data, y="score", x="late",
mods=c("gender", "family_inc"),
mod.type="3way")
PROCESS(data, y="pass", x="gender",
mods=c("late", "family_inc"),
mod1.val=c(1, 3, 5), # moderator 1: late
mod2.val=seq(1, 15, 2), # moderator 2: family_inc
mod.type="3way")
## Model 4 ##
PROCESS(data, y="score", x="parent_edu",
meds="family_inc", covs="gender",
ci="boot", nsim=100, seed=1)
# (allows an infinite number of multiple mediators in parallel)
PROCESS(data, y="score", x="parent_edu",
meds=c("family_inc", "late"),
covs=c("gender", "partjob"),
ci="boot", nsim=100, seed=1)
# (multilevel mediation)
PROCESS(data, y="score", x="SCH_free",
meds="late", clusters="SCH_ID",
ci="mcmc", nsim=100, seed=1)
## Model 6 ##
PROCESS(data, y="score", x="parent_edu",
meds=c("family_inc", "late"),
covs=c("gender", "partjob"),
med.type="serial",
ci="boot", nsim=100, seed=1)
## Model 8 ##
PROCESS(data, y="score", x="fight",
meds="late",
mods="gender",
mod.path=c("xm", "xy"),
ci="boot", nsim=100, seed=1)
## For more examples and details, see the "note" subfolder at:
## https://github.com/psychbruce/bruceR

Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.