Search code examples
rregressionpoissonissue-trackingrjags

Error in model compiling using RJAGS package


I´m trying to run the following Poisson Bayesian Regression Model using RJAGS, but I´m founding some issue.

library(rjags)
library(mosaic)
library(dplyr)

For this dataset from mosaic package:


glimpse(RailTrail)

I engineered a Poisson regression model of volume $Y_i$ by weekday status $X_i$ and temperature $Z_i$:

  • likelihood: $Y_i \sim Poisson(l_i)$ where $log(l_i) = a +bX_i + cZ_i$

  • priors: $a\sim N(0,200^2), b \sim N(0,2^2)$, and $c \sim N(0,2^2)$

poisson_model <- "model{
 
 # Likelihood model for Y[i]
  
  for(i in 1:length(Y)) { 
      Y[i] ~ dpois(l[i])
      log(l[i]) <- a + b[X[i]] + c*Z[i] 
}  

# Prior models for a, b, c

  a ~ dnorm(0, 200^(-2))
  b[1] <- 0
  b[2] ~ dnorm(0, 2^(-2))
  c ~ dnorm(0, 2^(-2))

}"

Then when I will compile the model

# COMPILE the model
poisson_jags <- jags.model(textConnection(poisson_model), 
    data = list(Y = RailTrail$volume, X = RailTrail$weekday, Z = RailTrail$hightemp), 
    inits = list(.RNG.name = "base::Wichmann-Hill", .RNG.seed = 10))

I found the following issue:

How can I solve this ?


Solution

  • The problem is that the weekday variable from the RailTrail data is logical, with values {FALSE, TRUE} which have numerical values of 0 and 1, respectively. You need to turn those into values 1 and 2. This should do the trick:

    poisson_jags <- jags.model(textConnection(poisson_model), 
                               data = list(Y = RailTrail$volume, 
                                           X = as.numeric(RailTrail$weekday)+1, 
                                           Z = RailTrail$hightemp), 
                               inits = list(.RNG.name = "base::Wichmann-Hill", 
                                            .RNG.seed = 10))