關(guān)于中介分析的介紹,可以參考這個(gè)帖子
Reference
mediation: R Package for Causal Mediation Analysis
mediation包主要用來(lái)做中介分析宗收,包的核心框架如圖所示
-
Design based主要在數(shù)據(jù)不滿足要求時(shí)使用,大部分時(shí)候使用左邊model-based接口就可以了。
具體說(shuō)明可以讀參考文獻(xiàn)俏脊,我就不翻譯了,下面說(shuō)一下怎么用
Model-based causal mediation analysis
mediation包的核心是mediate函數(shù)漫萄,詳細(xì)參數(shù)說(shuō)明可以用?mediate
查看
Usage
mediate(model.m, model.y, sims = 1000, boot = FALSE,
boot.ci.type = "perc", treat = "treat.name", mediator = "med.name",
covariates = NULL, outcome = NULL, control = NULL,
conf.level = 0.95, control.value = 0, treat.value = 1,
long = TRUE, dropobs = FALSE, robustSE = FALSE, cluster = NULL,
group.out = NULL, use_speed = FALSE, ...)
參數(shù)簡(jiǎn)單說(shuō)明如下
Arguments
model.m
a fitted model object for mediator. Can be of class 'lm', 'polr', 'bayespolr', 'glm', 'bayesglm', 'gam', 'rq', 'survreg', or 'merMod'.
model.y
a fitted model object for outcome. Can be of class 'lm', 'polr', 'bayespolr', 'glm', 'bayesglm', 'gam', 'vglm', 'rq', 'survreg', or 'merMod'.
sims
number of Monte Carlo draws for nonparametric bootstrap or quasi-Bayesian approximation.
boot
a logical value. if 'FALSE' a quasi-Bayesian approximation is used for confidence intervals; if 'TRUE' nonparametric bootstrap will be used. Default is 'FALSE'.
boot.ci.type
a character string indicating the type of bootstrap confidence intervals. If "bca" and boot = TRUE, bias-corrected and accelerated (BCa) confidence intervals will be estimated. If "perc" and boot = TRUE, percentile confidence intervals will be estimated. Default is "perc".
treat
a character string indicating the name of the treatment variable used in the models. The treatment can be either binary (integer or a two-valued factor) or continuous (numeric).
mediator
a character string indicating the name of the mediator variable used in the models.
covariates
a list or data frame containing values for a subset of the pre-treatment covariates in 'model.m' and 'model.y'. If provided, the function will return the estimates conditional on those covariate values.
outcome
a character string indicating the name of the outcome variable in ‘model.y’. Only necessary if 'model.y' is of class 'survreg'; otherwise ignored.
control
a character string indicating the name of the control group indicator. Only relevant if 'model.y' is of class 'gam'. If provided, 'd0', 'z0' and 'n0' are allowed to differ from 'd1', 'z1' and 'n1', respectively.
conf.level
level of the returned two-sided confidence intervals. Default is to return the 2.5 and 97.5 percentiles of the simulated quantities.
control.value
value of the treatment variable used as the control condition. Default is 0.
treat.value
value of the treatment variable used as the treatment condition. Default is 1.
long
a logical value. If 'TRUE', the output will contain the entire sets of simulation draws of the the average causal mediation effects, direct effects, proportions mediated, and total effect. Default is 'TRUE'.
dropobs
a logical value indicating the behavior when the model frames of 'model.m' and 'model.y' (and the 'cluster' variable if included) are composed of different observations. If 'TRUE', models will be re-fitted using common data rows. If 'FALSE', error is returned. Default is 'FALSE'.
robustSE
a logical value. If 'TRUE', heteroskedasticity-consistent stan
假設(shè)自變量X和因變量Y之間有中介變量M窿撬,mediate 函數(shù)需要兩個(gè)統(tǒng)計(jì)模型
(X可以是一系列變量)
* Y對(duì)X的模型(model.m, outcome model, ```out.fit```)
* M對(duì)X的模型(model.y, mediator model, ```med.fit```)dard errors will be used in quasi-Bayesian simulations. Ignored if 'boot' is 'TRUE' or neither 'model.m' nor 'model.y' has a method for vcovHC in the sandwich package. Default is 'FALSE'.
cluster
a variable indicating clusters for standard errors. Note that this should be a vector of cluster indicators itself, not a character string for the name of the variable.
group.out
a character string indicating the name of the lmer/glmer group on which the mediate output is based. Can be used even when a merMod function is applied to only one of the mediator or the outcome. If merMod functions are applied to both the mediator and the outcome, default is the group name used in the outcome model; if the mediator group and the outcome group are different and the user is interested in the mediate output based on the mediator group, then set group.out to the group name used in the mediator merMod model. If a merMod function is applied to only one of the mediator or the outcome, group.out is automatically set to the group name used in the merMod model.
use_speed
a logical value indicating whether, if nonparametric bootstrap is used, lm and glm models should be re-fit using functions from the speedglm package. Ignored if 'boot' is 'FALSE' or if neither 'model.m' nor 'model.y' is of class 'lm' or 'glm'. Default is 'FALSE'.
...
other arguments passed to vcovHC in the sandwich package: typically the 'type' argument, which is ignored if 'robustSE' is 'FALSE'. Arguments to the boot in the boot package may also be passed, e.g. 'parallel' and 'ncpus'.
假設(shè)自變量X和因變量Y之間有中介變量M跛璧,mediate 函數(shù)需要兩個(gè)統(tǒng)計(jì)模型
(X可以是一系列變量)
- Y對(duì)X的模型(model.m, outcome model,
out.fit
) - M對(duì)X的模型(model.y, mediator model,
med.fit
)
下圖是可以放在mediation函數(shù)里的模型刹碾,*表示使用mediate函數(shù)的時(shí)候需要bootstrap非參,即設(shè)置boot = TRUE
Bootstrap方法是目前用的比較多的一種中介分析的實(shí)現(xiàn)方法景用,在參數(shù)里設(shè)置boot = T, sims = N
可以調(diào)用采樣數(shù)為N的bootstrap割粮。
比如
mod.xm = lm(m ~ x + cov1 + cov2)
mod.xy = lm(y ~ m + x + cov1 + cov2)
mod.med = mediate(mod.xm, mod.xy, treat = 'x', mediator = 'm', sims = 10000, boot = T)
med.sum = summary(mod.med)
結(jié)果說(shuō)明
ACME stands for average causal mediation effects.間接因果效應(yīng)耗美,表示X通過(guò)M對(duì)Y的效應(yīng)大小
通過(guò)med.sum$d0和med.sum$d0.p可以獲得ACME的效應(yīng)和p值
ADE stands for average direct effects.直接效應(yīng),表示X直接對(duì)Y的作用大小
通過(guò)med.sum$z0和med.sum$z0.p可以獲得ADE的效應(yīng)和p值
Total Effect stands for the total effect (direct + indirect) of the IV on the DV. X對(duì)Y的直接和間接作用總和
Prop. Mediated describes the proportion of the effect of the IV on the DV that goes through the mediator. X通過(guò)M對(duì)Y的作用的比例