forked from duty-machine/duty-machine
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
15 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
title: "中介变量或者因变量是因子变量的中介分析" | ||
date: 2024-11-04T00:47:46Z | ||
draft: ["false"] | ||
tags: [ | ||
"fetched", | ||
"R 学习践行者" | ||
] | ||
categories: ["Acdemic"] | ||
--- | ||
中介变量或者因变量是因子变量的中介分析 by R 学习践行者 | ||
------ | ||
<div><p> 在中介变量或因变量为类别变量的中介分析中,需要使用 Logistic 回归取代通常的线性回归;此时因为a、b、c’系数存在尺度上的问题所以需要进行系数转换,使得回归系数变成具有相同的尺度。</p><p> 下面使用示例展示这种类型的中介分析过程,示例中自变量是多分类变量,中介变量是连续变量,因变量是二分类变量。</p><pre><code><span>library</span>(mediation)<br>data(jobs)</code></pre><p> 我们仍然depress1作为自变量,econ_hard作为中介变量,depress2作为因变量;同时把depress1和depress2转换成因子变量:</p><pre><code>hist(jobs$depress2)</code></pre><p><img data-imgfileid="100003138" data-ratio="0.7138888888888889" data-type="png" data-w="1080" width="672" data-src="https://mmbiz.qpic.cn/sz_mmbiz_png/IGjxEGEQ50neUDgPojaCEgXzJdVGF8HaYHcI4CHC4ZSfOORhKpArjYnNTUTIuxLkVQbjP7T6JAibna40vEMt5uQ/640?wx_fmt=png&from=appmsg" src="https://mmbiz.qpic.cn/sz_mmbiz_png/IGjxEGEQ50neUDgPojaCEgXzJdVGF8HaYHcI4CHC4ZSfOORhKpArjYnNTUTIuxLkVQbjP7T6JAibna40vEMt5uQ/640?wx_fmt=png&from=appmsg"></p><pre><code>jobs$depress1<-ifelse(<br> jobs$depress1<<span>1.5</span>,<span>"d1"</span>,<br> ifelse(jobs$depress1>=<span>1.5</span> & jobs$depress1<<span>2</span>,<span>"d2"</span>,<br> ifelse(jobs$depress1>=<span>2</span> & jobs$depress1<<span>2.5</span>,<br> <span>"d3"</span>,<span>"d4"</span>)))<br><br>jobs$depress1<-factor(jobs$depress1,levels = c(<span>"d1"</span>,<span>"d2"</span>,<span>"d3"</span>,<span>"d4"</span>))<br><br>jobs$depress2<-ifelse(jobs$depress2><span>2</span>,<span>1</span>,<span>0</span>)<br><br>table(jobs$depress2)</code></pre><pre><code> <br> 0 1 <br> 655 244</code></pre><pre><code><span># 转换多类别变量 X 为虚拟变量</span><br>X_dummy <- model.matrix(~ depress1 - <span>1</span>, data = jobs) <span># 创建虚拟变量矩阵</span><br>jobs<- cbind(jobs, X_dummy) <span># 合并虚拟变量至原数据集中</span></code></pre><p>1.拟合模型</p><p>第一个模型:</p><pre><code>model.0 <- glm(depress2 ~ depress1d2+depress1d3+depress1d4, <br> data=jobs,<br> family = binomial(link = <span>"logit"</span>))<br>summary(model.0)</code></pre><pre><code> <br> Call:<br> glm(formula = depress2 ~ depress1d2 + depress1d3 + depress1d4, <br> family = binomial(link = "logit"), data = jobs)<br> <br> Coefficients:<br> Estimate Std. Error z value Pr(>|z|) <br> (Intercept) -2.0534 0.1822 -11.272 < 2e-16 ***<br> depress1d2 0.7300 0.2531 2.884 0.00393 ** <br> depress1d3 1.4504 0.2258 6.424 1.32e-10 ***<br> depress1d4 2.1163 0.2416 8.760 < 2e-16 ***<br> ---<br> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1<br> <br> (Dispersion parameter for binomial family taken to be 1)<br> <br> Null deviance: 1051.2 on 898 degrees of freedom<br> Residual deviance: 952.3 on 895 degrees of freedom<br> AIC: 960.3<br> <br> Number of Fisher Scoring iterations: 4</code></pre><p>第二个模型:</p><pre><code>model.1 <- lm(econ_hard~ depress1d2 + depress1d3 + depress1d4,<br> data=jobs)<br>summary(model.1)</code></pre><pre><code> <br> Call:<br> lm(formula = econ_hard ~ depress1d2 + depress1d3 + depress1d4, <br> data = jobs)<br> <br> Residuals:<br> Min 1Q Median 3Q Max <br> -2.35006 -0.66874 -0.00874 0.66126 2.38124 <br> <br> Coefficients:<br> Estimate Std. Error t value Pr(>|t|) <br> (Intercept) 2.61876 0.05410 48.410 < 2e-16 ***<br> depress1d2 0.36431 0.08610 4.231 2.56e-05 ***<br> depress1d3 0.71998 0.08052 8.942 < 2e-16 ***<br> depress1d4 0.73130 0.09181 7.965 4.99e-15 ***<br> ---<br> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1<br> <br> Residual standard error: 0.9354 on 895 degrees of freedom<br> Multiple R-squared: 0.1038, Adjusted R-squared: 0.1008 <br> F-statistic: 34.55 on 3 and 895 DF, p-value: < 2.2e-16</code></pre><p>第三个模型:</p><pre><code>model.2 <- glm(depress2~ econ_hard+depress1d2 + <br> depress1d3 + depress1d4, <br> data=jobs,<br> family = binomial(link = <span>"logit"</span>))<br>summary(model.2)</code></pre><pre><code> <br> Call:<br> glm(formula = depress2 ~ econ_hard + depress1d2 + depress1d3 + <br> depress1d4, family = binomial(link = "logit"), data = jobs)<br> <br> Coefficients:<br> Estimate Std. Error z value Pr(>|z|) <br> (Intercept) -2.63074 0.29810 -8.825 < 2e-16 ***<br> econ_hard 0.21455 0.08527 2.516 0.0119 * <br> depress1d2 0.65628 0.25527 2.571 0.0101 * <br> depress1d3 1.30567 0.23268 5.612 2.01e-08 ***<br> depress1d4 1.97533 0.24750 7.981 1.45e-15 ***<br> ---<br> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1<br> <br> (Dispersion parameter for binomial family taken to be 1)<br> <br> Null deviance: 1051.22 on 898 degrees of freedom<br> Residual deviance: 945.92 on 894 degrees of freedom<br> AIC: 955.92<br> <br> Number of Fisher Scoring iterations: 4</code></pre><p> 根据输出的结果我们可以绘制出一下这张图:</p><p><img data-imgfileid="100003137" data-ratio="0.8818407960199005" data-type="png" data-w="804" data-src="https://mmbiz.qpic.cn/sz_mmbiz_png/IGjxEGEQ50neUDgPojaCEgXzJdVGF8HaZKwtC73KRYnkdjQBV2LeDTajSgg5fODYcjNp5ib7Xm9jmRicJJJO0jfQ/640?wx_fmt=png&from=appmsg" src="https://mmbiz.qpic.cn/sz_mmbiz_png/IGjxEGEQ50neUDgPojaCEgXzJdVGF8HaZKwtC73KRYnkdjQBV2LeDTajSgg5fODYcjNp5ib7Xm9jmRicJJJO0jfQ/640?wx_fmt=png&from=appmsg"> 我们可以看到未进行系数转换,总效应与直接效应+间接效应之和差异较大。</p><p><br></p><p>2.回归系数标准</p><p><img data-imgfileid="100003140" data-ratio="0.8078902229845626" data-type="png" data-w="583" data-src="https://mmbiz.qpic.cn/sz_mmbiz_png/IGjxEGEQ50neUDgPojaCEgXzJdVGF8HaTxoS8IIeNnTwN87pOXcMVHmNLibfm7yubr9tENf994DRWSJ1Vqg4VJA/640?wx_fmt=png&from=appmsg" src="https://mmbiz.qpic.cn/sz_mmbiz_png/IGjxEGEQ50neUDgPojaCEgXzJdVGF8HaTxoS8IIeNnTwN87pOXcMVHmNLibfm7yubr9tENf994DRWSJ1Vqg4VJA/640?wx_fmt=png&from=appmsg"></p><pre><code><span>#对自变量b2水平的b、c'、c系数标准化</span><br>b2_var<-var(jobs$depress1d2)<br>b2<-<span>0.73</span>^<span>2</span>*b2_var+pi^<span>2</span>/<span>3</span><br>SD1_b2<-sqrt(b2)<br><br>m_var<-var(jobs$econ_hard)<br>b2_m_cor<-cov(jobs$depress1d2,jobs$econ_hard)<br><br>b2_1<-<span>0.65628</span>^<span>2</span>*b2_var+<span>0.21455</span>^<span>2</span>*m_var+<span>2</span>*<span>0.65628</span>*<span>0.21455</span>*b2_m_cor+pi^<span>2</span>/<span>3</span><br><br>SD2_b2<-sqrt(b2_1)<br><br><span>#对b、c'、c系数标准化</span><br>b2_b<-<span>0.21455</span>/SD2_b2<br>b2_c1<-<span>0.65628</span>/SD2_b2<br>b2_c<-<span>0.73</span>/SD1_b2<br><br>b2_b;b2_c1;b2_c</code></pre><pre><code> [1] 0.116264</code></pre><pre><code> [1] 0.3556361</code></pre><pre><code> [1] 0.3970391</code></pre><pre><code><span>#对自变量b3水平的b、c'、c系数标准化</span><br>b3_var<-var(jobs$depress1d3)<br>b3<-<span>1.4504</span>^<span>2</span>*b3_var+pi^<span>2</span>/<span>3</span><br>SD1_b3<-sqrt(b3)<br><br>m_var<-var(jobs$econ_hard)<br>b3_m_cor<-cov(jobs$depress1d3,jobs$econ_hard)<br><br>b3_1<-<span>1.30567</span>^<span>2</span>*b3_var+<span>0.21455</span>^<span>2</span>*m_var+<span>2</span>*<span>1.30567</span>*<span>0.21455</span>*b3_m_cor+pi^<span>2</span>/<span>3</span><br><br>SD2_b3<-sqrt(b3_1)<br><br><span>#对b、c'、c系数标准化</span><br>b3_b<-<span>0.21455</span>/SD2_b3<br>b3_c1<-<span>1.30567</span>/SD2_b3<br>b3_c<-<span>1.4504</span>/SD1_b3<br><br>b3_b;b3_c1;b3_c</code></pre><pre><code> [1] 0.1112067</code></pre><pre><code> [1] 0.6767617</code></pre><pre><code> [1] 0.7531668</code></pre><pre><code><span>#对自变量b4水平的b、c'、c系数标准化</span><br>b4_var<-var(jobs$depress1d4)<br>b4<-<span>2.1163</span>^<span>2</span>*b4_var+pi^<span>2</span>/<span>3</span><br>SD1_b4<-sqrt(b4)<br><br>m_var<-var(jobs$econ_hard)<br>b4_m_cor<-cov(jobs$depress1d4,jobs$econ_hard)<br><br>b4_1<-<span>1.97533</span>^<span>2</span>*b4_var+<span>0.21455</span>^<span>2</span>*m_var+<span>2</span>*<span>1.97533</span>*<span>0.21455</span>*b4_m_cor+pi^<span>2</span>/<span>3</span><br><br>SD2_b4<-sqrt(b4_1)<br><br><span>#对b、c'、c系数标准化</span><br>b4_b<-<span>0.21455</span>/SD2_b4<br>b4_c1<-<span>1.97533</span>/SD2_b4<br>b4_c<-<span>2.1163</span>/SD1_b4<br><br>b4_b;b4_c1;b4_c</code></pre><pre><code> [1] 0.107921</code></pre><pre><code> [1] 0.9936122</code></pre><pre><code> [1] 1.065822</code></pre><p> 经过系数标准化后,虽然总效应不完全等于直接效应+间接效应,但是两者差异已经非常小了,如下图所示。因此我们可以采用整个流程并使用自助法计算直接效应(标准化的c’)、间接效应(a*b(标准化的))、总效应(标准化的c)的置信区间。</p><p><img data-imgfileid="100003139" data-ratio="0.801948051948052" data-type="png" data-w="924" data-src="https://mmbiz.qpic.cn/sz_mmbiz_png/IGjxEGEQ50neUDgPojaCEgXzJdVGF8HayEr5PdhITEx0j7ujG19xtZgvtDAxEOTyYyRhicFgelRFy0UQfIcAu5A/640?wx_fmt=png&from=appmsg" src="https://mmbiz.qpic.cn/sz_mmbiz_png/IGjxEGEQ50neUDgPojaCEgXzJdVGF8HayEr5PdhITEx0j7ujG19xtZgvtDAxEOTyYyRhicFgelRFy0UQfIcAu5A/640?wx_fmt=png&from=appmsg"></p><p><br></p><p>3.使用RMediation包检验Za*Zb</p><p><mp-pay-preview-filter data-offset="40"></mp-pay-preview-filter></p></div> | ||
<hr> | ||
<a href="https://mp.weixin.qq.com/s/jHg1lo_AuFG9WyENLj8x-Q",target="_blank" rel="noopener noreferrer">原文链接</a> |