# Population and Group-Size Weights
# Module 8 - Group Means, Population Weights, and Aggregated Data
# Dataset: MODULE8_WLS_GROUP_MEANS

# Population and Group-Size Weights
#
# Module 8 notebook lab. This notebook uses original Ceteris Lab teaching data and does not report real empirical findings.

# Learning goal
# Use group size as a precision weight.
#
# Dataset: MODULE8_WLS_GROUP_MEANS. Variables: group_id, group_size, mean_outcome, mean_income, mean_education, population_weight.

# %% Cell 3
import pandas as pd
import statsmodels.api as sm

df = pd.read_csv("/data/module-8/module8_wls_group_means.csv")
X = sm.add_constant(df[["mean_income", "mean_education"]])
unweighted = sm.OLS(df["mean_outcome"], X).fit()
weighted = sm.WLS(df["mean_outcome"], X, weights=df["population_weight"]).fit()
print(pd.DataFrame({"unweighted": unweighted.params, "population_weighted": weighted.params}).round(4))

# Reflection
# Write two sentences: one sentence explaining what the diagnostic or robust result says, and one sentence explaining a limitation or next step.
