Skip to content

Causal Inference: What If 第 11-15 章中英对照

原书: Causal Inference: What If
作者: Miguel A. Hernán, James M. Robins
出版社: Chapman & Hall/CRC (2020)
ISBN: 978-1-4398-8662-5
官方链接: https://www.hsph.harvard.edu/miguel-hernan/causal-inference-book/
翻译时间: 2026-06-01
状态: 中英对照编译版
前续: 第 1-3 章, 第 4-6 章, 第 7-10 章


📚 目录 (Table of Contents)

第二部分:因果推断的高级主题 (Chapters 11-20)

  • Chapter 11: G-Computation Formula (G 计算公式)
  • Chapter 12: G-Estimation of Structural Nested Mean Models (结构嵌套均值模型的 G 估计)
  • Chapter 13: Multi Cause Variables (多原因变量)
  • Chapter 14: Competing Events (竞争事件)
  • Chapter 15: Causal Inference with Multiple Time Points (多时间点的因果推断)

Chapter 11: G-Computation Formula

第 11 章:G 计算公式

11.1 G 公式的基本思想 (Basic Idea of G-Computation)

English:

The G-computation formula, also known as the standardization formula or the G-formula, provides a method to estimate the causal effect of a treatment by averaging over the distribution of confounders.

中文:

G 计算公式(也称为标准化公式或 G 公式)提供了一种通过对混杂因素分布进行平均来估计处理因果效应的方法。

G 公式 (G-Computation Formula):

\[ E[Y^a] = \sum_{l} E[Y|A=a, L=l] \cdot P(L=l) \]

对于连续混杂因素:

\[ E[Y^a] = \int E[Y|A=a, L=l] \cdot f(l) \, dl \]

其中: - \(E[Y^a]\): 处理水平为 a 时的潜在结果期望 - \(E[Y|A=a, L=l]\): 给定处理 A=a 和混杂 L=l 时的条件期望 - \(P(L=l)\)\(f(l)\): 混杂因素的分布


11.2 G 公式的实施步骤 (Implementation Steps)

English:

Implementing the G-formula involves several steps:

中文:

实施 G 公式涉及几个步骤:

步骤流程 (Step-by-Step Process):

步骤 1: 拟合结局模型
       E[Y|A, L, X]
步骤 2: 为每个个体设定处理 A=a
       创建反事实数据集
步骤 3: 使用模型预测潜在结果
       Ŷᵃ = f(A=a, L, X)
步骤 4: 对所有个体平均
       E[Yᵃ] = (1/n) Σ Ŷᵢᵃ
步骤 5: 计算因果效应
       ACE = E[Yᵃ⁼¹] - E[Yᵃ⁼⁰]

Python 实现示例 (Python Implementation):

import pandas as pd
import numpy as np
import statsmodels.api as sm

# 1. 加载观察数据
df = pd.read_csv('observational_data.csv')

# 2. 拟合结局模型 (包含所有混杂因素)
model = sm.GLM(
    df['outcome'],
    sm.add_constant(df[['treatment', 'age', 'sex', 'comorbidity', 'severity']]),
    family=sm.families.Gaussian()
)
result = model.fit()

# 3. 创建反事实数据集 - 所有人都接受治疗
df_counterfactual_1 = df.copy()
df_counterfactual_1['treatment'] = 1

# 4. 预测潜在结果 Y^a=1
Y_a1 = result.predict(sm.add_constant(
    df_counterfactual_1[['treatment', 'age', 'sex', 'comorbidity', 'severity']]
))

# 5. 创建反事实数据集 - 所有人都不接受治疗
df_counterfactual_0 = df.copy()
df_counterfactual_0['treatment'] = 0

# 6. 预测潜在结果 Y^a=0
Y_a0 = result.predict(sm.add_constant(
    df_counterfactual_0[['treatment', 'age', 'sex', 'comorbidity', 'severity']]
))

# 7. 计算平均因果效应
E_Y_a1 = Y_a1.mean()
E_Y_a0 = Y_a0.mean()
ACE = E_Y_a1 - E_Y_a0

