This is my first time running WinBUGS. My model is fairly simple, with only an intercept and two random effects. However, when I run the model I get a whole list of errors (see below). The first error says "undefined variable". I am having trouble figuring out which of my variables are undefined in the syntax. Any help is GREATLY appreciated.
Here is my R code. I'm using the R2WinBUGS package to run my 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)
Here is the full list of errors:
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)
My goal at the moment is just to run the model. Then, I will add independent variables and use a greater number of iterations. Much of this code was originally pulled from the textbook: "Modelling Spatial and Spatial-Temporal Data: A Bayesian approach". However, I'm using a different data set than the original tutorial.
The names of the car.normal variables in my data list did not match the code. For example, "adj" was "nb.bugs.adj" because of how I specified the list. I changed the names to match my model syntax and this fixed the error.