Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

r - jags.parallel - Error in get(name, envir = envir) : invalid first argument

When using jags.parallel, I get the 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. I have only found one thread on this topic, but there is only one speculative suggestion that does not apply nor work here.

Reproducible code, taken from Introduction to WinBUGS for 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)) ) # For 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
 for (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 to estimate
params <- c("lambda","alpha", "beta", "sigma", "maybe_overdisp")

# MCMC settings
nc <- 3     # Number of chains
ni <- 3000     # Number of draws from posterior per chain
nb <- 1000     # Number of draws to discard as burn-in
nt <- 5     # Thinning rate

require(R2jags)

# THIS WORKS FINE
out <- R2jags::jags(win.data, inits, params, "Poisson.OD.t.test.txt",
    nc, ni, nb, nt);

# THIS PRODUCES ERROR
out <- jags.parallel(win.data, inits, params, "Poisson.OD.t.test.txt",
    nc, ni, nb, nt);

# THIS ALSO PRODUCES ERROR
out <- do.call(jags.parallel, list(win.data, inits, params, "Poisson.OD.t.test.txt",
    nc, ni, nb, nt));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Jags/R had practically two problems with this line:

out <- jags.parallel(win.data, inits, params, "Poisson.OD.t.test.txt",
    nc, ni, nb, nt);

Both are related to evaluation of function parameters - he is probably not able to resolve parameters which refer to other R variables:

1) The win.data was encoded as variable names as usually for WinBUGS/Jags:

win.data <- list(C.OD = C.OD, x = as.numeric(x)-1, n = length(x))`

but jags.parallel issues the error "Error in get(name, envir = envir) : invalid first argument". He wants the data in this format:

windata <- list("C.OD", "x", "n")

Why? Don't ask me... I discovered this when reading the example of ?jags.

2) The arguments nc, ni, nb, nt in function call are a problem! If I put constants, it is OK, but references to variables are unacceptable for him. Don't ask me why. Remedy found at 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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...