print(f"E[Y^a=1] = {E_Y_a1:.3f}")
print(f"E[Y^a=0] = {E_Y_a0:.3f}")
print(f"Average Causal Effect = {ACE:.3f}")

# 8. Bootstrap 置信区间
from sklearn.utils import resample

def bootstrap_g_formula(df, n_iterations=1000):
    ace_estimates = []

    for i in range(n_iterations):
        # 重采样
        df_sample = resample(df, replace=True, n_samples=len(df))

        # 重新拟合模型
        model = sm.GLM(df_sample['outcome'], 
                      sm.add_constant(df_sample[['treatment', 'age', 'sex', 'comorbidity']]))
        result = model.fit()

        # 预测
        df_1 = df_sample.copy()
        df_1['treatment'] = 1
        Y_a1 = result.predict(sm.add_constant(df_1[['treatment', 'age', 'sex', 'comorbidity']]))

        df_0 = df_sample.copy()
        df_0['treatment'] = 0
        Y_a0 = result.predict(sm.add_constant(df_0[['treatment', 'age', 'sex', 'comorbidity']]))

        ace = Y_a1.mean() - Y_a0.mean()
        ace_estimates.append(ace)

    # 95% CI
    ci_lower = np.percentile(ace_estimates, 2.5)
    ci_upper = np.percentile(ace_estimates, 97.5)

    return ci_lower, ci_upper

ci = bootstrap_g_formula(df)
print(f"95% CI: ({ci[0]:.3f}, {ci[1]:.3f})")

11.3 G 公式 vs IPW (G-Formula vs IPW)

English:

Both G-computation and Inverse Probability Weighting (IPW) can estimate causal effects, but they have different properties and assumptions.

中文:

G 计算和逆概率加权 (IPW) 都可以估计因果效应,但它们有不同的性质和假设。

对比表 (Comparison):

特征 G 公式 IPW
建模对象 结局模型 E[Y|A,L] 处理模型 P(A|L)
效率 通常更高效 可能方差较大
稳健性 依赖结局模型正确 依赖倾向评分模型正确
外推 需要模型外推 不需要外推
正值性违反 可以处理 权重不稳定
计算复杂度 中等 较低
双重稳健 与 IPW 结合 与 G 公式结合

双重稳健估计量 (Doubly Robust Estimator):

\[ \hat{E}[Y^a] = \frac{1}{n} \sum_{i=1}^n \left[ \hat{E}[Y|A=a, L_i] + \frac{I(A_i=a)}{\hat{P}(A=a|L_i)} (Y_i - \hat{E}[Y|A=a, L_i]) \right] \]

双重稳健的性质: - 如果结局模型倾向评分模型有一个正确,估计就是一致的 - 两个模型都正确时,估计最有效


Chapter 12: G-Estimation of Structural Nested Mean Models

第 12 章:结构嵌套均值模型的 G 估计

12.1 结构嵌套均值模型 (Structural Nested Mean Models)

English:

Structural Nested Mean Models (SNMMs) are models for the causal effect of treatment within strata defined by past treatment and covariate history.

中文:

结构嵌套均值模型 (SNMM) 是用于在由过去处理和协变量历史定义的层内估计处理因果效应的模型。

SNMM 基本形式 (Basic SNMM Form):

\[ E[Y^a - Y^0 | L, A] = \gamma(a, L; \psi) \]

其中: - \(\gamma(a, L; \psi)\): 结构函数,参数化为ψ - 常见形式:\(\gamma(a, L; \psi) = \psi_0 a + \psi_1 a \cdot L\)

SNMM vs MSM 对比 (SNMM vs MSM Comparison):

特征 SNMM MSM
** estimand** 条件因果效应 边际因果效应
模型 嵌套结构 边际结构
估计方法 G 估计 IPW
解释 给定历史的效应 人群平均效应
计算 更复杂 相对简单

12.2 G 估计方法 (G-Estimation Method)

English:

G-estimation is a method to estimate the parameters of structural nested models by solving estimating equations.

中文:

G 估计是通过求解估计方程来估计结构嵌套模型参数的方法。

G 估计方程 (G-Estimating Equations):

