1 개요
Ch.20 이 deadlock 을 보여줬다 — 32,000 명 가상 sequentially randomized 시험에서 sharp null 인데도 stratification, 회귀, matching 모두 -8 의 비영 추정. 표준 도구 box 전체 실패.
Ch.21 은 해결책 을 도입한다 — Robins (1986) 의 g-method 의 시간변동 일반화. 같은 Table 21.1 데이터에 g-formula, IPW MSM, g-estimation 적용 시 모두 정확히 0 추정. deadlock 의 정량적 해결.
직관 — Ch.20 의 deadlock → Ch.21 의 escape: 같은 데이터, 다른 도구. -8 → 0. 도구 선택의 결정적 영향. Robins 의 1986 년 발명이 인과 분석의 패러다임 전환을 만든 정확한 사례.
Robins (1986, 1987) 가 도입한 3 가지 도구의 시간변동 일반화:
G-formula (Parametric): Ch.13 의 표준화의 시간변동 형태. 시점별 결과 모형 + covariate 분포 모형 + Monte Carlo 시뮬레이션.
IPW Marginal Structural Model: Ch.12 의 IPW MSM 의 시간변동 형태. 시점별 가중치의 곱
- 가중 회귀.
- G-estimation of SNMM: Ch.14 의 SNMM 의 시간변동 형태. Sequential structural nested model + g-test 로 추정.
Doubly Robust 일반화: 위 3 도구의 결합 (Ch.18 의 DR ML 의 시간변동 형태).
직관 — 3 도구의 결정적 차이: 단순 시점 도구 (회귀) 가 한 단계 분석. G-method 는 시점별 분리 분석 + 통합. 시간 차원을 일급 시민으로 취급 — 단순 도구의 본질적 한계 회피.
2 6 개 소챕터의 흐름
| 소챕터 | 핵심 도구 | 분석 형태 |
|---|---|---|
| 21.1 | G-formula | 시점별 결과 모형 + 시뮬레이션 |
| 21.2 | IPW for time-varying | 시점별 가중치 곱 + 가중 회귀 |
| 21.3 | Doubly Robust | 위 두 도구의 결합 |
| 21.4 | G-estimation | SNMM + g-test |
| 21.5 | Censoring as time-varying treatment | Censoring 도 처치처럼 처리 |
| 21.6 | Big g-formula | 단일 모형으로 모든 시점 처리 |
3 NHEFS Table 21.1 사례 — 3 도구의 일관된 답
Table 21.1 의 32,000 명 데이터 (= Ch.20 의 Table 20.1):
| 도구 | \(\widehat{\mathrm{E}}[Y^{a_0=0,a_1=0}]\) | \(\widehat{\mathrm{E}}[Y^{a_0=1,a_1=1}]\) | ATE |
|---|---|---|---|
| g-formula | \(84 \times 0.25 + 52 \times 0.75 = 60\) | \(76 \times 0.5 + 44 \times 0.5 = 60\) | 0 |
| IPW MSM | 60 (가상 모집단 평균) | 60 | 0 |
| g-estimation | 60 | 60 | 0 |
(Ch.20 의 전통 도구: -13.3 (비보정), -8 (층화), -8 (회귀). 진짜 = 0).
3 g-method 모두 정확히 0 — deadlock 해결.
직관 — 3 도구의 일치성의 의미: 다른 가정·다른 절차의 3 도구가 모두 같은 답 → 통계적 robust. Ch.20 의 deadlock 이 Ch.21 의 도구로 완전 해결. Robins (1986) 의 발명 가치.
직관 — 각 도구의 메커니즘: - g-formula: covariate 분포 모형, Monte Carlo 로 가상 시나리오 시뮬레이션. - IPW MSM: 가중치 곱 으로 가상 모집단 생성, 단순 가중 회귀. - g-estimation: structural model + sequential 추정. 셋 모두 conditional 분석을 marginal 로 변환 — collider 함정 회피.
4 핵심 개념 6 가지
4.1 1. G-formula 의 시간변동 일반화 (21.1)
단일 시점 g-formula (Ch.13): \[\mathrm{E}[Y^a] = \sum_l \mathrm{E}[Y | A=a, L=l] \Pr(L=l)\]
시간변동 g-formula: \[\mathrm{E}[Y^{\bar{a}}] = \sum_{\bar{l}} \mathrm{E}[Y | \bar{A}=\bar{a}, \bar{L}=\bar{l}] \prod_{k=0}^{K} f(l_k | \bar{a}_{k-1}, \bar{l}_{k-1})\]
= 매 시점 confounder 분포 의 곱 으로 가중. 시점별 covariate 분포 모형 각각 필요.
핵심: 가상 시나리오 시뮬레이션 — 만약 모든 환자가 strategy \(\bar{a}\) 따랐다면 어떤 covariate 분포 + 결과 분포?
직관 — 시뮬레이션의 본질: \(A_0\) 강제 → \(L_1\) 분포 결정 (모형 사용) → \(A_1\) 강제 → \(L_2\) 분포 → … → \(Y\) 분포. Markov chain Monte Carlo 와 유사한 sequential 시뮬레이션.
직관 — 표 분해의 직관: Hernan Table 21.1 데이터에서 g-formula 적용: - “Never treat” 시 \(A_0=0\) → \(\Pr(L_1|A_0=0)\) 의 분포 = (0.25, 0.75). - 두 stratum 의 평균 outcome (84, 52) 을 가중 평균. - \(84 \times 0.25 + 52 \times 0.75 = 60\). - 같은 절차로 “always treat” → 60. 두 가상 시나리오 모두 동일 → ATE = 0.
4.2 2. IPW for Time-Varying (21.2)
비안정화 가중치: \[W^{\bar{A}} = \prod_{k=0}^{K} \frac{1}{f(A_k | \bar{A}_{k-1}, \bar{L}_k)}\]
안정화 가중치: \[SW^{\bar{A}} = \prod_{k=0}^{K} \frac{f(A_k | \bar{A}_{k-1})}{f(A_k | \bar{A}_{k-1}, \bar{L}_k)}\]
= 매 시점 처치 결정의 역확률 곱. 가상 모집단에서 \(\bar{A}\) 가 \(\bar{L}\) 와 독립.
시점 곱이 분산 폭발의 원인 — \(K\) 시점이 길수록 가중치 폭발.
직관 — Sequential Randomization 의 시뮬레이션: 매 시점의 조건부 무작위 처치를 가중치로 모방. 가상 모집단에서는 \(\bar{A}\) 가 모든 covariate 과 독립 — sequentially randomized experiment 의 인공적 회복.
직관 — Stabilization 의 결정적 중요성: 시점별 IPW 의 곱 이 비안정화에서 분산 폭발. 안정 화 (\(f(A_k | \bar{A}_{k-1})\) 분자) 이 시점별 변동 흡수 → 분산 안정. 시간변동에서는 안정화가 사실상 필수.
4.3 3. Marginal Structural Model 의 시간변동 (21.2)
처치 시점 수 \(K+1\) 에서 가능한 history \(2^{K+1}\) — 너무 많음.
대신 압축된 모형 사용: \[\mathrm{E}[Y^{\bar{a}}] = \beta_0 + \beta_1 \cdot \text{cumulative}(\bar{a}) + \beta_2 \cdot \text{duration}(\bar{a})\]
또는: \[\mathrm{E}[Y^{\bar{a}}] = \beta_0 + \beta_1 \cdot a_K + \beta_2 \cdot a_K \cdot \text{cumulative}(\bar{a}_{K-1})\]
→ 시점별 처치 효과를 간략한 함수형 으로 표현. IPW 가중 회귀로 모수 추정.
Functional form 가정 이 모형의 misspecification 위험 추가.
직관 — Structural Marginal 의 의미: \(\mathrm{E}[Y^{\bar{a}}]\) 의 함수형 직접 모형링. 표준 회귀의 \(\mathrm{E}[Y | \bar{A}, \bar{L}]\) 와 다름 — covariate \(\bar{L}\) 없이 처치 history 만 의존.
직관 — Cumulative Treatment 모형의 흔함: 약물의 총 노출량 이 결과 결정. “60 개월 중 몇 개월 ART 받았나” 의 함수. 단순한 가정 + 임상 의미 명확.
4.4 4. Doubly Robust 시간변동 (21.3)
g-formula + IPW MSM 의 결합:
\[\widehat{\mathrm{E}}[Y^{\bar{a}}]_\text{DR} = \widehat{\mathrm{E}}[Y^{\bar{a}}]_\text{g-formula} + \frac{1}{n} \sum_i \prod_k \frac{\mathbb{1}\{A_{k,i} = a_k\}}{\widehat{f}(A_{k,i} | \bar{A}_{k-1,i}, \bar{L}_{k,i})} \times \text{(잔차 보정)}\]
→ 처치 모형 (IPW) + 결과 모형 (g-formula) 중 한쪽만 옳아도 일치.
→ ML 결합 시 (Ch.18 의 DR ML 의 시간변동 일반화) — 활발한 연구 영역.
직관 — 시간변동 DR 의 어려움: 단일 시점 DR 은 모형 2 개. 시간변동 DR 은 시점별 모형 2 개 — 시점 수 만큼 모형 폭발. 구현 매우 복잡, 실무에서 single-robust 가 흔함.
직관 — 향후 발전 방향: Chernozhukov et al. 의 DDML + 시간변동의 결합. TMLE (Targeted Maximum Likelihood Estimation) 의 시간변동 형태. 2025 년 이후 표준화 진행 중.
4.5 5. G-estimation 의 시간변동 (21.4)
Structural Nested Mean Model (SNMM) 의 시간변동 일반화:
\[\mathrm{E}[Y^{\bar{a}_{k-1}, a_k = 1, \bar{a}_{k+1:K}^g} - Y^{\bar{a}_{k-1}, a_k = 0, \bar{a}_{k+1:K}^g} | \bar{A}_{k-1}, \bar{L}_k] = \beta_k\]
= 시점 \(k\) 의 처치 효과 만 모형링 (covariate effect 비제약 — semiparametric).
추정: G-test (Ch.14) 의 sequential 일반화 — 시점별 후보 모수 검색.
직관 — SNMM 의 강점 유지: Semiparametric — covariate 의 결과 효과 (\(\mathrm{E}[Y|\bar{L}, \bar{A}]\) 의 nuisance) 미명시. 결과 모형 misspecification 영향 분리.
직관 — 구현의 복잡성: G-test 의 sequential 시점별 적용. Closed-form 없으면 grid search 또는 Newton-Raphson. 이론적 도구지만 g-formula·IPW MSM 보다 실무 사용 적음.
4.6 6. Censoring as Time-Varying Treatment (21.5)
매 시점 censoring \(C_k = 1\) (drop out) 또는 \(C_k = 0\) (계속) 을 처치처럼 처리.
가중치: \(W^{\bar{A}, \bar{C}} = \prod_k \frac{1}{f(A_k | \bar{A}_{k-1}, \bar{L}_k)} \times \prod_k \frac{1}{\Pr(C_k = 0 | \bar{A}_{k}, \bar{L}_k)}\).
→ 처치 + censoring 의 결합 가중치. 인과 추정 대상: \(\mathrm{E}[Y^{\bar{a}, \bar{c}=\bar{0}}]\) — “모든 사람이 strategy 따르고 + 아무도 dropout 안 했다면”.
직관 — Censoring 의 인과적 해석: “Censoring 안 됨” 을 가상 처치로. 단일 시점 IPW (Ch.12.6) 의 시간변동 일반화. Loss to follow-up + 시간변동 처치의 통합 분석.
직관 — Big G-formula (21.6): 시점별 모형 분리 대신 단일 결합 모형 으로 모든 시점 처리. 계산 효율적이지만 모형 specification 가정이 강함. ML + g-method 결합의 표준 방향.
5 표준 분석 워크플로우
Step 1: DAG 분석 (Ch.19) — 시간변동 confounder 식별.
Step 2: Sequential exchangeability 가정 검토 (Ch.19) — 모든 시점 보정 변수 식별.
Step 3: Strategy 결정 — static (always vs never) 또는 dynamic (CD4 임계값).
Step 4: G-method 선택: - Outcome 모형 자신 → g-formula. - 처치 모형 자신 → IPW MSM. - Robustness 우선 → DR. - Effect modification 명시 → SNMM g-estimation.
Step 5: 다중 도구 비교 — Ch.18.5 의 sensitivity 원칙.
직관 — Ch.18.5 의 원칙이 시간변동에도 적용: 단일 도구 의존 금지. 다중 도구의 결과 일치성으로 robust 검증. Ch.20 의 deadlock 해결되어도 도구 가정의 robust 검토는 여전히 필요.
6 응용 분야 — 시간변동 g-method 의 표준 사례
HIV/AIDS Cohort (CASCADE, MACS, ACTG): - 매 방문 (3-6 개월) ART 처치 + CD4 측정. - ART 시작 시점, dose 변경, 중단 모두 시간변동. - G-formula + IPW MSM 표준 분석. - 핵심 결과: ART 의 사망률 감소 효과 정량화 (1990-2000 년대).
종양학 SMART Trial: - 항암제 sequential 투여 + 종양 반응에 따른 dose 조정. - Adaptive trial design — sequentially randomized. - G-method 의 표준 적용.
SWEDEN HEART Registry: - 심부전 약물 (베타차단제, ACE inhibitor) 시작·변경 시점. - 매 약물 처방의 시간변동 효과. - IPW MSM 분석.
자동차 보험 가격: - 가입자별 가격 변동 (시간변동 처치). - 각 시점의 가입 vs 해지 결과. - 마케팅 attribution 의 인과 분석.
직관 — Real-World 데이터의 풍부한 시간변동: 의료 EHR, 보험 회사 시스템, 추천 알고리즘 모두 시점별 처치 + 시점별 결과 데이터. 시간변동 g-method 가 21 세기 인과 분석의 표준 도구.
7 후속 글로 이어지는 다리
| 글 | 다루는 내용 |
|---|---|
| 20-1 | 21.1 + 21.2 — Time-Varying G-Formula + IPW MSM |
| 20-2 | 21.3 + 21.4 — Doubly Robust + G-Estimation |
| 20-3 | 21.5 + 21.6 — Censoring 통합 + Big G-Formula |
8 코드 미리보기 — Time-Varying G-Formula
import pandas as pd
import numpy as np
import statsmodels.formula.api as smf
from sklearn.ensemble import GradientBoostingClassifier, GradientBoostingRegressor
# === Hernan Table 21.1 데이터 (= Table 20.1) ===
data_summary = pd.DataFrame({
"A0": [0, 0, 0, 0, 1, 1, 1, 1],
"L1": [0, 0, 1, 1, 0, 0, 1, 1],
"A1": [0, 1, 0, 1, 0, 1, 0, 1],
"N": [2400, 1600, 2400, 9600, 4800, 3200, 1600, 6400],
"Y": [84, 84, 52, 52, 76, 76, 44, 44]
})
rows = []
for _, r in data_summary.iterrows():
for _ in range(int(r["N"])):
rows.append({"A0": r["A0"], "L1": r["L1"], "A1": r["A1"], "Y": r["Y"]})
df = pd.DataFrame(rows)
print("=== Hernan Table 21.1 ===\n")
# === G-formula (수동 계산) ===
print("=== g-formula ===")
# Never treat (a_0 = a_1 = 0)
# E[Y | A_0=0, A_1=0, L_1=l] * Pr(L_1=l | A_0=0) sum over l
e_y_l0 = df[(df.A0==0) & (df.A1==0) & (df.L1==0)].Y.mean()
e_y_l1 = df[(df.A0==0) & (df.A1==0) & (df.L1==1)].Y.mean()
pr_l0_a0 = (df[df.A0==0].L1 == 0).mean()
pr_l1_a0 = (df[df.A0==0].L1 == 1).mean()
e_y_never = e_y_l0 * pr_l0_a0 + e_y_l1 * pr_l1_a0
print(f"E[Y^(0,0)] = {e_y_l0} × {pr_l0_a0:.2f} + {e_y_l1} × {pr_l1_a0:.2f} = {e_y_never:.1f}")
# Always treat (a_0 = a_1 = 1)
e_y_l0_a = df[(df.A0==1) & (df.A1==1) & (df.L1==0)].Y.mean()
e_y_l1_a = df[(df.A0==1) & (df.A1==1) & (df.L1==1)].Y.mean()
pr_l0_a1 = (df[df.A0==1].L1 == 0).mean()
pr_l1_a1 = (df[df.A0==1].L1 == 1).mean()
e_y_always = e_y_l0_a * pr_l0_a1 + e_y_l1_a * pr_l1_a1
print(f"E[Y^(1,1)] = {e_y_l0_a} × {pr_l0_a1:.2f} + {e_y_l1_a} × {pr_l1_a1:.2f} = {e_y_always:.1f}")
ate = e_y_always - e_y_never
print(f"\n진짜 ATE = 0, g-formula ATE = {ate:.1f} ✓")
# === IPW MSM ===
print("\n=== IPW MSM ===")
# 시점 0 가중치
df["w0"] = np.where(df.A0 == 1, 1/0.5, 1/0.5) # 50:50
# 시점 1 가중치 — L1 의존
df["pa1"] = np.where(df.L1 == 1, 0.8, 0.4)
df["w1"] = np.where(df.A1 == 1, 1/df.pa1, 1/(1-df.pa1))
df["w_total"] = df["w0"] * df["w1"]
# 가중 평균
e_y_never_ipw = (df[(df.A0==0) & (df.A1==0)].Y * df[(df.A0==0) & (df.A1==0)].w_total).sum() / df[(df.A0==0) & (df.A1==0)].w_total.sum()
e_y_always_ipw = (df[(df.A0==1) & (df.A1==1)].Y * df[(df.A0==1) & (df.A1==1)].w_total).sum() / df[(df.A0==1) & (df.A1==1)].w_total.sum()
print(f"E[Y^(0,0)] (IPW) = {e_y_never_ipw:.1f}")
print(f"E[Y^(1,1)] (IPW) = {e_y_always_ipw:.1f}")
print(f"IPW MSM ATE = {e_y_always_ipw - e_y_never_ipw:.1f} ✓")
# === Ch.20 의 전통 도구 비교 ===
print("\n=== Comparison with Ch.20 traditional methods ===")
m_traditional = smf.ols("Y ~ A0 + A1 + L1 + A0:L1 + A1:L1", data=df).fit()
# A0=A1=1 vs A0=A1=0 의 차이
df1 = pd.DataFrame({"A0": [1], "A1": [1], "L1": [df.L1.mean()]})
df0 = pd.DataFrame({"A0": [0], "A1": [0], "L1": [df.L1.mean()]})
trad_ate = m_traditional.predict(df1)[0] - m_traditional.predict(df0)[0]
print(f"전통 회귀 ATE: {trad_ate:.1f} (편향 — 진짜 0)")
print(f"\n→ G-method 가 deadlock 해결 (Robins 1986)")9 한 줄 요약
Ch.21 은 Ch.20 의 deadlock 의 본격 해결책. Robins (1986) 의 g-method (g-formula, IPW MSM, g-estimation) 의 시간변동 일반화. Hernan Table 21.1 (= Table 20.1) 에서 3 도구 모두 정확히 0 추정 — 전통 도구의 -8 대비. G-formula = 시점별 covariate 분포 모형 + 시뮬레이션. IPW MSM = 시점별 가중치 곱. G-estimation = sequential SNMM. Doubly robust 결합 + censoring 통합 처리 + big g-formula 의 단일 결합 모형. 인과 분석의 패러다임 전환 — Ch.21 이 Part III 의 정점.
10 관련 주제
선행 지식
- Ch.20 TC Feedback
- Ch.19 시간변동 처치
- Ch.13 표준화·g-formula 단일 시점
- Ch.12 IPW와 MSM 단일 시점
- Ch.14 G-estimation 단일 시점
후속 주제
- Time-Varying G-Formula + IPW MSM — Ch.21.1-21.2
- Doubly Robust + G-Estimation — Ch.21.3-21.4
- Censoring + Big G-Formula — Ch.21.5-21.6
- Target Trial Emulation — Ch.22
- Causal Mediation — Ch.23
다른 카테고리 연결