What is causing this rjags error: dimension mismatch?
What is causing this rjags error: dimension mismatch?
我在使用 运行 R 中的以下时间序列 JAGS 模型时遇到问题:
data(lynx)
y <- as.vector(lynx)
y
x <- 1:length(y)
library(rjags)
mod <- "model {
alpha ~ dnorm(0, 0.0001)
beta ~ dnorm(0, 0.0001)
lambda ~ dgamma(1, 1)
for (i in 2:length(y)) {
y[i] ~ dpois(lambda[i])
lambda[i] <- alpha + beta * x[i - 1]
}
}"
mod <- textConnection(mod)
samples <- jags.model(mod, data = list('x' = x, 'y' = y), n.chains = 3) #
# Error in jags.model(mod, data = list(x = x, y = y), n.chains = 3) :
# RUNTIME ERROR:
# Cannot insert node into lambda[1:114]. Dimension mismatch
有人能解释上述错误指的是什么以及如何解决它吗?
lambda
在循环中写为泊松分布的速率项,但随后在先验中将其指定为伽马分布。这导致尺寸不匹配。除此之外,您需要为泊松分布使用适当的 link 函数。
mod <- "model {
alpha ~ dnorm(0, 0.0001)
beta ~ dnorm(0, 0.0001)
for (i in 2:length(y)) {
y[i] ~ dpois(lambda[i])
log(lambda[i]) <- alpha + beta * x[i - 1]
}
}"
mod <- textConnection(mod)
# create model object
model_fit <- jags.model(mod, data = list('x' = x, 'y' = y), n.chains = 3)
# collect samples
samples <- coda.samples(model_fit, c("alpha", "beta"), n.iter = 10000)
我在使用 运行 R 中的以下时间序列 JAGS 模型时遇到问题:
data(lynx)
y <- as.vector(lynx)
y
x <- 1:length(y)
library(rjags)
mod <- "model {
alpha ~ dnorm(0, 0.0001)
beta ~ dnorm(0, 0.0001)
lambda ~ dgamma(1, 1)
for (i in 2:length(y)) {
y[i] ~ dpois(lambda[i])
lambda[i] <- alpha + beta * x[i - 1]
}
}"
mod <- textConnection(mod)
samples <- jags.model(mod, data = list('x' = x, 'y' = y), n.chains = 3) #
# Error in jags.model(mod, data = list(x = x, y = y), n.chains = 3) :
# RUNTIME ERROR:
# Cannot insert node into lambda[1:114]. Dimension mismatch
有人能解释上述错误指的是什么以及如何解决它吗?
lambda
在循环中写为泊松分布的速率项,但随后在先验中将其指定为伽马分布。这导致尺寸不匹配。除此之外,您需要为泊松分布使用适当的 link 函数。
mod <- "model {
alpha ~ dnorm(0, 0.0001)
beta ~ dnorm(0, 0.0001)
for (i in 2:length(y)) {
y[i] ~ dpois(lambda[i])
log(lambda[i]) <- alpha + beta * x[i - 1]
}
}"
mod <- textConnection(mod)
# create model object
model_fit <- jags.model(mod, data = list('x' = x, 'y' = y), n.chains = 3)
# collect samples
samples <- coda.samples(model_fit, c("alpha", "beta"), n.iter = 10000)