\[ \sum_{i=1}^n S_i(\psi) = 0 \]

其中 \(S_i(\psi)\) 是得分函数:

\[ S_i(\psi) = \{A_i - \hat{P}(A=1|L_i)\} \cdot \{Y_i - \gamma(A_i, L_i; \psi)\} \]

G 估计步骤 (G-Estimation Steps):

步骤 1: 拟合处理模型
       P(A|L)
步骤 2: 定义估计函数
       S(ψ) = {A - P(A=1|L)} × {Y - γ(A,L;ψ)}
步骤 3: 求解ψ使 ΣS(ψ) = 0
       使用数值优化方法
步骤 4: 计算因果效应
       ACE = γ(1, L; ψ̂) - γ(0, L; ψ̂)

Chapter 13: Multi Cause Variables

第 13 章:多原因变量

13.1 多处理设置 (Multiple Treatment Setting)

English:

In many studies, there are multiple treatments or exposures of interest. The causal inference framework can be extended to handle multiple causes.

中文:

在许多研究中,有多个感兴趣的处理或暴露。因果推断框架可以扩展以处理多个原因。

多处理设定 (Multiple Treatment Setup):

处理变量 A 可以有 K 个水平:
A ∈ {0, 1, 2, ..., K}

例如:
- 药物剂量:0mg, 50mg, 100mg, 200mg
- 治疗类型:无治疗,药物 A,药物 B,联合治疗
- 暴露水平:无,低,中,高

潜在结果 (Potential Outcomes):

对于 K+1 个处理水平,有 K+1 个潜在结果: - \(Y^0, Y^1, Y^2, ..., Y^K\)

因果对比 (Causal Contrasts):

对比类型 公式 说明
每种处理 vs 对照 \(E[Y^k] - E[Y^0]\) 每个处理与基线比较
处理间对比 \(E[Y^j] - E[Y^k]\) 任意两个处理比较
剂量反应 \(E[Y^k]\) as function of k 效应随剂量变化

13.2 广义倾向评分 (Generalized Propensity Score)

English:

The generalized propensity score is the conditional probability of receiving a particular treatment level given covariates.

中文:

广义倾向评分是在给定协变量条件下接受特定处理水平的概率。

定义 (Definition):

\[ r_k(l) = P(A = k | L = l), \quad k = 0, 1, ..., K \]

估计方法 (Estimation Methods):

import statsmodels.api as sm
import numpy as np

# 多项逻辑回归估计广义倾向评分
def estimate_generalized_ps(df, treatment_col, covariates):
    """
    使用多项逻辑回归估计广义倾向评分
    """
    X = df[covariates]
    A = df[treatment_col]

    # 多项逻辑回归
    model = sm.MNLogit(A, sm.add_constant(X))
    result = model.fit(disp=0)

    # 预测每个处理水平的概率
    ps_matrix = result.predict(sm.add_constant(X))

    return ps_matrix

# 使用示例
treatments = ['no_treatment', 'drug_a', 'drug_b', 'combination']
ps = estimate_generalized_ps(df, 'treatment', ['age', 'sex', 'severity'])

# 逆概率权重
df['gps'] = ps.values[np.arange(len(df)), df['treatment'].values]
df['weight'] = 1 / df['gps']

13.3 多处理 IPW (IPW for Multiple Treatments)

English:

The IPW estimator for multiple treatments extends the binary treatment case by using the generalized propensity score.

中文:

多处理的 IPW 估计量通过使用广义倾向评分扩展了二值处理的情况。

IPW 估计量 (IPW Estimator):

\[ E[Y^k] = \frac{1}{n} \sum_{i=1}^n \frac{I(A_i = k)}{P(A_i = k | L_i)} Y_i \]

稳定化权重 (Stabilized Weights):

\[ SW_i = \frac{P(A_i = k)}{P(A_i = k | L_i)} \]

Chapter 14: Competing Events

第 14 章:竞争事件

14.1 竞争事件的定义 (Definition of Competing Events)

English:

A competing event is an event that prevents the occurrence of the primary event of interest. Once a competing event occurs, the primary event can no longer be observed.

中文:

