hnovice
hnovice

Reputation: 43

RJAGS compilation with categorical variable throws index out of range error

Background
Trying to model volume of bikers in a rail trail which is less for a weekday as compared to a weekend. RailTrail from mosaicData contains data collected by the Pioneer Valley Planning Commission on the usage of a local rail-trail. For each of 90 days, they recorded the rail-trail volume (number of users) and whether it was a weekday (TRUE if yes and FALSE otherwise).

Model

Yi = trail volume (# of users) on day i
Xi = 1 for weekdays, 0 for weekends.

Likelihood

Priors

Code

Trying to implement this in R as follows:

library(rjags)
library(mosaicData)

data(RailTrail)

# DEFINE the model    
rail_model_1 <- "model{
    # Likelihood model for Y[i]
    for(i in 1:length(Y)) {
      Y[i] ~ dnorm(m[i], s^(-2))
      m[i] <- a + b[X[i]]
    }

    # Prior models for a, b, s
    a ~ dnorm(400, 100^(-2))
    b[1] <- 0
    b[2] ~ dnorm(0, 200^(-2))
    s ~ dunif(0, 200)
}"

Attempting to compile the model above using the following code:

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

Error

However, I am getting the following error in my attempt to compile:

Error in jags.model(textConnection(rail_model_1), data = list(Y = RailTrail$volume,  : 
  RUNTIME ERROR:
Compilation error on line 5.
Index out of range taking subset of  b

Question

Can you please help me with what is wrong here? I tested this in Ubuntu 20.04, MacOS Catalina as well as RStudio Cloud - same error. rjags.version() is 4.3.0.

Upvotes: 1

Views: 418

Answers (1)

hnovice
hnovice

Reputation: 43

as shared by @user20650:

The code works with using explicit X = factor(RailTrail$weekday)) in the compile statement, i.e.,

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

Upvotes: 3

Related Questions