q_mediation | R Documentation |
Simple-to-use functions for fitting regression models and testing indirect effects using just one function.
q_mediation(
x,
y,
m = NULL,
cov = NULL,
data = NULL,
boot_ci = TRUE,
level = 0.95,
R = 100,
seed = NULL,
boot_type = c("perc", "bc"),
model = NULL,
parallel = TRUE,
ncores = max(parallel::detectCores(logical = FALSE) - 1, 1),
progress = TRUE
)
q_simple_mediation(
x,
y,
m = NULL,
cov = NULL,
data = NULL,
boot_ci = TRUE,
level = 0.95,
R = 100,
seed = NULL,
boot_type = c("perc", "bc"),
parallel = TRUE,
ncores = max(parallel::detectCores(logical = FALSE) - 1, 1),
progress = TRUE
)
q_serial_mediation(
x,
y,
m = NULL,
cov = NULL,
data = NULL,
boot_ci = TRUE,
level = 0.95,
R = 100,
seed = NULL,
boot_type = c("perc", "bc"),
parallel = TRUE,
ncores = max(parallel::detectCores(logical = FALSE) - 1, 1),
progress = TRUE
)
q_parallel_mediation(
x,
y,
m = NULL,
cov = NULL,
data = NULL,
boot_ci = TRUE,
level = 0.95,
R = 100,
seed = NULL,
boot_type = c("perc", "bc"),
parallel = TRUE,
ncores = max(parallel::detectCores(logical = FALSE) - 1, 1),
progress = TRUE
)
## S3 method for class 'q_mediation'
print(
x,
digits = 4,
annotation = TRUE,
pvalue = TRUE,
pvalue_digits = 4,
se = TRUE,
for_each_path = FALSE,
se_ci = TRUE,
wrap_computation = TRUE,
lm_ci = TRUE,
lm_beta = TRUE,
lm_ci_level = 0.95,
...
)
x |
For |
y |
The name of the outcome. |
m |
A character vector of the
name(s) of the mediator(s). For
a simple mediation model, it must
has only one name. For serial and
parallel mediation models, it can
have one or more names. For a serial
mediation models, the direction of
the paths go from the first names to
the last names. For example,
|
cov |
The names of the covariates,
if any. If it is a character vector,
then the outcome ( |
data |
The data frame. Note that
listwise deletion will be used and
only cases with no missing data on
all variables in the model (e.g.,
|
boot_ci |
Logical. Whether
bootstrap confidence interval will be
formed. Default is |
level |
The level of confidence of the confidence interval. Default is .95 (for 95% confidence intervals). |
R |
The number of bootstrap samples. Default is 100. Should be set to 5000 or at least 10000. |
seed |
The seed for the random
number generator. Default is |
boot_type |
The type of the
bootstrap confidence intervals.
Default is |
model |
The type of model. For
|
parallel |
If |
ncores |
Integer. The number of
CPU cores to use when |
progress |
Logical. Display
bootstrapping progress or not.
Default is |
digits |
Number of digits to display. Default is 4. |
annotation |
Logical. Whether
the annotation after the table of
effects is to be printed. Default is
|
pvalue |
Logical. If |
pvalue_digits |
Number of decimal places to display for the p-values. Default is 4. |
se |
Logical. If |
for_each_path |
Logical. If
|
se_ci |
Logical. If |
wrap_computation |
Logical.
If |
lm_ci |
If |
lm_beta |
If |
lm_ci_level |
The level of confidence
of the confidence interval. Ignored
if |
... |
Other arguments. If
|
The family of "q" (quick) functions are for testing mediation effects in common models. These functions do the following in one single call:
Fit the regression models.
Compute and test all the indirect effects.
They are easy-to-use and are suitable for common models which are not too complicated. For now, there are "q" functions for these models:
A simple mediation: One predictor
(x
), one mediator (m
), one
outcome (y
), and optionally some
control variables (cov
ariates)
(q_simple_mediation()
)
A serial mediation model: One
predictor (x
), one or more
mediators (m
), one outcome (y
),
and optionally some control variables
(cov
ariates). The mediators
positioned sequentially between x
and y
(q_serial_mediation()
):
x -> m1 -> m2 -> ... -> y
A parallel mediation model: One
predictor (x
), one or more
mediators (m
), one outcome (y
),
and optionally some control variables
(cov
ariates). The mediators
positioned in parallel between x
and y
(q_parallel_mediation()
):
x -> m1 -> y
x -> m2 -> y
...
Users only need to specify the x
,
m
, and y
variables, and covariates
or control variables, if any (by cov
),
and the functions will automatically
identify all indirect effects and
total effects.
Note that they are not intended to
be flexible. For models that are
different from these common models,
it is recommended to fit the models
manually, either by structural
equation modelling (e.g.,
lavaan::sem()
) or by regression
analysis using stats::lm()
or
lmhelprs::many_lm()
. See
https://sfcheung.github.io/manymome/articles/med_lm.html
for an illustration on how to compute
and test indirect effects for an
arbitrary mediation model.
This is the workflow of the "q" functions:
Do listwise deletion based on all the variables used in the models.
Generate the regression models based on the variables specified.
Fit all the models by OLS regression
using stats::lm()
.
Call all_indirect_paths()
to
identify all indirect paths.
Call many_indirect_effects()
to
compute all indirect effects and
form their confidence intervals.
Call total_indirect_effect()
to
compute the total indirect effect.
Return all the results for printing.
The output of the "q" functions have
a print
method for
printing all the major results.
The "q" functions are designed to be
easy to use. They are not designed to
be flexible. For maximum flexibility,
fit the models manually and call
functions such as
indirect_effect()
separately. See
https://sfcheung.github.io/manymome/articles/med_lm.html
for illustrations.
We do not recommend using Monte Carlo
confidence intervals for models
fitted by regression because the
covariances between parameter
estimates are assumed to be zero,
which may not be the case in some
models. Therefore, the "q" functions
do not support Monte Carlo confidence
intervals. If Monte Carlo intervals
are desired, please fit the model by
structural equation modeling using
lavaan::sem()
.
The function q_mediation()
returns
a q_mediation
class object, with
its print
method.
The function q_simple_mediation()
returns
a q_simple_mediation
class object, which
is a subclass of q_mediation
.
The function q_serial_mediation()
returns
a q_serial_mediation
class object, which
is a subclass of q_mediation
.
The function q_parallel_mediation()
returns
a q_parallel_mediation
class object, which
is a subclass of q_mediation
.
print(q_mediation)
: The print
method of the outputs
of q_mediation()
, q_simple_mediation()
,
q_serial_mediation()
, and q_parallel_mediation()
.
q_mediation()
: The general
"q" function for common mediation
models. Not to be used directly.
q_simple_mediation()
: A wrapper of q_mediation()
for
simple mediation models (a model with only one mediator).
q_serial_mediation()
: A wrapper of q_mediation()
for
serial mediation models.
q_parallel_mediation()
: A wrapper of q_mediation()
for
parallel mediation models.
lmhelprs::many_lm()
for
fitting several regression models
using model syntax,
indirect_effect()
for computing and
testing a specific path,
all_indirect_paths()
for
identifying all paths in a model,
many_indirect_effects()
for
computing and testing indirect
effects along several paths, and
total_indirect_effect()
for
computing and testing the total
indirect effects.
# ===== Simple mediation
# Set R to 5000 or 10000 in real studies
# Remove 'parallel' or set it to TRUE for faster bootstrapping
# Remove 'progress' or set it to TRUE to see a progress bar
out <- q_simple_mediation(x = "x",
y = "y",
m = "m",
cov = c("c2", "c1"),
data = data_med,
R = 40,
seed = 1234,
parallel = FALSE,
progress = FALSE)
# Suppressed printing of p-values due to the small R
# Remove `pvalue = FALSE` when R is large
print(out,
pvalue = FALSE)
# # Different control variables for m and y
# out <- q_simple_mediation(x = "x",
# y = "y",
# m = "m",
# cov = list(m = "c1",
# y = c("c1", "c2")),
# data = data_med,
# R = 100,
# seed = 1234,
# parallel = FALSE,
# progress = FALSE)
# out
# ===== Serial mediation
# Set R to 5000 or 10000 in real studies
# Remove 'parallel' or set it to TRUE for faster bootstrapping
# Remove 'progress' or set it to TRUE to see a progress bar
# out <- q_serial_mediation(x = "x",
# y = "y",
# m = c("m1", "m2"),
# cov = c("c2", "c1"),
# data = data_serial,
# R = 40,
# seed = 1234,
# parallel = FALSE,
# progress = FALSE)
# # Suppressed printing of p-values due to the small R
# # Remove `pvalue = FALSE` when R is large
# print(out,
# pvalue = FALSE)
# # Different control variables for m and y
# out <- q_serial_mediation(x = "x",
# y = "y",
# m = c("m1", "m2"),
# cov = list(m1 = "c1",
# m2 = c("c2", "c1"),
# y = "c2"),
# data = data_serial,
# R = 100,
# seed = 1234,
# parallel = FALSE,
# progress = FALSE)
# out
# ===== Parallel mediation
# Set R to 5000 or 10000 in real studies
# Remove 'parallel' or set it to TRUE for faster bootstrapping
# Remove 'progress' or set it to TRUE to see a progress bar
# out <- q_parallel_mediation(x = "x",
# y = "y",
# m = c("m1", "m2"),
# cov = c("c2", "c1"),
# data = data_parallel,
# R = 40,
# seed = 1234,
# parallel = FALSE,
# progress = FALSE)
# # Suppressed printing of p-values due to the small R
# # Remove `pvalue = FALSE` when R is large
# print(out,
# pvalue = FALSE)
# # Different control variables for m and y
# out <- q_parallel_mediation(x = "x",
# y = "y",
# m = c("m1", "m2"),
# cov = list(m1 = "c1",
# m2 = c("c2", "c1"),
# y = "c2"),
# data = data_parallel,
# R = 100,
# seed = 1234,
# parallel = FALSE,
# progress = FALSE)
# out
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.