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_pathMile
Trend of record times in the mile run. See Chapter 3 in Regression and Other Stories.
Source: Mile/mile.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
mile = pd.read_csv(ros_path('Mile/data', 'mile.csv'))
mile.head()| yr | month | min | sec | year | seconds | |
|---|---|---|---|---|---|---|
| 0 | 1913 | 5.0 | 4 | 14.4 | 1913.416667 | 254.4 |
| 1 | 1915 | 7.0 | 4 | 12.6 | 1915.583333 | 252.6 |
| 2 | 1923 | 8.0 | 4 | 10.4 | 1923.666667 | 250.4 |
| 3 | 1931 | 10.0 | 4 | 9.2 | 1931.833333 | 249.2 |
| 4 | 1933 | 7.0 | 4 | 7.6 | 1933.583333 | 247.6 |
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('seconds ~ year', data=mile, family=lp.gaussian(), chains=4, iter_sampling=1000)Notes
- Source computation blocks represented: 23.
- 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.