【问题标题】:Getting WinBUGS Leuk example to work from R using R2winBUGS使用 R2winBUGS 从 R 获取 WinBUGS Leuk 示例
【发布时间】:2013-02-16 10:34:06
【问题描述】:

我正在尝试从 R 中学习使用 winBUGS,我有使用 R 的经验。我已经成功地从 R 中成功运行了一个简单的示例,没有任何问题。我一直在尝试运行 Leuk:WinBUGS 示例第 1 卷中的生存。我已经成功地从 winBUGS GUI 运行它,没有任何问题。我的问题是尽我所能(我一直在尝试和搜索几天)我无法使用 R2winBUGS 运行它。我相信这很简单。

如果我尝试在脚本中设置 inits,我收到的错误消息是

Error in bugs(data = L, inits = inits, 
      parameters.to.save = params, model.file "model.txt",  : 
      Number of initialized chains (length(inits)) != n.chains

我知道这意味着我没有初始化一些链,但我正在粘贴 winbugs 示例手册中的 inits 代码,所有其他设置在我看来与在 winBUGS GUI 上运行时相同。

如果我尝试 inits=NULL,我会收到另一条错误消息

    display(log)
check(C:/BUGS/model.txt)
model is syntactically correct
data(C:/BUGS/data.txt)
data loaded
compile(1)
model compiled
gen.inits()
shape parameter (r) of gamma dL0[1] too small -- cannot sample
thin.updater(1)
update(500)
command #Bugs:update cannot be executed (is greyed out)
set(beta)

这表明我在解决第一个问题后仍然会遇到问题! 我即将放弃使用winBUGS,请有人救救我吗? 我知道我可能会看起来很愚蠢,但每个人都必须学习:-)

我在 Windows XP 2002 SP3 上使用 winBUGS 1.4.3

我的 R 代码如下,非常感谢您至少阅读到这里。

   rm(list = ls())

L<-list(N = 42, T = 17, eps = 1.0E-10,
        obs.t = c(1, 1, 2, 2, 3, 4, 4, 5, 5, 8, 8, 8, 8, 11, 11, 12, 12, 15, 17, 22, 23, 6, 
        6, 6, 6, 7, 9, 10, 10, 11, 13, 16, 17, 19, 20, 22, 23, 25, 32, 32, 34, 35),
        fail = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0),
        Z = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
               -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5),
        t = c(1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 15, 16, 17, 22, 23, 35))

### 5.4. Analysis using WinBUGS
library(R2WinBUGS)      # Load the R2WinBUGS library CHOOSE to use WinBUGS
    #library(R2OpenBUGS)        # Load the R2OpenBUGS library CHOOSE to use OpenBUGS
setwd("C://BUGS") 

# Save BUGS description of the model to working directory
sink("model.txt")
cat("

model
{
# Set up data
for(i in 1:N) {
for(j in 1:T) {

# risk set = 1 if obs.t >= t
Y[i,j] <- step(obs.t[i] - t[j] + eps)
# counting process jump = 1 if obs.t in [ t[j], t[j+1] )
# i.e. if t[j] <= obs.t < t[j+1]
dN[i, j] <- Y[i, j] * step(t[j + 1] - obs.t[i] - eps) * fail[i]
}
}
# Model
for(j in 1:T) {
for(i in 1:N) {
dN[i, j] ~ dpois(Idt[i, j]) # Likelihood
Idt[i, j] <- Y[i, j] * exp(beta * Z[i]) * dL0[j] # Intensity
}
dL0[j] ~ dgamma(mu[j], c)
mu[j] <- dL0.star[j] * c # prior mean hazard
# Survivor function = exp(-Integral{l0(u)du})^exp(beta*z)
S.treat[j] <- pow(exp(-sum(dL0[1 : j])), exp(beta * -0.5));
S.placebo[j] <- pow(exp(-sum(dL0[1 : j])), exp(beta * 0.5));
}
c <- 0.001
r <- 0.1
for (j in 1 : T) {
dL0.star[j] <- r * (t[j + 1] - t[j])
}
beta ~ dnorm(0.0,0.000001)
}


",fill=TRUE)
sink()

params<- c("beta","S.placebo","S.treat")

inits<-list( beta = 0.0, 
     dL0 = c(1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,
             1.0,1.0,1.0,1.0,1.0,1.0, 1.0,1.0))

# MCMC settings
nc <-1                  # Number of chains
ni <- 1000              # Number of draws from posterior (for each chain)
ns<-1000 #Number of sims (n.sims)
nb <- floor(ni/2)                   # Number of draws to discard as burn-in
nt <- max(1, floor(nc * (ni - nb) / ns))# Thinning rate
Lout<-list()



# Start Gibbs sampler: Run model in WinBUGS and save results in object called out
out <- bugs(data = L, inits =inits, parameters.to.save = params, model.file = "model.txt", 
n.thin = nt, n.chains = nc, n.burnin = nb, n.iter = ni, debug = T, DIC = TRUE,digits=5,
codaPkg=FALSE, working.directory = getwd())

当我设置 nc

display(log)
check(C:/BUGS/model.txt)
model is syntactically correct
data(C:/BUGS/data.txt)
data loaded
compile(2)
model compiled
inits(1,C:/BUGS/inits1.txt)
this chain contains uninitialized variables
inits(2,C:/BUGS/inits2.txt)
this chain contains uninitialized variables
gen.inits()
shape parameter (r) of gamma dL0[1] too small -- cannot sample
thin.updater(1)
update(500)
command #Bugs:update cannot be executed (is greyed out)

【问题讨论】:

  • nc &lt;- 1 更改为 nc &lt;- 2(列表初始化的长度)将更正此错误。
  • 你好,谢谢你,我试过这个,但我认为链数是每个节点运行的 MCMC 链数,与运行的 MCMC 链数无关。当我这样做时,它确实会走得更远,但我得到日志说模型已编译但 inits(1,C:/BUGS/inits1.txt) 这个链包含未初始化的变量
  • 这个还没解决,谁能帮忙?

标签: r


【解决方案1】:

好的,我自己解决了这个问题

我改变的第一件事是我不喜欢许多变量的名称与 r 函数 't' 相同的事实,例如 因此,我将所有内容都更改为具有唯一的变量名。

其次,我没有为每条链提供唯一的起始值列表

所以 inits 被更改为

inits

现在它可以工作了。

【讨论】:

  • 感谢您回答您自己的问题,我也遇到了同样的问题。
猜你喜欢
  • 2013-01-08
  • 2015-11-04
  • 2013-04-11
  • 2014-03-10
  • 2011-11-11
  • 2018-12-28
  • 2017-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多