from pathlib import Path
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import lapylace as lp
sys.path.append(str(Path.cwd().parent / 'python'))
from data import ros_pathPoststratification
Poststratification after estimation. See Chapter 17 in Regression and Other Stories.
The CBS News poll conducted from 12–16 October 2016 reported that, among likely voters who preferred one of the two major-party candidates, 45% intended to vote for Donald Trump and 55% for Hillary Clinton. Of these respondents, Party ID 33% Republican, 40% Republican, 27% independent.
source: http://www.cbsnews.com/news/cbs-poll-clintons-lead-over-trump-widens-with-three-weeks-to-go/ and https://www.scribd.com/document/327938789/CBS-News-Poll-10-17-toplines
Effective sample size of likely voters
254 Republican, 282 Democrat, 242 Independent
Compare to:
exit polls 2012 32 38 29
exit polls 2016 33 36 31
Republicans: 77% Trump, 8% Clinton (must normalize to 100%)
Democrats: 5% Trump, 89% Clinton (must normalize to 100%)
Independents: 36% Trump, 38% Clinton (must normalize to 100%)
`
Source: Poststrat/poststrat.Rmd.
The Python version keeps data handling explicit and uses lapylace for Stan-backed generalized linear models, so the statistical model can be read from a formula rather than from handwritten Stan.
Data
poll = pd.read_csv(ros_path('Poststrat/data', 'poll.csv'))
poll.head()| vote | pid | |
|---|---|---|
| 0 | 1.0 | Republican |
| 1 | 1.0 | Republican |
| 2 | 1.0 | Republican |
| 3 | 1.0 | Republican |
| 4 | 1.0 | Republican |
Models
The formulas below are the Python counterparts of the model formulas in the source example. Use lapylace for the Stan-backed Bayesian fit with the same formula interface.
# fit = lp.stan_glm('vote ~ factor(pid)', data=poll, family=lp.gaussian(), chains=4, iter_sampling=1000)
# fit = lp.stan_glm('vote ~ factor(pid)', data=poll, family=lp.bernoulli(), chains=4, iter_sampling=1000)Notes
- Source computation blocks represented: 11.
- Data paths are expressed through the shared
ros_path()helper. - Formula-based Bayesian regressions are routed through
lapylace.stan_glm(). - Plotting and simulation work uses NumPy, pandas, matplotlib idioms.