r - jags.parallel - Error in get(name, envir = envir) : invalid first argument -
when using jags.parallel
, following error:
> out <- jags.parallel(win.data, inits, params, "poisson.od.t.test.txt", + nc, ni, nb, nt); error in get(name, envir = envir) : invalid first argument
the same call using jags
function runs ok. have found one thread on topic, there 1 speculative suggestion not apply nor work here.
reproducible code, taken introduction winbugs ecologists, see chapter 14.1 (slightly modified):
set.seed(123) ### 14.1.2. data generation n.site <- 10 x <- gl(n = 2, k = n.site, labels = c("grassland", "arable")) eps <- rnorm(2*n.site, mean = 0, sd = 0.5)# normal random effect lambda.od <- exp(0.69 +(0.92*(as.numeric(x)-1) + eps) ) lambda.poisson <- exp(0.69 +(0.92*(as.numeric(x)-1)) ) # comparison c.od <- rpois(n = 2*n.site, lambda = lambda.od) c.poisson <- rpois(n = 2*n.site, lambda = lambda.poisson) ### 14.1.4. analysis using winbugs # define model sink("poisson.od.t.test.txt") cat(" model { # priors alpha ~ dnorm(0,0.001) beta ~ dnorm(0,0.001) sigma ~ dunif(0, 10) tau <- 1 / (sigma * sigma) maybe_overdisp <- mean(exp_eps[]) # likelihood (i in 1:n) { c.od[i] ~ dpois(lambda[i]) log(lambda[i]) <- alpha + beta *x[i] #+ eps[i] eps[i] ~ dnorm(0, tau) exp_eps[i] <- exp(eps[i]) } } ",fill=true) sink() # bundle data win.data <- list(c.od = c.od, x = as.numeric(x)-1, n = length(x)) # inits function inits <- function(){ list(alpha=rlnorm(1), beta=rlnorm(1), sigma = rlnorm(1))} # parameters estimate params <- c("lambda","alpha", "beta", "sigma", "maybe_overdisp") # mcmc settings nc <- 3 # number of chains ni <- 3000 # number of draws posterior per chain nb <- 1000 # number of draws discard burn-in nt <- 5 # thinning rate require(r2jags) # works fine out <- r2jags::jags(win.data, inits, params, "poisson.od.t.test.txt", nc, ni, nb, nt); # produces error out <- jags.parallel(win.data, inits, params, "poisson.od.t.test.txt", nc, ni, nb, nt); # produces error out <- do.call(jags.parallel, list(win.data, inits, params, "poisson.od.t.test.txt", nc, ni, nb, nt));
jags/r had practically 2 problems line:
out <- jags.parallel(win.data, inits, params, "poisson.od.t.test.txt", nc, ni, nb, nt);
both related evaluation of function parameters - not able resolve parameters refer other r variables:
1) win.data
encoded variable names winbugs/jags:
win.data <- list(c.od = c.od, x = as.numeric(x)-1, n = length(x))`
but jags.parallel
issues error "error in get(name, envir = envir) : invalid first argument". wants data in format:
windata <- list("c.od", "x", "n")
why? don't ask me... discovered when reading example of ?jags
.
2) arguments nc, ni, nb, nt
in function call problem! if put constants, ok, references variables unacceptable him. don't ask me why. remedy found @ strange jags.parallel error / avoiding lazy evaluation in function call.
the complete fix looks like:
out <- do.call(jags.parallel, list(names(win.data), inits, params, "poisson.od.t.test.txt", nc, ni, nb, nt));
Comments
Post a Comment