--- jupytext: text_representation: extension: .md format_name: myst format_version: 0.13 kernelspec: display_name: Python 3 language: python name: python3 --- # Union premium heterogeneity — effects by subgroup An average marginal effect answers "what is the effect on average?". Often the research question is sharper: *for whom* is the effect large, and *for whom* is it small? The union wage premium is the textbook case — unions are widely held to *compress* wages, raising pay most at the bottom of the skill distribution and least at the top. That is a statement about heterogeneity, and it is invisible in a single AME. This demo uses the `linearmodels` wage panel (545 young men, 1980–1987) to estimate the union premium **by education band**, using the `over=` subgroup argument and a contrast that lets the premium differ across groups. It also makes the central modelling point explicit: *a subgroup effect can only differ if the model lets it* — through an interaction, or through a nonlinear link. ```{code-cell} python import numpy as np import pandas as pd import statsmodels.formula.api as smf from linearmodels.datasets import wage_panel from pymargins import Margins df = wage_panel.load().reset_index() df["educ_band"] = pd.cut( df["educ"], [0, 11, 12, 20], labels=["