竞争事件是阻止主要感兴趣事件发生的事件。一旦发生竞争事件,主要事件就无法再被观察到。

示例 (Examples):

主要事件 竞争事件 说明
癌症死亡 其他原因死亡 死于心脏病就无法观察癌症死亡
疾病复发 死亡 患者在复发前死亡
出院 院内死亡 死亡患者无法出院

14.2 因果效应定义 (Causal Effect Definitions)

English:

In the presence of competing events, there are multiple ways to define causal effects:

中文:

在存在竞争事件的情况下,有多种方法定义因果效应:

效应定义类型 (Types of Effect Definitions):

类型 英文 中文 说明
1 Cause-specific hazard 原因别风险 不考虑竞争事件
2 Subdistribution hazard 次分布风险 考虑竞争事件
3 Cumulative incidence 累积发生率 实际发生概率

累积发生率函数 (Cumulative Incidence Function):

\[ CIF_k(t) = P(T \leq t, \text{event type} = k) \]

其中: - \(T\): 事件发生时间 - \(k\): 事件类型 (1=主要事件,2=竞争事件)


14.3 Fine-Gray 模型 (Fine-Gray Model)

English:

The Fine-Gray model is a proportional hazards model for the subdistribution hazard, which accounts for competing events.

中文:

Fine-Gray 模型是用于次分布风险的比例风险模型,考虑了竞争事件。

Fine-Gray 模型公式 (Fine-Gray Model Formula):

\[ h_1(t | X) = h_{1,0}(t) \cdot \exp(\beta^T X) \]

其中: - \(h_1(t|X)\): 事件类型 1 的次分布风险 - \(h_{1,0}(t)\): 基线次分布风险 - \(X\): 协变量

Python 实现 (Python Implementation):

from lifelines import FineGrayModel
import pandas as pd

# 准备数据
# event: 0=删失,1=主要事件,2=竞争事件
df = pd.read_csv('competing_risks_data.csv')

# 拟合 Fine-Gray 模型
fgm = FineGrayModel()
fgm.fit(
    df,
    event_col='event',
    duration_col='time',
    covariates=['treatment', 'age', 'sex']
)

print(fgm.summary)

# 解释:
# treatment 的系数表示对次分布风险比的影响
# exp(coef) = 次分布风险比 (SHR)

14.4 因果推断与竞争事件 (Causal Inference with Competing Events)

English:

Causal inference in the presence of competing events requires careful consideration of the estimand and the appropriate methods.

中文:

在存在竞争事件的情况下进行因果推断需要仔细考虑 estimand 和适当的方法。

推荐方法 (Recommended Approaches):

┌─────────────────────────────────────────────┐
│ 竞争事件的因果推断策略                       │
├─────────────────────────────────────────────┤
│ 1. 定义清晰的 estimand                        │
│    - 总效应 vs 直接效应                      │
│    - 累积发生率 vs 风险比                     │
│                                             │
│ 2. 选择合适的方法                            │
│    - Fine-Gray 模型 (次分布风险)             │
│    - 原因别风险模型                          │
│    - 多状态模型                              │
│                                             │
│ 3. 敏感性分析                                │
│    - 不同定义的比较                          │
│    - 未测量混杂的评估                        │
└─────────────────────────────────────────────┘

Chapter 15: Causal Inference with Multiple Time Points

第 15 章:多时间点的因果推断

15.1 纵向数据结构 (Longitudinal Data Structure)

English:

Longitudinal data involves repeated measurements on the same individuals over time. Causal inference with longitudinal data requires accounting for time-varying treatments and confounders.

中文:

纵向数据涉及对同一个体随时间的重复测量。纵向数据的因果推断需要考虑时变处理和混杂因素。

数据结构 (Data Structure):

个体 i 在时间 t 的数据:

L₀ → A₀ → L₁ → A₁ → L₂ → A₂ → ... → Y
│    │    │    │    │    │
└────┴────┴────┴────┴────┘
     随时间变化的协变量和处理

其中:
- Lₜ: 时间 t 的协变量
- Aₜ: 时间 t 的处理
- Y: 最终结局

