繁体   English   中英

WinBUGS 错误 - 我的“未定义变量”是什么?

[英]WinBUGS Error - What is my "undefined variable"?

这是我第一次运行 WinBUGS。 我的 model 相当简单,只有一个截距和两个随机效应。 但是,当我运行 model 时,我得到了一个完整的错误列表(见下文)。 第一个错误是“未定义的变量”。 我无法确定我的哪些变量在语法中未定义。 任何帮助是极大的赞赏。

这是我的 R 代码。 我正在使用 R2WinBUGS package 来运行我的 model。

library(R2WinBUGS)
library(coda)
library(car)
library(denstrip)
library(spdep)

# Load data
load("~/final_data.Rdata")
load("~/cb_2018_42_bg.nb.Rdata")

sink("model_null.txt")
cat("

# WinBUGS code for fitting a space-time separable model with BYM (space) + RW1 (time)
model {

  # an outer for-loop to go through all areas (n=9,731)
  for (i in 1:N) {
    # an inner for-loop to go through all time points (n=10)
    for (t in 1:T) {
      # defining poisson likelihood of the outcome for all areas x time
      y[i,t] ~ dpois(mu[i,t]) # poisson likelihood for the outcome in area i at time t
      mu[i,t] <- theta[i,t] # define the mean of the poisson distribution, which is
                                 # the additive space+time structure for the outcome
      theta[i,t] <- SP[i] + v[t]
    } # close T for-loop
  } # close N for-loop

  # BYM for overall spatial component
  # a spatial ICAR on S[1:N]
  
  S[1:N] ~ car.normal(adj[],weights[],num[],prec.S)
  
  for (i in 1:N) {
    mu.SP[i] <- alpha + S[i]
    SP[i] ~ dnorm(mu.SP[i], prec.U)
  }  
  
  # RW1 for the overall temporal component
  # a temporal ICAR on v[1:T]
  v[1:T] ~ car.normal(tm.adj[],tm.weights[],tm.num[],prec.v)  
  
  # specification of vague priors
  alpha ~ dflat()
  sigma.S ~ dunif(0.0001,10)
  sigma.U ~ dunif(0.0001,10)
  sigma.v ~ dunif(0.0001,10)
  prec.S <- pow(sigma.S, -2)
  prec.U <- pow(sigma.U, -2)
  prec.v <- pow(sigma.v, -2)
  
} # end model
", fill=TRUE)
sink()

# Making Spatial Weights for car.normal function
nb.bugs <- nb2WB(cb_2018_42_bg.nb)

# Making Temporal Specifications for car.normal function
tm.bugs <- list(tm.adj=c(2,1,3,2,4,3,5,4,6,5,7,6,8,7,9,8,10,9),
                tm.weights=c(1,1,1,1,1,1,1,1,1,1),
                tm.num=c(1,2,2,2,2,2,2,2,2,1)
)

# Making matrix for data list
outcome.matrix <- matrix(final_data$outcome.var, nrow = 9731, byrow = TRUE) # vector is n=97310 and 10 time points

# Defining time and space dimensions
T <- 10
N <- 9731

# Data list for null space-time model
data <- c(list(T=T, # 10 years
             N=N, # 9,731 block groups
             y=outcome.matrix), # N x T matrix for outcome
             nb.bugs=nb.bugs, # spatial weights
             tm.bugs=tm.bugs) # temporal weights

# setting initial values for chains

inits1 <- list(alpha=rnorm(1), S=c(0.01,-0.01,z), SP=rep(rnorm(1,0,0.1),N), v=c(0.1,-0.1,0,0,0,0,0,0,0,0), sigma.S=rnorm(1,0,0.1), sigma.U=rnorm(1,0,0.1), sigma.v=rnorm(1,0,0.1))
inits2 <- list(alpha=rnorm(1), S=c(0.01,-0.01,z), SP=rep(rnorm(1,0,0.1),N), v=c(0.1,-0.1,0,0,0,0,0,0,0,0), sigma.S=rnorm(1,0,0.1), sigma.U=rnorm(1,0,0.1), sigma.v=rnorm(1,0,0.1))
inits3 <- list(alpha=rnorm(1), S=c(0.01,-0.01,z), SP=rep(rnorm(1,0,0.1),N), v=c(0.1,-0.1,0,0,0,0,0,0,0,0), sigma.S=rnorm(1,0,0.1), sigma.U=rnorm(1,0,0.1), sigma.v=rnorm(1,0,0.1))

inits<- list(inits1, inits2, inits3)

parameters <- c("alpha", "S", "SP", "v", "sigma.S", "sigma.U", "sigma.v")

m.null <- bugs(model.file="C:/file-path-to-folder/WinBUGS/model_null.txt",
             data = data,
             parameters = parameters,
             inits = inits,
             n.chains = 3,
             n.iter = 10, n.burnin = 1, n.thin = 1,
             bugs.directory = "C:/file-path-to-application/WinBUGS14/",
             debug=TRUE)

以下是错误的完整列表:

display(log)
check(C:/file-path-to-folder/WinBUGS/model_null.txt)
model is syntactically correct
data(C:/file-path-to-folder/data.txt)
undefined variable
compile(3)
inits(1,C:/file-path-to-folder/inits1.txt)
command #Bugs:inits cannot be executed (is greyed out)
inits(2,C:/file-path-to-folder/inits2.txt)
command #Bugs:inits cannot be executed (is greyed out)
inits(3,C:/file-path-to-folder/inits3.txt)
command #Bugs:inits cannot be executed (is greyed out)
gen.inits()
command #Bugs:gen.inits cannot be executed (is greyed out)
thin.updater(1)
update(1)
command #Bugs:update cannot be executed (is greyed out)
set(alpha)
command #Bugs:set cannot be executed (is greyed out)
set(S)
command #Bugs:set cannot be executed (is greyed out)
set(SP)
command #Bugs:set cannot be executed (is greyed out)
set(v)
command #Bugs:set cannot be executed (is greyed out)
set(sigma.S)
command #Bugs:set cannot be executed (is greyed out)
set(sigma.U)
command #Bugs:set cannot be executed (is greyed out)
set(sigma.v)
command #Bugs:set cannot be executed (is greyed out)
set(deviance)
command #Bugs:set cannot be executed (is greyed out)
dic.set()
command #Bugs:dic.set cannot be executed (is greyed out)
update(9)
command #Bugs:update cannot be executed (is greyed out)
coda(*,C:/file-path-to-folder/coda)
command #Bugs:coda cannot be executed (is greyed out)
stats(*)
command #Bugs:stats cannot be executed (is greyed out)
dic.stats()

DIC
history(*,C:/file-path-to-folder/history.odc)
command #Bugs:history cannot be executed (is greyed out)
save(C:/file-path-to-folder/log.odc)
save(C:/file-path-to-folder/log.txt)

我目前的目标只是运行 model。然后,我将添加自变量并使用更多的迭代次数。 大部分代码最初是从教科书中提取的:“建模空间和时空数据:贝叶斯方法”。 但是,我使用的数据集与原始教程不同。

我的数据列表中 car.normal 变量的名称与代码不匹配。 例如,由于我指定列表的方式,“adj”是“nb.bugs.adj”。 我更改了名称以匹配我的 model 语法,这修复了错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM