我是JAGS的新手,正在尝试理解JAGS中用于截断数据的dinterval()函数。我正在对只有每个数据点的上下限(而不是真实值)的粗略数据进行建模。以下是我认为它应该如何工作的简单示例:
每个点的上下限:
> head(lim)
L U
[1,] 14.98266 15.68029
[2,] 21.21827 21.91590
[3,] 18.34953 19.04716
[4,] 19.00186 19.69949
[5,] 15.39891 16.09654
[6,] 17.81705 18.51468
编写模型的函数(假设数据来自具有共同均值和方差的正态分布):
playmodel <- function(){
for (i in 1:50){
is.censored[i] ~ dinterval(t[i], lim[i,])
t[i] ~ dnorm(mu,tau)
}
mu ~ dnorm(0,.001)
tau ~ dgamma(.01,.01)
}
filename <- "toymod.bug"
write.model(toymod,filename)
对于jags调用,有一些函数和赋值需要注意:
data <- list("lim"=lim)
inits <- list(mu=rnorm(1),tau=rgamma(1,.01,.01),t=as.vector(apply(lim,1,mean)))
#last part is to ensure the starting value is between the upper and lower limit
#each chain will start at the same place for t but this is just for this case
params <- c("mu","tau")
运行此模型:
playmodel.jags <- jags(data,inits, params, model.file="toymod.bug", n.chains=3,
n.iter=50000,n.burnin=30000, n.thin=1, DIC=TRUE,
working.directory=NULL,refresh = 50000/50, progress.bar = "text")
当我运行这个程序时会发生什么?
1)我的mu估计值在0附近徘徊,而实际上应该是15。
2)如果DIC=TRUE,它将无法运行:
错误:"Error in jags.samples(model, variable.names, n.iter, thin, type = "trace", : Failed to set trace monitor for node deviance"
我相信我做了一些愚蠢的事情,希望有人能帮助我找到正确的方向。