Why am I getting "Error in handleRes(res) : NA" when running bugs() with syntactically correct model?

1.1k views Asked by At

I'm trying to run a bayes model through R using R2WinBugs and BRugs but running into an error which I cannot solve. I've checked that my model is syntactically correct, and this is the output from when I try running (example below):

model is syntactically correct
data loaded
model compiled
Initializing chain 1: 
model is initialized
model is already initialized
Sampling has been started ...
Error in handleRes(res) : NA
In addition: Warning message:
running command '"C:/Users/user/Documents/R/win-library/3.0/BRugs/exec/BugsHelper.exe" "C:/Users/user/AppData/Local/Temp/RtmpWweqIM" "C:/Users/user/AppData/Local/Temp/RtmpWweqIM/trash" "file32f44e4c5ab7.bug" "C:/Users/user/AppData/Local/Temp/RtmpWweqIM/cmds.txt" "1"' had status 144 

Session info:

> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-w64-mingw32/x64 (64-bit)

RStudio 0.99.441 Windows 7

Here is an example though I wasn't sure how to attach data, if you'd like the full dataset, I can copy it here, please let me know.

library(BRugs)
library(R2WinBUGS)
library(data.table)
> dat
       date mth inc ind  response
  1: Jan-10   1   1   0  100.0000
  2: Jan-10   1   2   0  103.1304
  3: Jan-10   1   3   0  106.2609
  4: Jan-10   1   4   0  109.3913
  5: Jan-10   1   5   0  112.5217
 ---                             
572: Dec-10  12  44   1 1887.4783
573: Dec-10  12  45   1 1890.6087
574: Dec-10  12  46   1 1893.7391
575: Dec-10  12  47   1 1896.8696
576: Dec-10  12  48   1 1900.0000

# set data values as individual vectors
N <- nrow(dat)
periods <- length(unique(dat$date))
real_response <- dat$response
TT <- dat$mth
P <- dat$inc

ind <- dat$ind

x_dat <- list("real_response"=real_response,"P"=P,"N"=N,"TT"=TT,"periods"=periods, "ind"=ind)

CreateModel <- function(){

    for(k in 1:N) {
        P2[k] <- P[k]*30+ind[k]*speed
    }

    for(i in 1:N) {

        real_response[i] ~ dnorm(real_responseHat[i],t_response[i])
        t_response[i]  <- pow(var_response[i],-1)
        var_response[i] <- pow(sigma,2)* mu_target * (1 - exp(-pow((P[i]/theta),
                                                                 omega[TT[i]])))
        real_responseHat[i] <- TARGET[TT[i]] * (1 - exp(-pow((P2[i]/theta),
                                                             omega[TT[i]])))
    }

    sigma ~ dgamma(0.5,0.001)
    theta ~ dgamma(0.5,0.001)

    for(j in 1:periods){
        newmu[j] <- mu_target*pow((1+adj_factor), j)
        TARGET[j] ~ dnorm(newmu[j],t_target)
        omega[j] ~ dgamma(s1,s2)
    }

    speed ~ dcat(p[])
    for(j in 1:15){
        p[j] <- 1/15
    }

    adj_factor ~ dnorm(2, 1.2)
    t_target <- pow(s_target,-2)
    s_target ~ dgamma(0.5,0.0001)
    s1 ~ dgamma(1.5,.5)
    s2 ~ dgamma(1.5,.5)
    mu_target ~ dnorm(1000,0.0001)
}

mod <- file.path(paste(getwd(), "\\stackex_model.txt", sep=""))
writeModel(CreateModel, mod)
modelCheck(mod)
# file.show(mod)

##

inits <- function() {
    list("TARGET" = rep(1000,periods), "sigma" = 5, "theta" = 15, "omega" = rep(0.5,periods),"mu_target"=1000,"s_target"=60, "s1"=1.5, "s2"=1, "speed"=3, "adj_factor"=2)
}

parms <- c("TARGET", "sigma", "theta", "omega","mu_target","s_target","s1","s2", "speed", "adj_factor")

system.time(bmod <- bugs(data=x_dat, 
                         inits=inits, parameters.to.save=parms,
                         n.iter=100, n.chains=1, n.burnin=50, 
                         n.thin=1, model.file=mod, 
                         debug=TRUE, codaPkg=FALSE,
                         bugs.directory="C:/Program Files (x86)/OpenBUGS/OpenBUGS323",
                         program="OpenBUGS"))

I'm continuing to try and debug this problem but if anyone's seen this before or has a clue as to what's happening I'd really appreciate it.

Also, the iterations and thinning are set very low so de-bugging is easier. I plan to increase these (and hopefully parallelize) once I know the model runs.

0

There are 0 answers