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_pathAgePeriodCohort
Age-Period-Cohort - Demonstration of age adjustment to estimate trends in mortality rates. See Chapter 3 in Regression and Other Stories.
Source: AgePeriodCohort/births.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
us_est00int_alldata = pd.read_csv(ros_path('AgePeriodCohort/data', 'US-EST00INT-ALLDATA.csv'))
us_est00int_alldata.head()
births = pd.read_csv(ros_path('AgePeriodCohort/data', 'births.txt'), sep=r'\s+')
births.head()
black_death_rates_from_1999_to_2013_by_sex = pd.read_csv(ros_path('AgePeriodCohort/data', 'black_death_rates_from_1999_to_2013_by_sex.txt'), sep=r'\s+')
black_death_rates_from_1999_to_2013_by_sex.head()
deaton = pd.read_csv(ros_path('AgePeriodCohort/data', 'deaton.txt'), sep=r'\s+')
deaton.head()
white_hisp_death_rates_from_1999_to_2013_by_sex = pd.read_csv(ros_path('AgePeriodCohort/data', 'white_hisp_death_rates_from_1999_to_2013_by_sex.txt'), sep=r'\s+')
white_hisp_death_rates_from_1999_to_2013_by_sex.head()
white_nonhisp_death_rates_from_1999_to_2013_by_sex = pd.read_csv(ros_path('AgePeriodCohort/data', 'white_nonhisp_death_rates_from_1999_to_2013_by_sex.txt'), sep=r'\s+')
white_nonhisp_death_rates_from_1999_to_2013_by_sex.head()| Age | Male | Year | Deaths | Population | Rate | |
|---|---|---|---|---|---|---|
| 0 | 35 | 0 | 1999 | 1291 | 1578829 | 81.8 |
| 1 | 35 | 0 | 2000 | 1264 | 1528463 | 82.7 |
| 2 | 35 | 0 | 2001 | 1186 | 1377466 | 86.1 |
| 3 | 35 | 0 | 2002 | 1194 | 1333639 | 89.5 |
| 4 | 35 | 0 | 2003 | 1166 | 1302188 | 89.5 |
Notes
- Source computation blocks represented: 59.
- 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.