R - mediation package的使用

關(guān)于中介分析的介紹,可以參考這個(gè)帖子

Reference

mediation: R Package for Causal Mediation Analysis

mediation包主要用來(lái)做中介分析宗收,包的核心框架如圖所示

  • Design based主要在數(shù)據(jù)不滿足要求時(shí)使用,大部分時(shí)候使用左邊model-based接口就可以了。


    Core structure of the mediation package as of version 4.0.

具體說(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
Types of statistical models that can be used with the mediate function.

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的作用的比例

Mediation
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末揽涮,一起剝皮案震驚了整個(gè)濱河市唬格,隨后出現(xiàn)的幾起案子汰聋,更是在濱河造成了極大的恐慌,老刑警劉巖拟蜻,帶你破解...
    沈念sama閱讀 206,126評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件搔扁,死亡現(xiàn)場(chǎng)離奇詭異稿蹲,居然都是意外死亡忠聚,警方通過(guò)查閱死者的電腦和手機(jī)咒林,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)欢瞪,“玉大人重贺,你說(shuō)我怎么就攤上這事次企∏逼裕” “怎么了缸棵?”我有些...
    開(kāi)封第一講書人閱讀 152,445評(píng)論 0 341
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)谭期。 經(jīng)常有香客問(wèn)我堵第,道長(zhǎng)吧凉,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 55,185評(píng)論 1 278
  • 正文 為了忘掉前任踏志,我火速辦了婚禮阀捅,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘针余。我一直安慰自己也搓,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,178評(píng)論 5 371
  • 文/花漫 我一把揭開(kāi)白布涵紊。 她就那樣靜靜地躺著傍妒,像睡著了一般。 火紅的嫁衣襯著肌膚如雪摸柄。 梳的紋絲不亂的頭發(fā)上颤练,一...
    開(kāi)封第一講書人閱讀 48,970評(píng)論 1 284
  • 那天,我揣著相機(jī)與錄音驱负,去河邊找鬼嗦玖。 笑死,一個(gè)胖子當(dāng)著我的面吹牛跃脊,可吹牛的內(nèi)容都是我干的宇挫。 我是一名探鬼主播,決...
    沈念sama閱讀 38,276評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼酪术,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼器瘪!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起绘雁,我...
    開(kāi)封第一講書人閱讀 36,927評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤橡疼,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后庐舟,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體欣除,經(jīng)...
    沈念sama閱讀 43,400評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,883評(píng)論 2 323
  • 正文 我和宋清朗相戀三年挪略,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了历帚。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 37,997評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡杠娱,死狀恐怖挽牢,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情墨辛,我是刑警寧澤卓研,帶...
    沈念sama閱讀 33,646評(píng)論 4 322
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響奏赘,放射性物質(zhì)發(fā)生泄漏寥闪。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,213評(píng)論 3 307
  • 文/蒙蒙 一磨淌、第九天 我趴在偏房一處隱蔽的房頂上張望疲憋。 院中可真熱鬧,春花似錦梁只、人聲如沸缚柳。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 30,204評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)秋忙。三九已至,卻和暖如春构舟,著一層夾襖步出監(jiān)牢的瞬間灰追,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 31,423評(píng)論 1 260
  • 我被黑心中介騙來(lái)泰國(guó)打工狗超, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留弹澎,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,423評(píng)論 2 352
  • 正文 我出身青樓努咐,卻偏偏與公主長(zhǎng)得像苦蒿,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子渗稍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,722評(píng)論 2 345