[英]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.尝试volume
铁路小道上骑自行车的人数量比weekday
少。 RailTrail
from mosaicData
contains data collected by the Pioneer Valley Planning Commission on the usage of a local rail-trail.来自mosaicData
RailTrail
先锋谷规划委员会收集的有关当地铁路使用情况的数据。 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).对于 90 天中的每一天,他们记录了铁路运输volume
(用户数量)以及是否是weekday
(如果是,则为 TRUE,否则为 FALSE)。
Model Model
Yi = trail volume (# of users) on day i Yi = 第 i 天的跟踪量(用户数)
Xi = 1 for weekdays, 0 for weekends. Xi = 1 表示工作日,0 表示周末。
Likelihood可能性
Priors先验
Code代码
Trying to implement this in R as follows:尝试在 R 中实现此功能,如下所示:
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:尝试使用以下代码编译上面的 model:
# 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.我在 Ubuntu 20.04、MacOS Catalina 以及 RStudio Cloud 中对此进行了测试——同样的错误。 rjags.version()
is 4.3.0
. rjags.version()
是4.3.0
。
as shared by @user20650 :由@user20650分享:
The code works with using explicit X = factor(RailTrail$weekday))
in the compile statement, ie,该代码适用于在编译语句中使用显式X = factor(RailTrail$weekday))
,即
# 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)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.