Skip to main content

Simple Linear Regression: Bayesian Inference

Lecture 11B handout

Author

Name: _________________

Capital Bikeshare

The bikes data in the {bayesrules} package has 500 days of Capital Bikeshare data from Washington, D.C. (2011–2012): rides (number of rides that day), temp_feel (what the temperature felt like, in °F), and weekend (TRUE for Saturday or Sunday).

We fit Bayesian regression models with stan_glm() from the {rstanarm} package.

Example from Johnson, Ott, & Dogucu (2022), Bayes Rules!, Chapter 9.

Same model, different view. On Monday, \(\beta_0\) and \(\beta_1\) were blank to fill in. Today they are blank to fill in with a prior and a posterior:

\[Y_i \mid \beta_0, \beta_1, \sigma \ \sim \ N(\beta_0 + \beta_1 X_i, \ \sigma^2)\]

\(\beta_0 + \beta_1 X_i\) is the blank to fill in on days with temperature \(X_i\), and \(\sigma\) is how far a single day typically is blank to fill in.

Step 1: prior

Prior understanding of bikeshare ridership
Parameter Prior understanding Prior
Centered intercept On an average temperature day (69.1 °F), typically about 5,000 rides (between 3,000 and 7,000). \(N(5000, 1000^2)\)
\(\beta_1\) Each extra degree, about 100 more rides (between 20 and 180). \(N(100, 40^2)\)

We set priors for the centered intercept and the slope. For \(\sigma\), we let stan_glm() choose the prior.1

Centered intercept. Remember centering from Monday! In stan_glm(), prior_intercept is a prior for the centered intercept: the typical number of rides on an average temperature day, not on a 0 °F day. Why is it easier to set a prior for the centered intercept?

blank to fill in

100 straight lines for rides against temperature, from 45 to 90 degrees, simulated from the priors. Almost all of the lines go up, but they vary a lot in steepness and height.

Steps 2 and 3: data and posterior

library(rstanarm)
library(bayesrules)

bike_model <- stan_glm(rides ~ temp_feel, data = bikes,
                       prior_intercept = normal(5000, 1000),
                       prior = normal(100, 40),
                       seed = 84735, refresh = 0)

bike_draws <- as.data.frame(bike_model)
head(bike_draws, 3)
  (Intercept) temp_feel    sigma
1   -2026.328  79.59005 1280.649
2   -2002.014  80.28257 1244.610
3   -1998.677  78.96210 1338.058

The posterior has no simple formula, so stan_glm() simulates 4,000 plausible values of \((\beta_0, \beta_1, \sigma)\) from it. Each blank to fill in of bike_draws is one plausible line.

Scatterplot of rides against temperature with 50 posterior plausible lines in pink. The lines are almost on top of each other, and all go up with a slope of about 82 rides per degree.

Compared to the prior lines, the posterior lines are blank to fill in.

Using the posterior

Two intercepts. Our prior was for the centered intercept, but bike_draws has only one intercept column:

  • (Intercept): the usual blank to fill in intercept, \(\beta_0\), the typical number of rides on a blank to fill in day. That is why it is negative.
  • The blank to fill in intercept, the typical number of rides on an average temperature day, is not a column. Each draw’s line gives it: \(\beta_0 + \beta_1 \times\) blank to fill in.
bike_draws |>
  mutate(centered = `(Intercept)` + temp_feel * mean(bikes$temp_feel)) |>
  summarize(posterior_mean_centered = mean(centered),
            posterior_mean_uncentered = mean(`(Intercept)`))
  posterior_mean_centered posterior_mean_uncentered
1                3488.193                 -2197.799

Our prior said about 5,000 rides on an average temperature day. What does the posterior say? blank to fill in

bike_draws |>
  summarize(posterior_mean_b1 = mean(temp_feel),
            posterior_lower_b1 = quantile(temp_feel, 0.025),
            posterior_upper_b1 = quantile(temp_feel, 0.975))
  posterior_mean_b1 posterior_lower_b1 posterior_upper_b1
