본문 바로가기

계량경제학/인과추론의 데이터과학

인과추론을 위한 회귀분석 - Sample Weights

https://theeffectbook.net/ch-StatisticalAdjustment.html?panelset5=python-code6&panelset6=python-code7&panelset7=python-code8#additional-regression-concerns 

 

Chapter 13 - Regression | The Effect

Chapter 13 - Regression | The Effect is a textbook that covers the basics and concepts of research design, especially as applied to causal inference from observational data.

theeffectbook.net


Sample Weights

  • 일반적으로 regression에서는 sample이 모집단으로부터 ramdom하게 추출되었다고 가정함 (e.g. 한국인 표본은 한국에 사는 사람들의 지역, 나이, 교육 등에 관계 없이 랜덤하게 추출되어야 함)
  • 하지만 많은 경우, 표본 추출 시 random sampling이 이루어지지 않았을 가능성이 큼 (e.g. 서울에 사는 젊은 사람들이 추출하기 훨씬 용이하기 때문) -> regression 결과가 표본을 추출할 확률이 더 높은 사람들에게 편향되었을 가능성이 높아짐
  • 이 때, sample weights을 줌으로써 sample에 중요도를 부여할 수 있음 
  • Ordinary Least Squares가 아닌, Weighted Least Squares으로 회귀계수를 추정함 

Sample Weigts의 예시 

  • 모집단: 남성 500명 (50%), 여성 500명 (50%)
  • 표본: 모집단에서 100명을 추출한 결과, 남성 70명 (70%), 여성 30명 (30%)으로 구성됨 
  • 모집단에서 추출한 표본에 남성이 포함될 확률 = 70/500 = 14%, 여성이 포함될 확률 = 30 / 500 = 6%
  • 남성의 sample weight = 1 / .14 = 7.143, 여성의 sample weight = 1 / .06 = 16.667
  • 표본 추출이 될 가능성이 더 낮은 사람들에게 높은 가중치를 부여하여 추정치가 모집단을 더 잘 대표하도록 한다.

관찰이 개인 level이 아닐 때 Sample Weights를 어떻게 줘야 하는가? 

  • e.g.) 관찰이 교실 level일 때 - 1반은 30명, 2반은 40명, ... y는 반 평균 -> 각 반의 명 수에 대해서 frequency weights를 줘도 됨  
import statsmodels.formula.api as sm
from causaldata import restaurant_inspections
df = restaurant_inspections.load_pandas().data

# Aggregate the data
df['Num_Inspections'] = 1
df = df.groupby('business_name').agg(
    {'inspection_score': 'mean',
    'Year': 'min',
    'Num_Inspections': 'sum'})

# Here we call a special WLS function
m1 = sm.wls(formula = 'inspection_score ~ Year',
            weights = df['Num_Inspections'],
            data = df).fit()

m1.summary()