DataCamp Bayesian Modeling with RJAGS BAYESIAN MODELING WITH RJAGS Bayesian regression with a categorical predictor Alicia Johnson Associate Professor, Macalester College
DataCamp Bayesian Modeling with RJAGS Chapter 4 goals Incorporate categorical predictors into Bayesian models Engineer multivariate Bayesian regression models Extend our methodology for Normal regression models to generalized linear models: Poisson regression
DataCamp Bayesian Modeling with RJAGS Rail-trail volume Goal: Explore daily volume on a rail-trail in Massachusetts. [1] Photo courtesy commons.wikimedia.org
DataCamp Bayesian Modeling with RJAGS Modeling volume Y = trail volume (# of users) on day i i Model 2 Y ∼ N ( m , s ) i i
DataCamp Bayesian Modeling with RJAGS Modeling volume by weekday Y = trail volume (# of users) on day i i X = 1 for weekdays, 0 for weekends i Model 2 Y ∼ N ( m , s ) i i
DataCamp Bayesian Modeling with RJAGS Modeling volume by weekday Y = trail volume (# of users) on day i i X = 1 for weekdays, 0 for weekends i Model 2 Y ∼ N ( m , s ) i i
DataCamp Bayesian Modeling with RJAGS Modeling volume by weekday Y = trail volume (# of users) on day i i X = 1 for weekdays, 0 for weekends i Model 2 Y ∼ N ( m , s ) i i m = a + bX i i
DataCamp Bayesian Modeling with RJAGS Modeling volume by weekday Y = trail volume (# of users) on day i i X = 1 for weekdays, 0 for weekends i Model 2 Y ∼ N ( m , s ) i i m = a + bX i i a = typical weekend volume
DataCamp Bayesian Modeling with RJAGS Modeling volume by weekday Y = trail volume (# of users) on day i i X = 1 for weekdays, 0 for weekends i Model 2 Y ∼ N ( m , s ) i i m = a + bX i i a = typical weekend volume a + b = typical weekday volume b = contrast between typical weekday vs weekend volume s = residual standard deviation
DataCamp Bayesian Modeling with RJAGS Priors for a & b Typical weekend volume is most likely We lack certainty about how weekday around 400 users per day, but possibly volume compares to weekend volume. It as low as 100 or as high as 700 users. could be more, it could be less.
DataCamp Bayesian Modeling with RJAGS Prior for s The standard deviation in volume from day to day (whether on weekdays or weekends) is equally likely to be anywhere between 0 and 200 users.
DataCamp Bayesian Modeling with RJAGS Bayesian model of volume by weekday status 2 Y ∼ N ( m , s ) i i m = a + bX i i 2 a ∼ N (400,100 ) 2 b ∼ N (0,200 ) s ∼ Unif (0,200)
DataCamp Bayesian Modeling with RJAGS DEFINE the Bayesian model in RJAGS rail_model_1 <- "model{ 2 Y ∼ N ( m , s ) # Likelihood model for Y[i] i i m = a + bX i i 2 a ∼ N (400,100 ) 2 b ∼ N (0,200 ) # Prior models for a, b, s s ∼ Unif (0,200) }"
DataCamp Bayesian Modeling with RJAGS DEFINE the Bayesian model in RJAGS rail_model_1 <- "model{ 2 Y ∼ N ( m , s ) # Likelihood model for Y[i] i i for(i in 1:length(Y)) { m = a + bX Y[i] ~ dnorm(m[i], s^(-2)) i i 2 a ∼ N (400,100 ) } 2 b ∼ N (0,200 ) # Prior models for a, b, s s ∼ Unif (0,200) a ~ dnorm(400, 100^(-2)) s ~ dunif(0, 200) }"
DataCamp Bayesian Modeling with RJAGS DEFINE the Bayesian model in RJAGS rail_model_1 <- "model{ # Likelihood model for Y[i] m[i] <- a + b[X[i]] for(i in 1:length(Y)) { Y[i] ~ dnorm(m[i], s^(-2)) m[i] <- a + b[X[i]] X[1] = weekend, X[2] = weekday } # Prior models for a, b, s b has 2 levels: b[1] , b[2] a ~ dnorm(400, 100^(-2)) s ~ dunif(0, 200) weekend trend ( m = a ) i }" m[i] <- a + b[1]
DataCamp Bayesian Modeling with RJAGS DEFINE the Bayesian model in RJAGS rail_model_1 <- "model{ # Likelihood model for Y[i] m[i] <- a + b[X[i]] for(i in 1:length(Y)) { Y[i] ~ dnorm(m[i], s^(-2)) m[i] <- a + b[X[i]] X[1] = weekend, X[2] = weekday } # Prior models for a, b, s b has 2 levels: b[1] , b[2] a ~ dnorm(400, 100^(-2)) s ~ dunif(0, 200) weekend trend ( m = a ) b[1] <- 0 i }" m[i] <- a + b[1] b[1] <- 0
DataCamp Bayesian Modeling with RJAGS DEFINE the Bayesian model in RJAGS rail_model_1 <- "model{ # Likelihood model for Y[i] m[i] <- a + b[X[i]] for(i in 1:length(Y)) { Y[i] ~ dnorm(m[i], s^(-2)) m[i] <- a + b[X[i]] X[1] = weekend, X[2] = weekday } # Prior models for a, b, s b has 2 levels: b[1] , b[2] a ~ dnorm(400, 100^(-2)) s ~ dunif(0, 200) weekend trend ( m = a ) b[1] <- 0 i b[2] ~ dnorm(0, 200^(-2)) }" m[i] <- a + b[1] b[1] <- 0 weekday trend ( m = a + b ) i m[i] <- a + b[2] b[2] ~ dnorm(0, 200^(-2))
DataCamp Bayesian Modeling with RJAGS BAYESIAN MODELING WITH RJAGS Let's practice!
DataCamp Bayesian Modeling with RJAGS BAYESIAN MODELING WITH RJAGS Multivariate Bayesian regression Alicia Johnson Associate Professor, Macalester College
DataCamp Bayesian Modeling with RJAGS Modeling volume Y = trail volume (# of users) on day i i [1] Photo courtesy commons.wikimedia.org
DataCamp Bayesian Modeling with RJAGS Modeling volume by weekday Y = trail volume (# of users) on day i i X = 1 for weekdays, 0 for weekends i
DataCamp Bayesian Modeling with RJAGS Modeling volume by temperature Y = trail volume (# of users) on day i i ∘ Z = high temperature on day i (in F) i
DataCamp Bayesian Modeling with RJAGS Modeling volume by temperature & weekday Y = trail volume (# of users) on day i i X = 1 for weekdays, 0 for weekends i ∘ Z = high temperature on day i (in F) i 2 Y ∼ N ( m , s ) i i m = a + bX + cZ i i i Weekends: m = a + cZ i i Weekdays: m = ( a + b ) + cZ i i
DataCamp Bayesian Modeling with RJAGS Modeling volume by temperature & weekday Y = trail volume (# of users) on day i i X = 1 for weekdays, 0 for weekends i ∘ Z = high temperature on day i (in F) i 2 Y ∼ N ( m , s ) i i m = a + bX + cZ i i i Weekends: m = a + cZ i i Weekdays: m = ( a + b ) + cZ i i
DataCamp Bayesian Modeling with RJAGS Modeling volume by temperature & weekday m = a + bX + cZ i i i Weekends: m = a + cZ i i Weekdays: m = ( a + b ) + cZ i i a = weekend y-intercept a + b = weekday y-intercept b = contrast between weekday vs weekend y-intercepts c = common slope s = residual standard deviation
DataCamp Bayesian Modeling with RJAGS Priors for a and b We lack certainty about the y-intercept We lack certainty about how typical for the relationship between temperature volume compares on weekdays vs & weekend volume. weekends of similar temperature.
DataCamp Bayesian Modeling with RJAGS Priors for c and s Whether on weekdays or weekends, we The typical deviation from the trend is lack certainty about the association equally likely to be anywhere between 0 between trail volume & temperature. and 200 users.
DataCamp Bayesian Modeling with RJAGS Bayesian model of volume by weekday status 2 Y ∼ N ( m , s ) i i m = a + bX + cZ i i i 2 a ∼ N (0,200 ) 2 b ∼ N (0,200 ) 2 c ∼ N (0,20 ) s ∼ Unif (0,200)
DataCamp Bayesian Modeling with RJAGS DEFINE the Bayesian model in RJAGS rail_model_2 <- "model{ 2 Y ∼ N ( m , s ) # Likelihood model for Y[i] i i for(i in 1:length(Y)) { m = a + bX + cZ Y[i] ~ dnorm(m[i], s^(-2)) i i i m[i] <- a + b[X[i]] + c * Z[i] 2 a ∼ N (0,200 ) } 2 b ∼ N (0,200 ) # Prior models for a, b, c, s 2 c ∼ N (0,20 ) a ~ dnorm(0, 200^(-2)) b[1] <- 0 s ∼ Unif (0,200) b[2] ~ dnorm(0, 200^(-2)) c ~ dnorm(0, 20^(-2)) s ~ dunif(0, 200) }"
DataCamp Bayesian Modeling with RJAGS BAYESIAN MODELING WITH RJAGS Let's practice!
DataCamp Bayesian Modeling with RJAGS BAYESIAN MODELING WITH RJAGS Poisson regression Alicia Johnson Associate Professor, Macalester College
DataCamp Bayesian Modeling with RJAGS Normal likelihood structure Y = volume (# of users) on a given day 2 Y ∼ N ( m , s ) Technically... The Normal model assumes Y has a continuous scale and can be negative. But Y is a discrete count and cannot be negative.
DataCamp Bayesian Modeling with RJAGS The Poisson model Y = volume (# of users) on a given day Y ∼ Pois( l ) Y is the # of independent events that occur in a fixed interval (0, 1, 2,...). Rate parameter l represents the typical # of events per time interval ( l > 0 ).
DataCamp Bayesian Modeling with RJAGS The Poisson model Y = volume (# of users) on a given day Y ∼ Pois( l ) Y is the # of independent events that occur in a fixed interval (0, 1, 2,...). Rate parameter l represents the typical # of events per time interval ( l > 0 ).
DataCamp Bayesian Modeling with RJAGS The Poisson model Y = volume (# of users) on a given day Y ∼ Pois( l ) Y is the # of independent events that occur in a fixed interval (0, 1, 2,...). Rate parameter l represents the typical # of events per time interval ( l > 0 ).
DataCamp Bayesian Modeling with RJAGS The Poisson model Y = volume (# of users) on a given day Y ∼ Pois( l ) Y is the # of independent events that occur in a fixed interval (0, 1, 2,...). Rate parameter l represents the typical # of events per time interval ( l > 0 ).
DataCamp Bayesian Modeling with RJAGS Poisson regression Y ∼ Pois( l ) where l > 0 i i i
DataCamp Bayesian Modeling with RJAGS Poisson regression Y ∼ Pois( l ) where l > 0 i i i l = a + bX + cZ i i i
Recommend
More recommend