本文介绍了jags.parallel-get(name,envir = envir)中的错误:无效的第一个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

使用jags函数的同一调用运行正常.我只发现与此主题相关的一个主题,但是只是一个投机建议,在这里不适用也不起作用.

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.

可复制的代码,摘录自面向生态学家的WinBUGS简介,请参见第14.1章(稍作修改):

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

推荐答案

Jags/R在这一行中实际上有两个问题:

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

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

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

1)win.data像WinBUGS/Jags一样被编码为变量名:

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

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

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

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

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

2)函数调用中的参数nc, ni, nb, nt有问题!如果我放置常量,那是可以的,但是对变量的引用对他来说是不可接受的.不要问我为什么.可以在奇怪的jags.parallel错误/避免在函数调用中避免延迟求值的方法中找到补救措施.

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.

完整的修复程序如下:

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

这篇关于jags.parallel-get(name,envir = envir)中的错误:无效的第一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 04:26