### Chapter 15 Bayes Rules! R examples: # Cherry Blossom data: # Load packages library(bayesrules) library(tidyverse) library(rstanarm) library(broom.mixed) # Load data data(cherry_blossom_sample) running <- cherry_blossom_sample %>% select(runner, age, net) nrow(running) # Boxplots: ggplot(running, aes(x = runner, y = net)) + geom_boxplot() ## Complete pooling: # Scatterplot of race time against age: ggplot(running, aes(y = net, x = age)) + geom_point() # Regression model: Y=net time, X=age: complete_pooled_model <- stan_glm( net ~ age, data = running, family = gaussian, prior_intercept = normal(0, 2.5, autoscale = TRUE), prior = normal(0, 2.5, autoscale = TRUE), prior_aux = exponential(1, autoscale = TRUE), chains = 4, iter = 5000*2, seed = 84735) # Posterior summary statistics tidy(complete_pooled_model, conf.int = TRUE, conf.level = 0.80) # Plotting regression line from pooled model (blue) # and regression lines from models for individual runners (gray) # Plot of the posterior median model ggplot(running, aes(x = age, y = net, group = runner)) + geom_smooth(method = "lm", se = FALSE, color = "gray", size = 0.5) + geom_abline(aes(intercept = 75.2, slope = 0.268), color = "blue") # Looking at data for runners 1, 20, and 22: # Select an example subset examples <- running %>% filter(runner %in% c("1", "20", "22")) ggplot(examples, aes(x = age, y = net)) + geom_point() + facet_wrap(~ runner) + geom_abline(aes(intercept = 75.2242, slope = 0.2678), color = "blue") # Model with no pooling (three examples): ggplot(examples, aes(x = age, y = net)) + geom_point() + geom_smooth(method = "lm", se = FALSE, fullrange = TRUE) + facet_wrap(~ runner) + xlim(52, 62)