使用持续时间数据的调解分析

此笔记本演示了当调解变量和结果变量是持续时间变量时,使用比例风险回归建模的调解分析。这些示例基于模拟数据。

[1]:
import pandas as pd
import numpy as np
import statsmodels.api as sm
from statsmodels.stats.mediation import Mediation

使笔记本可复制。

[2]:
np.random.seed(3424)

指定样本量。

[3]:
n = 1000

生成一个暴露变量。

[4]:
exp = np.random.normal(size=n)

生成一个调解变量。

[5]:
def gen_mediator():
    mn = np.exp(exp)
    mtime0 = -mn * np.log(np.random.uniform(size=n))
    ctime = -2 * mn * np.log(np.random.uniform(size=n))
    mstatus = (ctime >= mtime0).astype(int)
    mtime = np.where(mtime0 <= ctime, mtime0, ctime)
    return mtime0, mtime, mstatus

生成一个结果变量。

[6]:
def gen_outcome(otype, mtime0):
    if otype == "full":
        lp = 0.5 * mtime0
    elif otype == "no":
        lp = exp
    else:
        lp = exp + mtime0
    mn = np.exp(-lp)
    ytime0 = -mn * np.log(np.random.uniform(size=n))
    ctime = -2 * mn * np.log(np.random.uniform(size=n))
    ystatus = (ctime >= ytime0).astype(int)
    ytime = np.where(ytime0 <= ctime, ytime0, ctime)
    return ytime, ystatus

构建包含所有相关变量的数据框。

[7]:
def build_df(ytime, ystatus, mtime0, mtime, mstatus):
    df = pd.DataFrame(
        {
            "ytime": ytime,
            "ystatus": ystatus,
            "mtime": mtime,
            "mstatus": mstatus,
            "exp": exp,
        }
    )
    return df

在特定的调解总体结构下运行完整的模拟和分析。

[8]:
def run(otype):

    mtime0, mtime, mstatus = gen_mediator()
    ytime, ystatus = gen_outcome(otype, mtime0)
    df = build_df(ytime, ystatus, mtime0, mtime, mstatus)

    outcome_model = sm.PHReg.from_formula(
        "ytime ~ exp + mtime", status="ystatus", data=df
    )
    mediator_model = sm.PHReg.from_formula("mtime ~ exp", status="mstatus", data=df)

    med = Mediation(
        outcome_model,
        mediator_model,
        "exp",
        "mtime",
        outcome_predict_kwargs={"pred_only": True},
    )
    med_result = med.fit(n_rep=20)
    print(med_result.summary())

运行具有完全调解的示例

[9]:
run("full")
                          Estimate  Lower CI bound  Upper CI bound  P-value
ACME (control)            0.742427        0.643339        0.862745      0.0
ACME (treated)            0.742427        0.643339        0.862745      0.0
ADE (control)             0.073017       -0.016189        0.155321      0.1
ADE (treated)             0.073017       -0.016189        0.155321      0.1
Total effect              0.815444        0.675214        0.919580      0.0
Prop. mediated (control)  0.912695        0.814965        1.025747      0.0
Prop. mediated (treated)  0.912695        0.814965        1.025747      0.0
ACME (average)            0.742427        0.643339        0.862745      0.0
ADE (average)             0.073017       -0.016189        0.155321      0.1
Prop. mediated (average)  0.912695        0.814965        1.025747      0.0

运行具有部分调解的示例

[10]:
run("partial")
                          Estimate  Lower CI bound  Upper CI bound  P-value
ACME (control)            0.987067        0.801560        1.192019      0.0
ACME (treated)            0.987067        0.801560        1.192019      0.0
ADE (control)             1.071734        0.964214        1.150352      0.0
ADE (treated)             1.071734        0.964214        1.150352      0.0
Total effect              2.058801        1.862231        2.288170      0.0
Prop. mediated (control)  0.481807        0.417501        0.533773      0.0
Prop. mediated (treated)  0.481807        0.417501        0.533773      0.0
ACME (average)            0.987067        0.801560        1.192019      0.0
ADE (average)             1.071734        0.964214        1.150352      0.0
Prop. mediated (average)  0.481807        0.417501        0.533773      0.0

运行没有调解的示例

[11]:
run("no")
                          Estimate  Lower CI bound  Upper CI bound  P-value
ACME (control)            0.010200       -0.039434        0.065176      1.0
ACME (treated)            0.010200       -0.039434        0.065176      1.0
ADE (control)             0.902295        0.824526        0.984934      0.0
ADE (treated)             0.902295        0.824526        0.984934      0.0
Total effect              0.912495        0.834728        1.009958      0.0
Prop. mediated (control)  0.003763       -0.044186        0.065520      1.0
Prop. mediated (treated)  0.003763       -0.044186        0.065520      1.0
ACME (average)            0.010200       -0.039434        0.065176      1.0
ADE (average)             0.902295        0.824526        0.984934      0.0
Prop. mediated (average)  0.003763       -0.044186        0.065520      1.0

上次更新:2024 年 10 月 3 日