15.2 时变混杂的 g 方法 (G-Methods for Time-Varying Confounding)

English:

G-methods (G-formula, IPW, G-estimation) are designed to handle time-varying confounding affected by prior treatment.

中文:

G 方法 (G 公式、IPW、G 估计) 旨在处理受先前处理影响的时变混杂。

三种 G 方法对比 (Three G-Methods Comparison):

方法 优点 缺点 适用场景
G 公式 可估计任意干预 需要正确指定所有模型 复杂干预方案
IPTW 概念简单 权重不稳定 简单处理方案
G 估计 某些假设下稳健 实现复杂 特定研究问题

多时间点 IPW 权重 (Multi-Timepoint IPW Weights):

\[ W_i = \prod_{t=0}^T \frac{P(A_t = a_t)}{P(A_t = a_t | \bar{L}_t, \bar{A}_{t-1})} \]

其中: - \(\bar{L}_t\): 到时间 t 为止的协变量历史 - \(\bar{A}_{t-1}\): 到时间 t-1 为止的处理历史


15.3 边际结构模型 (Marginal Structural Models)

English:

Marginal Structural Models (MSMs) are models for the marginal effect of treatment, estimated using IPW to account for time-varying confounding.

中文:

边际结构模型 (MSM) 是处理边际效应的模型,使用 IPW 估计时变混杂。

MSM 模型形式 (MSM Model Form):

\[ E[Y^{\bar{a}}] = \beta_0 + \beta_1 \cdot \text{cumulative}(\bar{a}) \]

其中: - \(\bar{a} = (a_0, a_1, ..., a_T)\): 处理历史 - \(\text{cumulative}(\bar{a})\): 累积处理 (如总剂量、持续时间)

Python 完整实现 (Complete Python Implementation):

import pandas as pd
import numpy as np
import statsmodels.api as sm
from scipy.special import expit

def calculate_longitudinal_iptw(df, time_points, treatment_var, covariate_vars):
    """
    计算纵向数据的逆概率处理权重
    """
    n = len(df)
    weights = np.ones(n)

    # 分子:边际处理概率
    # 分母:条件处理概率

    for t in time_points:
        # 准备数据
        A_t = df[f'{treatment_var}_t{t}']

        # 协变量:当前和历史的 L 和 A
        covs_t = []
        for var in covariate_vars:
            for t_past in range(t + 1):
                covs_t.append(f'{var}_t{t_past}')

        # 处理历史
        for t_past in range(t):
            covs_t.append(f'{treatment_var}_t{t_past}')

        # 拟合处理模型
        X_t = sm.add_constant(df[covs_t])
        treatment_model = sm.GLM(A_t, X_t, family=sm.families.Binomial())
        treatment_result = treatment_model.fit()

        # 预测概率
        p_t = treatment_result.predict(X_t)

        # 计算权重
        # P(A_t | 历史)
        prob_given_history = np.where(A_t == 1, p_t, 1 - p_t)

        # P(A_t) 边际概率
        p_marginal = A_t.mean()
        prob_marginal = np.where(A_t == 1, p_marginal, 1 - p_marginal)

        # 稳定化权重
        weights *= prob_marginal / prob_given_history

    return weights

# 使用示例
time_points = [0, 1, 2, 3]
weights = calculate_longitudinal_iptw(
    df,
    time_points,
    treatment_var='treatment',
    covariate_vars=['age', 'severity', 'comorbidity']
)

df['iptw'] = weights

# 拟合 MSM
msm_model = sm.WLS(
    df['outcome'],
    sm.add_constant(df['cumulative_treatment']),
    weights=df['iptw']
)
msm_result = msm_model.fit(cov_type='robust')
print(msm_result.summary)

15.4 动态处理制度 (Dynamic Treatment Regimes)

English:

A dynamic treatment regime is a sequence of decision rules that specify how treatment should be assigned at each time point based on the individual's history.

中文:

动态处理制度是一系列决策规则,指定在每个时间点如何根据个体的历史分配处理。

动态制度示例 (Dynamic Regime Example):

