如何在R中使用mediation包?
关于中介分析的介绍,可以参考这个帖子
Reference
mediation: R Package for Causal Mediation Analysis
mediation包主要用来做中介分析,包的核心框架如图所示
-
Design based主要在数据不满足要求时使用,大部分时候使用左边model-based接口就可以了。
具体说明可以读参考文献,我就不翻译了,下面说一下怎么用
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
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的作用的比例
推荐阅读
-
微信 "扫一扫 "物联网,全面揭秘 "扫一扫 "背后的扫盲技术!-1.1 扫一扫感知物体是做什么的? 1.1 微信扫一扫是做什么的? 扫一扫识物是指以图片或视频(商品图片:鞋/包/美妆/服饰/家电/玩具/图书/食品/珠宝/家具/其他商品)为输入媒介,挖掘微信内容生态中的有价值信息(电商+百科+资讯,如图1所示),并展示给用户。这里的电商基本涵盖了微信小程序覆盖上亿SKU的全量优质电商,可以支持用户货比N家并直接下单购买,百科和资讯则聚合了微信内的头部自媒体如搜狗、搜搜、百度等,向用户展示和分享拍摄商品相关的内容资讯。 图 1 扫一扫识别功能示意图 欢迎大家更新iOS新版微信→扫一扫→识货,亲自体验,也欢迎大家通过识货界面的反馈按钮向我们提交反馈意见。 扫一扫识物实景图展示 1.2 扫一扫识物有哪些使用场景? 扫一扫识物的目的是为用户访问微信内部生态内容开辟一个新窗口,以用户扫图片为输入形式,为用户提供微信生态内容中的百科、资讯、电商等作为展示页面。除了用户熟悉的扫一扫操作外,我们还将进一步拓展长按操作,让用户更方便地进行扫一扫操作。"扫一扫知事 "的落地场景主要涵盖三大部分: a. 科普知识: a.科普知识。用户通过扫一扫,可以在微信生态圈中获取该对象的百科、资讯等常识或趣闻,帮助用户更好地了解该对象; b.购物场景。同样的搜索功能支持用户看到喜欢的商品立即检索到微信小程序电商中的同款商品,支持用户即扫即购; c.广告场景。扫一扫识别物体可以辅助公众号文章、视频更好地理解其中蕴含的图片信息,从而更好地投放匹配广告,提高点击率。 1.3 Sweep Sense 为 Sweep 家族带来了哪些新技术? 对于扫一扫来说,大家耳熟能详的应该就是扫一扫二维码、扫一扫小程序码、扫一扫条形码、扫一扫翻译了。无论是各种形式的编码还是文字字符,都可以看作是图片的一种特定编码形式,而物的识别则是对自然场景图片的识别,这对于扫一扫家族来说是一个质的飞跃,我们希望从物的识别入手,进一步拓展扫一扫对自然场景图片的理解能力,比如扫酒、扫车、扫植物、扫人脸等服务,如下图3所示。 图 3 Sweep 家族
-
[推荐阅读 - 优化中的 R 语言]使用 Rglpk 软件包解决线性规划和整数规划问题
-
R 在优化中的应用] 使用 goalprog 软件包解决线性目标程序设计问题
-
R 语言使用 dietaryindex 软件包计算 NHANES 数据中的多个健康饮食指数(HEI 等人) (1)
-
如何在R语言BIEN包中获取特定种类的观测数据:BIEN_occurrence_species操作指南
-
如何在R中轻松使用ggtree来操作和展示进化树的基本技巧
-
如何在R中使用mediation包?
-
南邮OJ Web任务大揭秘:层层挑战剖析 1. 挑战一:迷宫般的目录探索 题目作者似乎穷举了所有可能的目录组合,最终在404.php中的