NES: linear probability summaries

Source: NES/nes_linear.Rmd

This page uses Gaussian lapylace regressions as the linear-probability companion to the NES logistic example.

Setup

from pathlib import Path
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import lapylace as lp

root = Path("../../ROS-Examples")

def coef_medians(fit):
    return pd.Series(
        [np.median(fit.alpha_draws()), *np.median(fit.beta_draws(), axis=0)],
        index=["Intercept", *fit.columns],
    )

def fit_glm(formula, data, family=None, seed=1, prior_scale=2.5, intercept_scale=5, aux_scale=10, **kwargs):
    return lp.stan_glm(
        formula,
        data=data,
        family=family or lp.gaussian(),
        prior=lp.normal(0, prior_scale),
        prior_intercept=lp.normal(0, intercept_scale),
        prior_aux=lp.exponential(aux_scale),
        chains=2,
        parallel_chains=2,
        iter_warmup=300,
        iter_sampling=500,
        seed=seed,
        refresh=100,
        **kwargs,
    )
nes = pd.read_csv(root / "NES/data/nes.txt", sep=r"\s+", index_col=0, na_values=["NA"])
d = nes.loc[nes.year == 1992, ["rvote", "income", "female", "black"]].dropna().copy()
d["rvote"] = d["rvote"].astype(float)
d.head()
rvote income female black
32093 1.0 4 1 0
32094 1.0 2 1 0
32096 0.0 1 1 1
32097 1.0 2 0 0
32098 0.0 3 1 0
fit = fit_glm("rvote ~ income + female + black", d, seed=18101, prior_scale=2.5, intercept_scale=5, aux_scale=1)
fit.summary(["alpha", "beta", "sigma"])
                                                                                                                                                                
Mean MCSE StdDev 5% 50% 95% N_Eff N_Eff/s R_hat
alpha 0.246450 0.002074 0.043692 0.171520 0.245949 0.318253 443.989000 708.117000 1.000900
beta[1] 0.044699 0.000532 0.011878 0.025663 0.044493 0.064241 497.837000 793.998000 1.001130
beta[2] 0.007528 0.000910 0.024746 -0.031872 0.008191 0.047391 740.262000 1180.640000 1.001020
beta[3] -0.322904 0.001405 0.040291 -0.389097 -0.324131 -0.255734 822.932000 1312.490000 0.999306
sigma 0.463037 0.000293 0.008898 0.448351 0.463127 0.477993 923.011032 1472.106909 1.001635
coef_medians(fit).round(3)
Intercept    0.246
income       0.044
female       0.008
black       -0.324
dtype: float64