1 정의
행 요인 \(A\) (\(j = 1, 2\)), 열 요인 \(B\) (\(k = 1, 2\)) 의 셀 모형:
\[ Y_{ijk} = \mu + \alpha_j + \beta_k + (\alpha\beta)_{jk} + \varepsilon_{ijk} \]
제약: \(\sum_j \alpha_j = 0\), \(\sum_k \beta_k = 0\), \(\sum_j (\alpha\beta)_{jk} = \sum_k (\alpha\beta)_{jk} = 0\).
자유도 분해 (등표본 \(n\)):
| 효과 | 자유도 | 의미 |
|---|---|---|
| \(A\) 주효과 (\(\alpha_j\)) | 1 | 행 marginal 평균의 차이 |
| \(B\) 주효과 (\(\beta_k\)) | 1 | 열 marginal 평균의 차이 |
| \(A \times B\) 상호작용 (\((\alpha\beta)_{jk}\)) | 1 | 두 요인 효과의 비가산성 |
| 오차 | \(4(n-1)\) | 셀 내 변동 |
2 데이터 — Maxwell SBP 사례
Maxwell Table 7.1, 2×2 (Drug × BF), 각 셀 \(n=5\).
평균 표:
| BF Present | BF Absent | Row Avg | |
|---|---|---|---|
| Drug Present | \(\bar Y_{11} = 168\) | \(\bar Y_{12} = 186\) | \(\bar Y_{1\cdot} = 177\) |
| Drug Absent | \(\bar Y_{21} = 188\) | \(\bar Y_{22} = 190\) | \(\bar Y_{2\cdot} = 189\) |
| Col Avg | \(\bar Y_{\cdot 1} = 178\) | \(\bar Y_{\cdot 2} = 188\) | \(\bar Y_{\cdot \cdot} = 183\) |
Group 번호: 1=BF+Drug, 2=BF only, 3=Drug only, 4=Neither.
각 셀의 표본 분산이 동일 (\(s = 7.91\) → \(s^2 = 62.56\)) 이므로 \(MS_W = 62.5\).
3 주효과: marginal mean 의 차이
3.1 Drug 주효과
Drug Present row vs Drug Absent row: \[ \hat\alpha_1 = \bar Y_{1\cdot} - \bar Y_{\cdot \cdot} = 177 - 183 = -6 \] \[ \hat\alpha_2 = \bar Y_{2\cdot} - \bar Y_{\cdot \cdot} = 189 - 183 = +6 \]
대비 표현 (4 셀 평균을 좌→우, 위→아래로 1, 2, 3, 4 순서로 정렬한 경우 — 즉 \(\bar Y_1 = \bar Y_{11}, \bar Y_2 = \bar Y_{12}, \bar Y_3 = \bar Y_{21}, \bar Y_4 = \bar Y_{22}\)):
\[ \psi_A = (1, 1, -1, -1) \cdot (168, 186, 188, 190) = (168+186) - (188+190) = -24 \]
이는 \(2 \times (\bar Y_{1\cdot} - \bar Y_{2\cdot}) \times 2 = 2 \cdot 2 (\hat\alpha_1 - \hat\alpha_2) = 2 \cdot 2 \cdot (-12) = -48\)? 부호 단위만 맞추자 — 정확히는 row marginal 의 두 배 차이이며, SS 계산에 사용되는 것은 \(\hat\psi_A^2 / \sum c_j^2\) 형태이다.
\[ SS_A = \frac{n \, \hat\psi_A^2}{\sum_j c_j^2} = \frac{5 \times 576}{4} = 720 \]
3.2 BF 주효과
\[ \psi_B = (1, -1, 1, -1) \cdot (168, 186, 188, 190) = (168 + 188) - (186 + 190) = -20 \]
\[ SS_B = \frac{5 \times 400}{4} = 500 \]
3.3 상호작용
상호작용 대비 \((1, -1, -1, 1)\) 은 첫 행의 차 (\(\bar Y_{11} - \bar Y_{12}\)) 와 둘째 행의 차 (\(\bar Y_{21} - \bar Y_{22}\)) 의 비교이다.
\[ \psi_{A \times B} = (1, -1, -1, 1) \cdot (168, 186, 188, 190) \] \[ = (168 + 190) - (186 + 188) = 358 - 374 = -16 \]
\[ SS_{A \times B} = \frac{5 \times 256}{4} = 320 \]
4 직교성과 SS 가산
세 대비의 직교성:
| 쌍 | \(\sum c_j^A c_j^B\) |
|---|---|
| Drug × BF | \((1)(1) + (1)(-1) + (-1)(1) + (-1)(-1) = 0\) |
| Drug × Inter | \((1)(1) + (1)(-1) + (-1)(-1) + (-1)(1) = 0\) |
| BF × Inter | \((1)(1) + (-1)(-1) + (1)(-1) + (-1)(1) = 0\) |
세 대비 모두 직교이므로 SS 가 가산된다.
\[ SS_{\text{between}} = SS_A + SS_B + SS_{A \times B} = 720 + 500 + 320 = 1540 \]
ANOVA 의 between SS (Table 7.2) 와 일치.
5 \(F\) 검정
| 효과 | \(SS\) | \(df\) | \(MS\) | \(F\) | \(p\) |
|---|---|---|---|---|---|
| Drug | 720 | 1 | 720 | \(720/62.5 = 11.52\) | 0.004 |
| BF | 500 | 1 | 500 | \(500/62.5 = 8.00\) | 0.012 |
| Drug × BF | 320 | 1 | 320 | \(320/62.5 = 5.12\) | 0.038 |
| Within | 1000 | 16 | 62.5 | — | — |
| Total | 2540 | 19 | — | — | — |
세 효과 모두 \(\alpha = 0.05\) 에서 유의.
6 상호작용의 두 가지 해석
6.1 해석 1 — 한 요인의 효과가 다른 요인 levels 에 따라 달라짐
BF 의 SBP 강하 효과: - Drug 와 함께: \(186 - 168 = 18\) (점) - Drug 없이: \(190 - 188 = 2\) (점)
평균 BF 효과는 10 이지만, 실제 효과는 Drug 사용 여부에 따라 18 vs 2 로 크게 다르다. 이 비가산성이 곧 상호작용이다.
6.2 해석 2 — Cell 평균의 차이의 차이
\[ (\bar Y_{12} - \bar Y_{11}) - (\bar Y_{22} - \bar Y_{21}) = (186 - 168) - (190 - 188) = 18 - 2 = 16 \]
이 값의 절댓값 16 은 상호작용 대비 \(\hat\psi_{A \times B} = -16\) 의 부호 변경이다 (대비 방향 차이뿐). 통계적 유의성은 동일.
\(A\) 와 \(B\) 가 가산적이면 “\(A\) 의 효과는 어디서나 같다 + \(B\) 의 효과는 어디서나 같다” 가 성립한다. 가산성을 깨려면 한 요인의 levels 별로 다른 요인의 효과가 달라야 한다. 이를 수치로 잡는 것이 “차이의 차이”. 인과추론의 difference-in-differences (DID) 와 형태가 같다 — 단 DID 는 시간을 한 축으로, factorial 은 두 처치 요인을 축으로 한다.
7 효과 모수 추정량
각 셀 평균에서 grand mean 과 marginal effect 를 빼면 상호작용 모수가 남는다.
\[ \widehat{(\alpha\beta)}_{jk} = \bar Y_{jk} - \hat\alpha_j - \hat\beta_k - \hat\mu \]
| BF Present (\(\hat\beta_1 = -5\)) | BF Absent (\(\hat\beta_2 = +5\)) | |
|---|---|---|
| Drug Present (\(\hat\alpha_1 = -6\)) | \(168 - 183 - (-6) - (-5) = -4\) | \(186 - 183 - (-6) - (+5) = +4\) |
| Drug Absent (\(\hat\alpha_2 = +6\)) | \(188 - 183 - (+6) - (-5) = +4\) | \(190 - 183 - (+6) - (+5) = -4\) |
상호작용 모수 표 \(((\alpha\beta)_{jk})\) 는 행과 열 합이 모두 0 인 \(2 \times 2\) 패턴을 가진다. 이 패턴이 곧 \(A \times B\) 의 부호 정보이다.
8 모형 비교 perspective
Maxwell 의 모형 비교 lens:
| 모형 | 식 | 모수 수 |
|---|---|---|
| Full | \(\mu_{jk} = \mu + \alpha_j + \beta_k + (\alpha\beta)_{jk}\) | 4 |
| No interaction | \(\mu_{jk} = \mu + \alpha_j + \beta_k\) | 3 |
| \(A\) only | \(\mu_{jk} = \mu + \alpha_j\) | 2 |
| \(B\) only | \(\mu_{jk} = \mu + \beta_k\) | 2 |
| Null | \(\mu_{jk} = \mu\) | 1 |
각 효과의 SS 는 두 모형의 잔차제곱합 차이이다 (등표본 가정):
\[ SS_{A \times B} = E_{\text{No interaction}} - E_{\text{Full}} \] \[ SS_A = E_{\text{B only}} - E_{\text{No interaction}} \] \[ SS_B = E_{\text{A only}} - E_{\text{No interaction}} \]
비등표본일 경우 모형 비교가 더는 직교 분해와 일치하지 않는다 — 이 점이 G-MAX7-3 (Type I/II/III SS) 의 핵심이다.
9 Python 코드
import numpy as np
import pandas as pd
import statsmodels.api as sm
from statsmodels.formula.api import ols
# Maxwell Table 7.1: 2x2 factorial, n=5 per cell
np.random.seed(2026)
data = pd.DataFrame({
"drug": ["yes"]*10 + ["no"]*10,
"bf": (["yes"]*5 + ["no"]*5) * 2,
"sbp": [158, 163, 173, 178, 168, # drug yes, bf yes
188, 183, 198, 178, 193, # drug yes, bf no
186, 191, 196, 181, 176, # drug no, bf yes
185, 190, 195, 200, 180] # drug no, bf no
})
# Type I/II/III ANOVA
model = ols("sbp ~ C(drug) * C(bf)", data=data).fit()
print("Cell means:")
print(data.groupby(["drug", "bf"])["sbp"].mean().unstack())
print(f"\nGrand mean: {data['sbp'].mean():.2f}")
print(f"Marginal Drug: {data.groupby('drug')['sbp'].mean().to_dict()}")
print(f"Marginal BF: {data.groupby('bf')['sbp'].mean().to_dict()}")
# Type II SS (등표본이면 Type I/II/III 모두 동일)
anova_table = sm.stats.anova_lm(model, typ=2)
print("\nType II ANOVA:")
print(anova_table.round(3))
# 대비 기반 검증
y_cells = data.groupby(["drug", "bf"])["sbp"].mean()
# 정렬: (drug=no, bf=no), (drug=no, bf=yes), (drug=yes, bf=no), (drug=yes, bf=yes)
order = [("no","no"), ("no","yes"), ("yes","no"), ("yes","yes")]
yc = np.array([y_cells.loc[o] for o in order])
print(f"\nCell means in order {order}:")
print(yc)
# 대비 계수 (Drug A: rows, BF B: cols)
n = 5
contrasts = {
"Drug (A) main": np.array([-1, -1, +1, +1]), # no vs yes
"BF (B) main": np.array([-1, +1, -1, +1]), # no vs yes
"Drug x BF (AB)": np.array([+1, -1, -1, +1]), # interaction
}
print("\nContrast-based SS:")
for name, c in contrasts.items():
psi = np.sum(c * yc)
ss = n * psi**2 / np.sum(c**2)
F = ss / (anova_table.loc["Residual", "sum_sq"] / anova_table.loc["Residual", "df"])
print(f" {name:>18s}: psi={psi:+.2f}, SS={ss:6.1f}, F={F:5.2f}")기대 출력:
Type II ANOVA:
sum_sq df F PR(>F)
C(drug) 720.0 1.0 11.52 0.0037
C(bf) 500.0 1.0 8.00 0.0121
C(drug):C(bf) 320.0 1.0 5.12 0.0381
Residual 1000.0 16.0 NaN NaN
Contrast-based SS:
Drug (A) main: psi=+24.00, SS= 720.0, F=11.52
BF (B) main: psi=+20.00, SS= 500.0, F= 8.00
Drug x BF (AB): psi=-16.00, SS= 320.0, F= 5.12
ANOVA 와 대비 분해의 결과가 정확히 일치한다.
10 시각적 해석 — 상호작용 plot
상호작용은 셀 평균을 한 요인의 levels 에 따라 plot 하면 한눈에 보인다.
SBP
│ * * ← Drug Absent (거의 평행, 효과 작음)
│
│
│ * ← Drug Present (BF 와 큰 차이)
│ *
│
└────────────── → BF level
Present Absent
두 line 이 평행하면 상호작용 없음 (additive). 기울기·교차가 다르면 상호작용 존재. “두 line 이 교차” 하면 disordinal interaction (한 요인의 효과 부호가 뒤집힘), “평행하지 않지만 교차하지 않으면” ordinal interaction (효과 크기만 다름).
11 가정과 한계
- 셀 표본 균등: 등표본 가정이 깨지면 직교 분해가 깨짐 (G-MAX7-3 의 Type I/II/III).
- 셀 내 정규성·등분산: 셀별 분산이 다르면 Welch-type 또는 robust ANOVA 적용.
- 충분한 셀당 표본: \(n \ge 5\) 는 최소 권장. 검정력은 \(n\) 이 결정 (G-MAX7-4).
- 상호작용이 있으면 주효과 해석 신중: BF 의 평균 효과 -10 은 Drug 와 함께 -18, 단독 -2 의 가중평균. 상호작용 유의 시 marginal 평균만 보지 말고 simple effect (G-MAX7-2) 를 검정한다.
12 관련 주제
선행 지식
후속 주제
다른 카테고리 연결