kingcounter36
kingcounter36

Reputation: 36

Undefined variable in BUGS with missing values

I'm running a BUGS model through R and I'm having a problem with BUGS saying one of my data sets is an 'undefined variable'. The data set it is having problems with has quite a few NA's in it, but why is this causing problems? It works just fine when the NA's are replaced by 0's, so I know there is a problem with BUGS using the NAs. Using 0's instead of NA's is not an acceptable solution because I need to log transform the resulting quantity. Has anyone run into something similar and found a solution?

Here is the part of the model that I am struggling with:

rm(list=ls(all=T))
library(R2OpenBUGS)

#set working directory separately, I name it here
 working_dir=getwd()

mod=function(){
  tau.cat~dgamma(0.001, 0.001)
  q.unr~dnorm(0, 1E-10)

  for(y in 1:nyears){
   log.N[y]~dnorm(0, 1E-10)
   N[y]<-exp(log.N[y])
   }

  for(w in 1:4){
   for(y in 1:nyears){
   #generating estimated catch
   est.c.cat.unr[y,w]<-N[y]*(1-exp(-q.unr*c.eff.unr[y,w]))
   #fitting it to data
   c.cat.unr[y,w]~dnorm(est.c.cat.unr[y,w], tau.cat)
  }
 }
}

#write model to a .txt file to pass to BUGS
write.model(mod, paste(working_dir, '/model_file.txt', sep=""))

dat=list(
 nyears=38,
 c.cat.unr=c.cat.unr,
 c.eff.unr=c.eff.unr)

#creating initial values for each chain
init1=list(
 log.N=log(rep(180000, 38)),
 q.unr=1E-5,
 tau.cat=.0001)

init2=list(
 log.N=log(rep(250000, 38)),
 q.unr=1E-6,
 tau.cat=.001)

inits=list(init1, init2)

#pass the model to BUGS; debug=T will keep the BUGS window open so you can see errors
sim=bugs(data=dat, 
     inits=inits, 
     parameters.to.save=c("N"), 
     n.iter=1000,
     n.chains=2,
     n.burnin=500,
     n.thin=1,
     model.file="model_file.txt",
     debug=T,
     codaPkg=T,
     working.directory=working_dir)

 ######DATA######

          ##c.cat.unr##

      chw.3 chw.4 chw.5 chw.6
 [1,]    NA 20010    NA    NA
 [2,] 12458 16227    NA    NA
 [3,] 18483 10066    NA    NA
 [4,] 24633    NA    NA    NA
 [5,]  9891    NA    NA    NA
 [6,] 29882    NA    NA    NA
 [7,]  4912 24628    NA    NA
 [8,] 13406    NA    NA    NA
 [9,]    NA 17181    NA    NA
[10,]    NA    NA    NA    NA
[11,]    NA    NA    NA    NA
[12,]    NA    NA    NA    NA
[13,]    NA    NA    NA    NA
[14,]    NA    NA    NA    NA
[15,]    NA    NA    NA    NA
[16,]    NA    NA    NA    NA
[17,]    NA    NA    NA    NA
[18,]    NA    NA    NA    NA
[19,]    NA    NA    NA    NA
[20,]    NA    NA    NA    NA
[21,]    NA    NA    NA    NA
[22,]    NA    NA    NA    NA
[23,]    NA    NA    NA    NA
[24,]    NA    NA    NA    NA
[25,]    NA    NA    NA    NA
[26,]    NA    NA    NA    NA
[27,]    NA    NA    NA    NA
[28,]    NA    NA    NA    NA
[29,]    NA    NA    NA    NA
[30,]    NA    NA    NA    NA
[31,]    NA    NA    NA    NA
[32,]    NA    NA    NA    NA
[33,]    NA    NA    NA    NA
[34,]    NA    NA    NA    NA
[35,]    NA    NA    NA    NA
[36,]    NA    NA    NA    NA
[37,]    NA    NA    NA    NA
[38,]    NA    NA    NA    NA


         ##c.eff.unr##

      cew.3 cew.4 cew.5 cew.6
 [1,]    NA  5724    NA    NA
 [2,]  2802  2904    NA    NA
 [3,]  3972  2004    NA    NA
 [4,]  6432    NA    NA    NA
 [5,]  2814    NA    NA    NA
 [6,]  6180    NA    NA    NA
 [7,]  2784  5970    NA    NA
 [8,]  5634    NA    NA    NA
 [9,]    NA  5562    NA    NA
[10,]    NA    NA    NA    NA
[11,]    NA    NA    NA    NA
[12,]    NA    NA    NA    NA
[13,]    NA    NA    NA    NA
[14,]    NA    NA    NA    NA
[15,]    NA    NA    NA    NA
[16,]    NA    NA    NA    NA
[17,]    NA    NA    NA    NA
[18,]    NA    NA    NA    NA
[19,]    NA    NA    NA    NA
[20,]    NA    NA    NA    NA
[21,]    NA    NA    NA    NA
[22,]    NA    NA    NA    NA
[23,]    NA    NA    NA    NA
[24,]    NA    NA    NA    NA
[25,]    NA    NA    NA    NA
[26,]    NA    NA    NA    NA
[27,]    NA    NA    NA    NA
[28,]    NA    NA    NA    NA
[29,]    NA    NA    NA    NA
[30,]    NA    NA    NA    NA
[31,]    NA    NA    NA    NA
[32,]    NA    NA    NA    NA
[33,]    NA    NA    NA    NA
[34,]    NA    NA    NA    NA
[35,]    NA    NA    NA    NA
[36,]    NA    NA    NA    NA
[37,]    NA    NA    NA    NA
[38,]    NA    NA    NA    NA

There is quite a bit more to the model, but the rest runs just fine. I have cut out the rest of the model and included only the part that is giving me problems. I have run this section of the model and I get the same "c.eff.unr is an undefined variable" error

Upvotes: 1

Views: 1079

Answers (1)

Chris Jackson
Chris Jackson

Reputation: 871

If there are NAs in a variable, and you then use that variable in the definition of a second variable, then BUGS will complain because it can't do calculations with NAs. You could specify a model in BUGS for how the c.eff.unr are generated. The parameters of that model will be estimated from the observed values, and it will be treated as a prior for the missing values, so BUGS will impute values for the NAs. Alternatively, you could format your data to exclude missing values, given there are a lot of them.

Upvotes: 0

Related Questions