jags.parallel - get(name,envir = envir)出错:第一个参数无效

时间:2013-07-23 11:13:16

标签: r winbugs jags r2jags jags.parallel

使用jags.parallel时,出现以下错误:

> 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

使用jags函数的同一调用运行正常。我只找到one thread on this topic,但只有一个推测性建议不适用,也不适用于此。

可重现代码,取自Introduction to WinBUGS for ecologists, see chapter 14.1(稍加修改):

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));

1 个答案:

答案 0 :(得分:4)

Jags / R在这一行上几乎有两个问题:

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

两者都与函数参数的评估有关 - 他可能无法解析引用其他R变量的参数:

1)win.data被编码为变量名,通常用于WinBUGS / Jags:

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

但是jags.parallel发出错误“get(name,envir = envir)中的错误:第一个参数无效”。他想要这种格式的数据:

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

为什么呢?不要问我......我在阅读?jags的例子时发现了这一点。

2)函数调用中的参数nc, ni, nb, nt是个问题!如果我把常量,它没关系,但对变量的引用对他来说是不可接受的。不要问我为什么。在strange jags.parallel error / avoiding lazy evaluation in function call找到补救措施。

完整修复如下:

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