以下是我将使用的模型和示例数据。我需要设置先验以生成数字的数据中有一些 NA,但这种方式可能会导致一些错误。我想知道我是否可以让 JAGS 跳过 NA,就像拥有一个具有不同行和列的矩阵一样。
NA 在ex_expectancy
和中ex_shock
。
# data
# 3 subjects * 14 trials
ex_expectancy <- structure(list(`1` = c(9L, 5L, 1L), `2` = c(5L, 6L, 1L), `3` = c(2L, 7L, 4L), `4` = c(3L, 6L, 2L), `5` = c(9L, 6L, 4L), `6` = c(9L, 7L, 1L), `7` = c(3L, 5L, 5L), `8` = c(8L, 5L, 1L), `9` = c(10L, 5L, NA), `10` = c(9L, NA, NA), `11` = c(2L, NA, NA), `12` = c(3L,NA, NA), `13` = c(3L, NA, NA), `14` = c(4L, NA, NA)), row.names = c(NA,-3L), class = c("data.table", "data.frame"))
ex_shock <- structure(list(`1` = c(0L, 1L, 1L), `2` = c(0L, 1L, 0L), `3` = c(1L, 0L, 1L), `4` = c(1L, 0L, 1L), `5` = c(0L, 1L, 1L), `6` = c(0L, 0L, 0L), `7` = c(1L, 0L, 1L), `8` = c(1L, 1L, 1L), `9` = c(0L,1L, NA), `10` = c(1L, NA, NA), `11` = c(1L, NA, NA), `12` = c(0L, NA, NA), `13` = c(1L, NA, NA), `14` = c(0L, NA, NA)), row.names = c(NA,-3L), class = c("data.table", "data.frame"))
v <- matrix(NA, nrow=3,ncol=14)
v[,1] <- 0 # first v is 0
dlist <- list(
Nsubjects = 3,
Ntrials = 14,
expectancy = ex_expectancy,
shock = ex_shock,
v=v
)
myinits <- list(list(
alpha = runif (3,0,1))) # 3 subjects
parameters <- c('alpha','v','predk','scale','c','tau')
# model
RW <- function(){
for (i in 1:Nsubjects)
{
for (j in 2:Ntrials) # for each trial
{
expectancy [i,j] ~ dnorm (scale [i] * v[i,j] + c[i],tau[i,j])
# posteiror predictive
predk [i,j] ~ dnorm (scale [i] * v[i,j] + c[i],tau[i,j])
pe [i,j-1] <- shock [i,j-1] - v [i,j-1]
v [i,j] <- v [i,j-1] + alpha [i] * pe [i,j-1]
}
}
# priors
for (i in 1: Nsubjects){
alpha [i] ~ dunif (0,1)
scale [i] ~ dunif (0,10)
c[i] ~ dunif (0,5)
for (j in 1:Ntrials){
sigma[i,j] ~ dunif (0,5)
tau [i,j] <- 1/pow(sigma [i,j],2)
}}
}
samples <- jags(dlist, inits=myinits, parameters,
model.file = RW,
n.chains=1, n.iter=1000, n.burnin=500, n.thin=1, DIC=T)