1          82.23443           72.14052           92.30615
  1. Interpret the posterior mean of \(\beta_1\) in context.
  1. The 95% posterior credible interval for \(\beta_1\) is the middle 95% of the posterior draws. Interpret it in context.
  1. \(H_0: \beta_1 \le 0\) and \(H_A: \beta_1 > 0\). The posterior probability of a hypothesis is the blank to fill in where it is true.
bike_draws |>
  summarize(posterior_prob_positive = mean(temp_feel > 0))
  posterior_prob_positive
1                       1

\(P(H_A \mid \text{data}) \approx\) blank to fill in. Conclusion:

  1. Prediction. Using the posterior means, the typical number of rides on a 75 °F day is blank to fill in. A single 75 °F day could be quite different from this: individual days vary around the line by about \(\sigma \approx\) blank to fill in rides (the posterior mean of \(\sigma\)).

A binary explanatory variable: weekends

\(X_i = 1\) on weekends and 0 on weekdays. Typical rides on a weekday: blank to fill in. On a weekend day: blank to fill in. So \(\beta_1\) is the blank to fill in.

We keep the same prior for the centered intercept (a typical day). We are not sure whether weekends have more or fewer rides, so the prior for \(\beta_1\) is \(N(0, 1000^2)\). Prior probability that \(\beta_1 < 0\): blank to fill in

week_model <- stan_glm(rides ~ weekend, data = bikes,
                       prior_intercept = normal(5000, 1000),
                       prior = normal(0, 1000),
                       seed = 84735, refresh = 0)
week_draws <- as.data.frame(week_model)
week_draws |>
  summarize(posterior_mean_b0 = mean(`(Intercept)`),
            posterior_mean_b1 = mean(weekendTRUE))
  posterior_mean_b0 posterior_mean_b1
1          3714.147         -793.0288
  1. (Intercept) is the uncentered intercept, so it is the typical number of rides when \(X = 0\). Posterior mean of the typical rides on a weekday: blank to fill in. On a weekend day: blank to fill in
week_draws |>
  summarize(posterior_lower_b1 = quantile(weekendTRUE, 0.025),
            posterior_upper_b1 = quantile(weekendTRUE, 0.975),
            posterior_prob_negative = mean(weekendTRUE < 0))
  posterior_lower_b1 posterior_upper_b1 posterior_prob_negative
1          -1090.733          -499.2031                       1
  1. Interpret the 95% posterior credible interval for \(\beta_1\) in context.
  1. \(H_A: \beta_1 < 0\) (fewer rides on weekends). Prior probability: blank to fill in. Posterior probability: blank to fill in. What do you conclude? Can we say weekends cause fewer rides?

Frequentist vs. Bayesian

lm() and confint() are from Monday (the {stats} package, which loads automatically).

confint(lm(rides ~ temp_feel, data = bikes))
                  2.5 %      97.5 %
(Intercept) -2883.88257 -1474.65468
temp_feel      71.82028    91.93961
Frequentist and Bayesian intervals for the slope
Frequentist (Monday) Bayesian (today)
R function (package) blank to fill in ({stats}) blank to fill in ({rstanarm})
95% interval for \(\beta_1\) blank to fill in blank to fill in
“95% probability that \(\beta_1\) is in the interval” blank to fill in blank to fill in

Why are the two intervals so similar? blank to fill in

Summary

  • In R: blank to fill in from the {rstanarm} package, with a prior for the intercept (prior_intercept) and the slope (prior); here stan_glm() chose the prior for sigma. prior_intercept is for the blank to fill in intercept. In the draws, (Intercept) is the blank to fill in intercept. The posterior is a set of simulated blank to fill in: as.data.frame().
  • From the draws: the posterior mean (mean()), the 95% posterior credible interval (blank to fill in), and the posterior probability of a hypothesis (blank to fill in).
  • With a lot of data, the posterior credible interval and the confidence interval are blank to fill in, but only the posterior credible interval is a probability statement about \(\beta_1\).

Footnotes

  1. It is usually not a good idea to leave a prior unspecified: we should say what we believe about every parameter. We do it here to keep our first Bayesian regression model simple. \(\sigma\) can only be positive, so a normal prior does not fit it well. In more advanced classes, you will see other probability distributions that are useful for setting a prior for \(\sigma\).↩︎