OpenBUGS:伯努利分布中的价值缺失 [英] OpenBUGS: missing value in Bernoulli distribution

查看:179
本文介绍了OpenBUGS:伯努利分布中的价值缺失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过R(R2OpenBUGS)使用OpenBUGS将观察值时间"建模为随机变量.如果所有观察时间都可用(没有NA),那么一切正常,但是如果我将其中一个设置为NA,则什么也不会发生.我使用WinBUGS测试了相同的代码,但出现陷阱错误"NIL取消引用(读取)".所以我的问题是我的代码中确实存在某些错误,还是我的模型对于BUGS来说太怪异了?

I'm trying to model the observation "time" as random variable with OpenBUGS via R (R2OpenBUGS). If all the observation times are available (no NA's) everything works, but if I set one of the times to NA, nothing happens. I tested the same code with WinBUGS, and I get trap error 'NIL dereference (read)'. So my question is that is there something really wrong in my code, or is my model too weird for BUGS?

我的模型是这样的:

model{
 for(i in 1:k){
  obs[i] ~ dbern(p) #is the observation done at time 1 or 2?
  y[(i-1)*2 + obs[i]+1] <- x[i]
 }    
 for(i in 1:n){    
   y[i] ~ dnorm(mu,tau) 
 }    
 mu ~ dnorm(0,0.0001)
 tau~ dgamma(0.001,0.001)  
 p ~ dunif(0,1) 
}

R代码如下:

library(R2OpenBUGS)
x<-obs<-rep(NA,5)
for(i in 1:k)
{
  obs[i]<-sample(c(0,1),1) #observation time of ith observation
  x[i]<-rnorm(1) #observed values
}

obs[2]<-NA #one of the sampling times is missing
INITS <- list(list(tau=1,mu=0,p=0.5))
DATA  <- list(x=x,n=n,k=k,obs=obs)

ob <- bugs(
  data=DATA,
  inits=INITS,
  parameters.to.save=c("tau","mu","p","y"),
  model.file="BUGSModel.R",
  n.chains=1,
  n.iter=50,
  n.burnin=10,
  n.thin=1,    
  DIC=FALSE)

推荐答案

如果我很好地理解了您的问题,则您是在问这个表达式

If I understand your question well, you are asking if this expression

obs[i] ~ dbern(p)

对于Win/OpenBUGS很奇怪,因此它不会处理缺少的值.不,我不这么认为.错误能够以这种方式处理缺失值,甚至通过后验分布来估算它们.

is weird for Win/OpenBUGS so that it will not handle the missing value. No, I don't think so; bugs is able to handle missing values this way and it even imputes them - with posterior distribution.

但是我很怀疑

y[(i-1)*2 + obs[i]+1] <- x[i]

真的很奇怪!当您强制使用观察值obs[i](它为null)来计算索引时,这可能会导致bug问题.这真的很奇怪,您应该尝试寻找另一种方法.首先尝试简化模型以跳过此规则,然后我敢打赌问题会消失.

is really weird! This could cause problems to bugs as you force to compute the index using the observation obs[i] which is null. This is really weird, you should try to find another way to do it. First try to simplify the model to skip this rule and I'd bet that the problem disappears.

这篇关于OpenBUGS:伯努利分布中的价值缺失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