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

bzzcjhmw  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(385)

这是我第一次运行WinBUGS。我的模型相当简单,只有一个截距和两个随机效应。然而,当我运行模型时,我得到了一个完整的错误列表(见下文)。第一个错误说“未定义变量”。我很难弄清楚我的哪些变量在语法中未定义。非常感谢任何帮助。
这是我的R代码,我使用R2WinBUGS包来运行我的模型。

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)

我现在的目标只是运行这个模型,然后添加自变量并使用更多的迭代次数,这段代码大部分是从教科书中摘录的:“空间和时空数据建模:一个贝叶斯方法”。然而,我使用的数据集与原来的教程不同。

von4xj4u

von4xj4u1#

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

相关问题