Introclass

Plot residuals vs. predicted values, or residuals vs. observed values? See Chapter 11 in Regression and Other Stories.

Source: Introclass/residual_plots.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.

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_path

Data

gradesw4315 = pd.read_csv(ros_path('Introclass/data', 'gradesW4315.dat'), sep=r'\s+')
gradesw4315.head()
hw1 hw2 hw3 hw4 midterm hw5 hw6 hw7 final
0 95 88 100 95 80 96 99 0 103
1 0 74 74 0 53 83 97 0 79
2 100 0 105 100 91 96 100 96 122
3 0 90 76 100 63 91 95 0 78
4 100 96 99 100 91 93 100 92 135

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('final ~ midterm', data=gradesw4315, family=lp.gaussian(), chains=4, iter_sampling=1000)

# fit = lp.stan_glm('final_fake ~ midterm', data=gradesw4315, family=lp.gaussian(), chains=4, iter_sampling=1000)

Notes

  • Source computation blocks represented: 20.
  • 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.