首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WINBUGS -代码中的错误

WINBUGS -代码中的错误
EN

Stack Overflow用户
提问于 2014-07-22 18:29:33
回答 1查看 634关注 0票数 1

我正在尝试学习WINBUGS,并尝试构建一个小模型,根据教科书中的一个示例(以下代码)进行调整,该模型假设受感染的携带者有一个隐藏的群体,随着时间的推移,增长率("R0")和去除率(筛选和治疗)都会增加。但是,我倾向于获得一系列错误消息(“无效或意外扫描的令牌”、"inits无法执行“等)。因此,如果我对WINBUGS的理解犯了愚蠢的错误,有更多的WINBUGS经验的人会对眼球这么好吗?我特别不确定在WINBUGS中是否可以连续更新人口(N.estt+1 <- N.estt + newcases obs)?事先非常感谢

代码语言:javascript
复制
set.seed(1)
n.years <- 10           # Number of years
N1 <- 30                # Initial population size
rnode <- 0.3
attendance <- 0.4

#generating some data. the model has a hidden population of infected carriers that grows, and is screened periodically (with x% attendance). screened individuals are removed from the population. the observed cases are used in the bayesian framework (BUGS) to infer the hidden parameters


obs <- N <- newcases <- numeric(n.years)
N[1] <- N1
for (t in 1:(n.years-1)){
  newcases[t] <- rbinom(n=1,size=N[t],prob=rnode) #
  obs[t] <- rbinom(1, N[t], attendance) #observed cases
  N[t+1] <- N[t] + newcases[t] - obs[t] }

# Specifying model in BUGS language
sink("ssm.bug")
cat("
    model { 
    # Priors and constraints
    N.est[1] ~ dbin(0.3, 100)    # Prior for initial population size
    rnode ~ dunif(0, 1)          # Prior for R0
    attendance ~ dunif(0, 1)     # Prior for attendance at screening

    # Likelihood
    # State process
    for (t in 1:(T-1)){
    newcases ~ dbin(rnode,N.est[t])
    obs ~ dbin(attendance,N.est[t])
    N.est[t+1] <- N.est[t] + newcases - obs    
    }
    ",fill = TRUE)
sink()

# Bundle data
bugs.data <- list(obs = obs, T = n.years)

# Initial values
inits <- function(){list(rnode=0.5,attendance=0.5, N.est = c(runif(1, 20, 40), rep(NA, (n.years-1))))} 

# Parameters monitored
parameters <- c("rnode", "attendance", "N.est")

# MCMC settings
ni <- 25000
nt <- 3
nb <- 10000
nc <- 3

# Call WinBUGS from R (BRT <1 min)
ssm <- bugs(bugs.data, inits, parameters, "ssm.bug", n.chains = nc, n.thin = nt, n.iter = ni, n.burnin = nb, debug = TRUE, bugs.directory = bugs.dir, working.directory = getwd())

# Define function to draw a graph to summarize results
graph.ssm <- function(ssm, N, y){
  fitted <- lower <- upper <- numeric()
  n.years <- length(y)
  for (i in 1:n.years){
    fitted[i] <- mean(ssm$sims.list$N.est[,i])
    lower[i] <- quantile(ssm$sims.list$N.est[,i], 0.025)
    upper[i] <- quantile(ssm$sims.list$N.est[,i], 0.975)}
  m1 <- min(c(y, fitted, N, lower))
  m2 <- max(c(y, fitted, N, upper))
  par(mar = c(4.5, 4, 1, 1), cex = 1.2)
  plot(0, 0, ylim = c(m1, m2), xlim = c(0.5, n.years), ylab = "Population size", xlab = "Year", las = 1, col = "black", type = "l", lwd = 2, frame = FALSE, axes = FALSE)
  axis(2, las = 1)
  axis(1, at = seq(0, n.years, 5), labels = seq(0, n.years, 5))
  axis(1, at = 0:n.years, labels = rep("", n.years + 1), tcl = -0.25)
  polygon(x = c(1:n.years, n.years:1), y = c(lower, upper[n.years:1]), col = "gray90", border = "gray90")
  points(N, type = "l", col = "red", lwd = 2)
  points(y, type = "l", col = "black", lwd = 2)
  points(fitted, type = "l", col = "blue", lwd = 2)
  legend(x = 1, y = m2, legend = c("True", "Observed", "Estimated"), lty = c(1, 1, 1), lwd = c(2, 2, 2), col = c("red", "black", "blue"), bty = "n", cex = 1)
}

# Execute function: Produce figure 
graph.ssm(ssm, N, y)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-23 09:39:47

通常,在开始通过WinBUGS (或R2OpenBUGS)自动化之前,我建议先深入了解R2WinBUGS或OpenBUGS来调试您的模型。

在上面的模型中,我立即发现您在结尾处缺少了一个封闭的括号,即您需要

代码语言:javascript
复制
model { 
# Priors and constraints
N.est[1] ~ dbin(0.3, 100)    # Prior for initial population size
rnode ~ dunif(0, 1)          # Prior for R0
attendance ~ dunif(0, 1)     # Prior for attendance at screening

# Likelihood
# State process
for (t in 1:(T-1)){
  newcases ~ dbin(rnode,N.est[t])
  obs ~ dbin(attendance,N.est[t])
  N.est[t+1] <- N.est[t] + newcases - obs    
}
}

OpenBUGS为您提供了关于这些类型错误的相当好的认识,当您点击ModelBaySpecification->检查模型时。如果无法编译模型,光标跳到错误发生的地方(在模型代码的末尾)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24895036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档