| q_mediation | R Documentation |
Simple-to-use functions for fitting linear models by regression or structural equation modeling and testing indirect effects, using just one function.
q_mediation(
x,
y,
m = NULL,
cov = NULL,
data = NULL,
boot_ci = TRUE,
mc_ci = FALSE,
level = 0.95,
R = 100,
seed = NULL,
ci_type = NULL,
boot_type = c("perc", "bc"),
model = NULL,
fit_method = c("lm", "regression", "sem", "lavaan"),
missing = "fiml",
fixed.x = TRUE,
sem_args = list(),
na.action = "na.pass",
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,
mc_ci = FALSE,
level = 0.95,
R = 100,
seed = NULL,
ci_type = NULL,
boot_type = c("perc", "bc"),
fit_method = c("lm", "regression", "sem", "lavaan"),
missing = "fiml",
fixed.x = TRUE,
sem_args = list(),
na.action = "na.pass",
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,
mc_ci = FALSE,
level = 0.95,
R = 100,
seed = NULL,
ci_type = NULL,
boot_type = c("perc", "bc"),
fit_method = c("lm", "regression", "sem", "lavaan"),
missing = "fiml",
fixed.x = TRUE,
sem_args = list(),
na.action = "na.pass",
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,
mc_ci = FALSE,
level = 0.95,
R = 100,
seed = NULL,
ci_type = NULL,
boot_type = c("perc", "bc"),
fit_method = c("lm", "regression", "sem", "lavaan"),
missing = "fiml",
fixed.x = TRUE,
sem_args = list(),
na.action = "na.pass",
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,
sem_style = c("lm", "lavaan"),
...
)
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 |
mc_ci |
Logical. Whether
Monte Carlo 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 |
ci_type |
The type of
confidence intervals to be formed.
Can be either |
boot_type |
The type of the
bootstrap confidence intervals.
Default is |
model |
The type of model. For
|
fit_method |
How the model is
to be fitted. If set to |
missing |
If |
fixed.x |
If |
sem_args |
If |
na.action |
How missing data is
handled. Used only when |
parallel |
If |
ncores |
Integer. The number of
CPU cores to use when |
progress |
Logical. Display progress or not. |
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 |
sem_style |
How the for the
model is to be printed if the model
is fitted by structural equation
modeling (using |
... |
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 linear models.
Compute and test all the indirect effects.
They are easy-to-use and are suitable for common models with mediators. 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 (covariates)
(q_simple_mediation())
A serial mediation model: One
predictor (x), one or more
mediators (m), one outcome (y),
and optionally some control variables
(covariates). 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
(covariates). The mediators
positioned in parallel between x
and y (q_parallel_mediation()):
x -> m1 -> y
x -> m2 -> y
...
An arbitrary mediation model: One
predictor (x), one or more
mediators (m), one outcome (y),
and optionally some control variables
(covariates). The mediators
positioned in an arbitrary form
between x
and y, as long as there are no
feedback loops (q_mediation()).
For example:
x -> m1
m1 -> m21 -> y
m1 -> m22 -> 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 more complex 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.
If a custom model is to be estimated,
instead of setting model to a name
of the form ("simple","serial",
or "parallel"), set model to the paths between xandy'. It can
take one of the following two forms:
A character vector, each element a
string of a path, with variable names
connected by "->" (the spaces are
optional):
c("x -> m11 -> m12 -> y",
"x -> m2 -> y")
A list of character vectors, each vector is a vector of names representing a path, going from the first element to the last element:
list(c("x", "m11, "m12", "y"),
c("x", "m2", "y")
The two forms above specify the same model.
Paths not included are fixed to zero (i.e., does not "exist" in the model). A path can be specified more than once if this can enhance readability. For example:
c("x1 -> m1 -> m21 -> y1",
"x1 -> m1 -> m22 -> y1")
The path "x1 -> m1" appears twice,
to indicate two different pathways from
x1 to y1.
The coefficients of the model can be
estimated by one of these two
methods: OLS (ordinary least squares)
regression (setting fit_method to
"regression" or "lm"), or path
analysis (SEM, structural equation
modeling, by setting fit_method to
"sem" or "lavaan").
This is the workflow of the "q" functions when estimating the coefficients by regression:
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.
This is the workflow of the "q" functions when estimating the coefficients by path analysis (SEM):
By default, cases with missing
data only on the mediators and the
outcome variable will be retained,
and full information maximum
likelihood (FIML) will be used to
estimate the coefficients.
(Controlled by missing, default
to "fiml") using lavaan::sem().
Generate the SEM (lavaan) model
syntax based on the model specified.
Fit the model by path analysis
using lavaan::sem().
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.
Two methods are available for testing
the indirect effects: nonparametric
bootstrap confidence intervals
(ci_type set to "boot") and
Monte Carlo confidence intervals
(ci_type set to "mc").
If the coefficients are estimated by OLS regression, only nonparametric bootstrap confidence intervals are supported.
If the coefficients are estimated by path analysis (SEM), then both methods are supported.
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 OLS regression is used.
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.
Idea to fit a model by structural equation modeling by Rong wei Sun https://orcid.org/0000-0003-0034-1422, implemented by Shu Fai Cheung https://orcid.org/0000-0002-9871-9448.
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.
# ===== A user-specified mediation model
# 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_mediation(x = "x1",
y = "y1",
model = c("x1 -> m11 -> m2 -> y1",
"x1 -> m12 -> m2 -> y1"),
cov = c("c2", "c1"),
data = data_med_complicated,
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)
# ===== 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 = 20,
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.