决策规则:
- t=0: 如果 severity > 5,则 A₀=1,否则 A₀=0
- t=1: 如果 severity_t=1 > 3 且 A₀=0,则 A₁=1,否则 A₁=0
- t=2: 如果 response=0 且 A₁=1,则 A₂=0 (停止治疗),否则继续

目标:估计遵循此动态制度的因果效应

G 公式估计动态制度 (G-Formula for Dynamic Regimes):

\[ E[Y^{\bar{d}}] = \sum_{\bar{l}} E[Y|\bar{A}=\bar{d}(\bar{L}), \bar{L}=\bar{l}] \cdot P(\bar{L}=\bar{l}) \]

其中 \(\bar{d}\) 是动态处理制度。


15.5 本章小结 (Chapter Summary)

核心概念 (Core Concepts):

  1. G 计算公式
  2. 通过结局模型标准化估计因果效应
  3. 可处理复杂干预方案
  4. 与 IPW 结合实现双重稳健

  5. 结构嵌套模型

  6. 建模条件因果效应
  7. G 估计方法
  8. 适用于时变处理

  9. 多处理变量

  10. 广义倾向评分
  11. 多值 IPW
  12. 剂量反应关系

  13. 竞争事件

  14. 累积发生率函数
  15. Fine-Gray 模型
  16. 因果效应定义

  17. 多时间点推断

  18. 纵向数据结构
  19. 时变混杂的 G 方法
  20. 动态处理制度

关键术语表 (Glossary):

English 中文 定义
G-computation G 计算 基于结局模型的标准化方法
SNMM 结构嵌套均值模型 条件因果效应模型
G-estimation G 估计 SNMM 的参数估计方法
Generalized propensity score 广义倾向评分 多值处理的倾向评分
Competing event 竞争事件 阻止主要事件发生的事件
Cumulative incidence 累积发生率 考虑竞争事件的发生概率
Fine-Gray model Fine-Gray 模型 次分布风险比例风险模型
Longitudinal data 纵向数据 重复测量的数据
Dynamic treatment regime 动态处理制度 基于历史的序贯决策规则
Marginal structural model 边际结构模型 处理边际效应的模型

📖 学习建议 (Study Recommendations)

理解检查 (Comprehension Check)

问题 1: G 公式和 IPW 的主要区别是什么?

点击查看答案 G 公式建模结局 E[Y\|A,L],IPW 建模处理 P(A\|L)。G 公式通常更高效但需要结局模型正确,IPW 更稳健但权重可能不稳定。两者结合可实现双重稳健。

问题 2: 什么是竞争事件?如何处理?

点击查看答案 竞争事件是阻止主要事件发生的事件(如其他原因死亡)。处理方法包括:累积发生率函数、Fine-Gray 模型、原因别风险模型。需要明确定义的 estimand。

问题 3: 动态处理制度与普通处理对比有何不同?

点击查看答案 动态处理制度是序贯决策规则,根据个体历史调整处理。估计方法包括 G 公式、IPW for regimes、强化学习。目标是找到最优动态制度。

延伸阅读 (Further Reading)

  1. G 公式与 SNMM:
  2. Robins, J. M. (1986). A new approach to causal inference in mortality studies with a sustained exposure period. Mathematical Modelling.
  3. Hernán, M. A., & Robins, J. M. (2006). Estimating causal effects from epidemiological data. JECH.

  4. 竞争事件:

  5. Fine, J. P., & Gray, R. J. (1999). A proportional hazards model for the subdistribution of a competing risk. JASA.
  6. Beyersmann, J., et al. (2011). Competing Risks and Multistate Models with R.

  7. 动态处理制度:

  8. Murphy, S. A. (2003). Optimal dynamic treatment regimes. JRSS-B.
  9. Zhao, Y., et al. (2015). Doubly robust learning for estimating individualized treatment with censored data. Biometrika.

翻译完成时间: 2026-06-01
原书章节: Chapter 11-15
前续: 第 1-3 章, 第 4-6 章, 第 7-10 章
后续: 第 16-20 章 (更高级主题,待创建)


**Causal Inference: What If 中英对照 | 第 11-15 章** [返回顶部](#目录-table-of-contents)