簡體   English   中英

使用分類變量進行 RJAGS 編譯會引發索引超出范圍錯誤

[英]RJAGS compilation with categorical variable throws index out of range error

背景
嘗試volume鐵路小道上騎自行車的人數量比weekday少。 來自mosaicData RailTrail先鋒谷規划委員會收集的有關當地鐵路使用情況的數據。 對於 90 天中的每一天,他們記錄了鐵路運輸volume (用戶數量)以及是否是weekday (如果是,則為 TRUE,否則為 FALSE)。

Model

Yi = 第 i 天的跟蹤量(用戶數)
Xi = 1 表示工作日,0 表示周末。

可能性

  • Yi ∼ N(mi,s^2)
  • mi =a+bXi

先驗

  • a ∼ N(400,100^2)
  • b ∼ N(0,200^2)
  • s ∼ Unif(0,200)

代碼

嘗試在 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)
}"

嘗試使用以下代碼編譯上面的 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 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

問題

你能幫我解決這里有什么問題嗎? 我在 Ubuntu 20.04、MacOS Catalina 以及 RStudio Cloud 中對此進行了測試——同樣的錯誤。 rjags.version()4.3.0

@user20650分享:

該代碼適用於在編譯語句中使用顯式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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM