bike_model <- stan_glm(rides ~ temp_feel, data = bikes,
prior_intercept = normal(5000, 1000),
prior = normal(100, 40),
seed = 84735, refresh = 0)Today: the same questions for Bayesian models. Some tools are the same, and some are new.
Capital Bikeshare rides and “feels like” temperature (°F) in Washington, D.C., 2011–2012.
The bikes data come from Capital Bikeshare, which rents bikes from stations around Washington, D.C. Each row is a day: the number of rides that day and that day’s “feels like” temperature, temp_feel.
Discussion
temp_feel is one number for the whole city each day. Does every neighborhood feel the same temperature on the same day? Why might they differ?A model can be fair in one context and unfair in another. We evaluate the model and how it will be used.
\[Y_i \mid \beta_0, \beta_1, \sigma \sim N(\beta_0 + \beta_1 X_i, \ \sigma^2)\]
This says the same thing as Monday’s \(Y_i = \beta_0 + \beta_1 X_i + \epsilon_i\) with \(\epsilon_i \sim N(0, \sigma^2)\): the same LINE assumptions.
Each row of bikes is a day, and the days come one after another. On its own, ridership is not independent: the number of rides today is likely similar to the number of rides tomorrow.
But much of this similarity comes from the time of year, and our model includes temperature, which follows the time of year. If we know today’s and tomorrow’s temperatures, today’s rides tell us little extra about tomorrow’s rides.
So, given temperature, it is reasonable to treat the days as independent. “Reasonable” does not mean “perfect”. It means “good enough to proceed.”
This argument is from Johnson, Ott, & Dogucu (2022), Bayes Rules!, Chapter 10.
The residual plots are the same as on Monday. For a stan_glm() model, fitted() gives \(\hat{y}_i\) and residuals() gives \(e_i\) (using the posterior median line).
Remember our Bayesian model:
Each row of bike_draws is one plausible line (\(\beta_0\), \(\beta_1\)) and one plausible \(\sigma\): how far single days typically are from the line.
The idea: if our model describes the real data well, then data simulated from the model should look like the real data.
For each of the 500 real days, we start at this line’s value for that day’s temperature, then add random noise of about \(\sigma\):
Do the simulated data look like the real data? What looks different?
pp_check() from {rstanarm} repeats this with 50 rows of bike_draws and compares the distributions of rides: the real rides (dark) and the 50 simulated data sets (light).
Is our model reasonable? Is it perfect?
On Monday, the least squares estimates had formulas. The posterior has no formula: stan_glm() simulates it, combining the prior (what we believed before) and the data (through the likelihood).
| Parameter | Least squares (lm()) |
Posterior mean (stan_glm()) |
|---|---|---|
| Intercept, β₀ | -2179.3 | -2198.6 |
| Slope, β₁ | 81.88 | 82.22 |
With 500 days of data, the data dominate the prior, and the two are close.
On Monday: a confidence interval for the average baby vs. a prediction interval for a single baby. Bayesian version: last week, each row of bike_draws gave the typical (average) number of rides on a 75 °F day: \(\beta_0 + \beta_1 \times 75\).
A single 75 °F day also varies around the line, by about \(\sigma\). So for each row, we add random noise of about that row’s \(\sigma\), just like when we simulated data.
The average day (left) is very certain. A single day (right) can have many more or many fewer rides.
average_lower average_upper
1 3838.285 4096.615
single_lower single_upper
1 1535.126 6496.355
We can make a 95% posterior predictive interval for each of the 500 days in the data, at that day’s temperature. If the model is accurate, about 95% of the real days should fall inside their own intervals.
prediction_summary() from the {bayesrules} package does this for us:
within_95: 97% of the real days fall inside their 95% posterior predictive interval (and within_50: 44% inside their 50% interval).mae: the typical distance between a day’s real rides and its prediction is about 986 rides.Exercise
Based on within_95 and within_50, are the model’s posterior predictive intervals about right? Is a typical error of about 986 rides a lot?
Last week: rides ~ temp_feel and rides ~ weekend, separately. Now both:
\[Y_i \mid \beta_0, \beta_1, \beta_2, \sigma \sim N(\beta_0 + \beta_1 \text{temp\_feel}_i + \beta_2 \text{weekend}_i, \ \sigma^2)\]
We need a prior for each parameter. We keep the priors from last week:
| Parameter | Prior |
|---|---|
| Centered intercept (an average temperature weekday) | \(N(5000, 1000^2)\) |
Slope of temp_feel, β₁ |
\(N(100, 40^2)\) |
Slope of weekend, β₂ |
\(N(0, 1000^2)\) |
prior takes one normal distribution for each slope, in the same order as the variables.
(Intercept) temp_feel weekendTRUE sigma
1 -1866.107 79.83063 -774.9802 1268.241
2 -1396.788 74.02053 -479.3370 1299.355
3 -2084.841 82.23587 -778.4981 1172.328
Each row is one plausible set of \((\beta_0, \beta_1, \beta_2, \sigma)\).
Two sets of parallel lines: weekdays (pink, solid) and weekends (green, dashed).
posterior_mean_b1 posterior_lower_b1 posterior_upper_b1
1 80.67803 70.80376 90.27475
posterior_mean_b2 posterior_lower_b2 posterior_upper_b2
1 -697.314 -950.6154 -453.8724
Exercise
weekendTRUE) in context. Remember: holding temperature constant.The same tools as on Monday. Each row of bike_draws_2 gives a plausible line for weekdays, one for weekends, and \(\sigma\). pp_check() simulates 50 data sets and compares them with the real rides.
Do the simulated data sets look like the real data?
On Monday: in a real analysis, splitting the data is the first step, not the last.
For Bayesian models, we check the testing data against the posterior predictive intervals with prediction_summary().
Fit stan_glm() on the training data only. Then, as earlier today, check how many days in the testing data fall inside their 95% posterior predictive intervals.
set.seed(84735)
bike_split <- initial_split(bikes, prop = 0.8)
bike_train <- training(bike_split)
bike_test <- testing(bike_split)
bike_train_2 <- stan_glm(rides ~ temp_feel + weekend, data = bike_train,
prior_intercept = normal(5000, 1000),
prior = normal(c(100, 0), c(40, 1000)),
seed = 84735, refresh = 0) mae mae_scaled within_50 within_95
1 931.9683 0.7416612 0.45 0.97
data = bike_test: the model never saw these days.
| Explanatory variables | mae (rides) |
within_50 |
within_95 |
|---|---|---|---|
| temp_feel | 1,006 | 0.39 | 0.97 |
| temp_feel + weekend | 932 | 0.45 | 0.97 |
Exercise
Which model predicts new days better? Are the posterior predictive intervals about right for both models?
| Question | Frequentist (lm()) |
Bayesian (stan_glm()) |
|---|---|---|
| How wrong? | LINE: residual plots with augment() |
LINE: residual plots and pp_check() |
| The average y at a given x | \(\hat{y}\) and a 95% confidence interval | 95% posterior credible interval for \(\beta_0 + \beta_1 x\) |
| A single new y at a given x | 95% prediction interval | 95% posterior predictive interval |
| How accurate? | RMSE and R² | prediction_summary() |
| More variables | lm(y ~ x1 + x2) |
stan_glm(y ~ x1 + x2), one prior per slope |
| New data | RMSE on testing data | prediction_summary() on testing data |
pp_check().prediction_summary().stan_glm(y ~ x1 + x2, ...) with one prior per slope, prior = normal(c(...), c(...)). Slopes are interpreted holding the other variables constant.prediction_summary(model, data = testing_data).