欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

如何在R中使用mediation包?

最编程 2024-02-15 13:05:14
...

关于中介分析的介绍,可以参考这个帖子

Reference

mediation: R Package for Causal Mediation Analysis

mediation包主要用来做中介分析,包的核心框架如图所示

  • Design based主要在数据不满足要求时使用,大部分时候使用左边model-based接口就可以了。


    Core structure of the mediation package as of version 4.0.

具体说明可以读参考文献,我就不翻译了,下面说一下怎么用

Model-based causal mediation analysis

mediation包的核心是mediate函数,详细参数说明可以用?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, ...)

参数简单说明如下

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
假设自变量X和因变量Y之间有中介变量M,mediate 函数需要两个统计模型
(X可以是一系列变量)
* Y对X的模型(model.m, outcome model, ```out.fit```)
* M对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'.

假设自变量X和因变量Y之间有中介变量M,mediate 函数需要两个统计模型
(X可以是一系列变量)

  • Y对X的模型(model.m, outcome model, out.fit)
  • M对X的模型(model.y, mediator model, med.fit)
    下图是可以放在mediation函数里的模型,*表示使用mediate函数的时候需要bootstrap非参,即设置boot = TRUE
Types of statistical models that can be used with the mediate function.

Bootstrap方法是目前用的比较多的一种中介分析的实现方法,在参数里设置boot = T, sims = N可以调用采样数为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)

结果说明
ACME stands for average causal mediation effects.间接因果效应,表示X通过M对Y的效应大小
通过med.sum$d0和med.sum$d0.p可以获得ACME的效应和p值

ADE stands for average direct effects.直接效应,表示X直接对Y的作用大小
通过med.sum$z0和med.sum$z0.p可以获得ADE的效应和p值

Total Effect stands for the total effect (direct + indirect) of the IV on the DV. X对Y的直接和间接作用总和

Prop. Mediated describes the proportion of the effect of the IV on the DV that goes through the mediator. X通过M对Y的作用的比例

Mediation

推荐阅读