Get startedGet started for free

Plotting the Poisson regression model

Recall the likelihood structure for your Bayesian Poisson regression model of volume \(Y\)i by weekday status \(X\)i and temperature \(Z\)i: \(Y\)i \(\sim Pois(l\)i) where

  • \(log(l\)i\() \; = a + b \; X\)i \(+ c \; Z\)i; thus
  • \(l\)i\( \; = exp(a + b \; X\)i \(+ c \; Z\)i\()\)

Your 10,000 iteration RJAGS simulation of the model posterior, poisson_sim, is in your workspace along with a data frame of the Markov chain output:

> head(poisson_chains, 2)
         a b.1.       b.2.          c
1 5.019807    0 -0.1222143 0.01405269
2 5.018642    0 -0.1217608 0.01407691

You will use these results to plot the posterior Poisson regression trends. These nonlinear trends can be added to a ggplot() using stat_function(). For example, specifying fun = function(x){x^2} would return a quadratic trend line.

This exercise is part of the course

Bayesian Modeling with RJAGS

View Course

Exercise instructions

Construct a scatterplot of volume by hightemp with the following features:

  • Use color to distinguish between weekdays & weekends.
  • Superimpose a red curve that represents the posterior mean Poisson regression trend \(l\)i of the linear relationship between volume and hightemp for weekends: l = exp(a + c Z)
  • Superimpose a turquoise3 curve that represents the posterior mean Poisson regression trend \(l\)i of the linear relationship between volume and hightemp for weekdays: l = exp((a + b.2.) + c Z)

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Plot the posterior mean regression models
ggplot(___, aes(x = ___, y = ___, color = ___)) + 
    geom_point() + 
    stat_function(fun = function(x){___(mean(___) + mean(___) * x)}, color = "red") + 
    stat_function(fun = function(x){___(mean(___) + mean(___) + mean(___) * x)}, color = "turquoise3")
Edit and Run Code