0

I am very new to WinBUGS so I appreciate anyone's help troubleshooting. I used the R2WinBUGS package to run my model. I had originally posted with an issue about a pop-up in WinBUGS that said "incompatible copy". I resolved this issue by shortening the file path for my model txt file. Now I have a new error in the log, which says "index out of range". I believe this means I have a for-loop that is larger than a vector or matrix. However, I can't tell where this issue is in my script. Any ideas if this truly is the issue or where the for-loop may be incorrect?

`

sink("model_null.txt")
cat("
model {
  # an outer for-loop to go through all geographic areas (n=9,731)
  for(i in 1:N){
    # an inner for-loop to go through all years (n=10)
    for(t in 1:T){
      # defining poisson likelihood of disease for all areas (bg) x year
      y[i,t] ~ dpois(mu[i,t]) 
      mu[i,t] <- theta[i,t] 
      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)
  
  # RR of average incidence in each year
  for (t in 1:T){
    temporal.RR[t] <- exp(v[t])
  }
  
  # RR for geographic areas
  for (i in 1:N){
    spatial.RR[i] <- exp(SP[i] - alpha) # remove alpha from mu.SP[i] inclusion
  }
} # end model
", fill=TRUE)
sink()

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

# Making Temporal Neighbors/Weights for car.normal function
tm.bugs <- list(tm.num=c(1,2,2,2,2,2,2,2,2,1),
                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)
)

# Making matrix for data list
y=structure(.Data=data$y, .Dim=c(9731,10))

# 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=y), # N x T matrix containing space-time CRC incidence
               nb.bugs, # spatial neighbors/weights matrix with 3 arguments for car.normal function
               tm.bugs # temporal neighbors/weights matrix with 3 arguments for car.normal function
)

z <- rep(0,N-2)

# setting initial values for chains
inits <- function(){
  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))
}

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

m.null <- bugs(model.file="C:/Documents/WinBUGS/model_null.txt",
             data = data,
             parameters = parameters,
             inits = inits,
             n.chains = 3,
             n.iter = 25000, n.burnin = 10000, n.thin = 5000,
             bugs.directory = "C:/Documents/WinBUGS14/",
             debug=TRUE)

`

Ryan Snead
  • 13
  • 3

0 Answers0