简体   繁体   English

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

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

When using jags.parallel , I get the following error: 使用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

The same call using jags function runs OK. 使用jags函数的相同调用运行正常。 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): 可重复使用的代码,取自生态学家的WinBUGS简介,参见第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));

Jags/R had practically two problems with this line: Jags / R几乎有两个问题:

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: 两者都与函数参数的评估有关 - 他可能无法解析引用其他R变量的参数:

1) The win.data was encoded as variable names as usually for WinBUGS/Jags: 1) win.data被编码为变量名,通常用于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". 但是jags.parallel发出错误“get(name,envir = jags.parallel的错误:第一个参数无效”。 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 . 不要问我......我在阅读“ ?jags的例子时发现了这一点。

2) The arguments nc, ni, nb, nt in function call are a problem! 2)函数调用中的参数nc, ni, nb, nt是个问题! 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 . 奇怪的jags.parallel错误/避免函数调用中的惰性求值时发现的补救措施。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从函数内部使用jags.parallel(R语言get(name,envir = envir)中的错误:未找到对象&#39;y&#39;) - Using jags.parallel from within a function (R language Error in get(name, envir = envir) : object 'y' not found) ls中的错误(envir = envir,all.names = private):R中的&#39;envir&#39;参数无效 - Error in ls(envir = envir, all.names = private) : invalid 'envir' argument in R eval(predvars,data,env)中的错误:类型为&#39;character&#39;的&#39;envir&#39;参数无效 - Error in eval(predvars, data, env) : invalid 'envir' argument of type 'character' eval(predvars,data,env)中的错误:类型为“ closure”的无效“ envir”参数 - Error in eval(predvars, data, env): invalid 'envir' argument of type 'closure' Coxph,类型为“字符”的无效“ envir”参数 - Coxph, invalid 'envir' argument of type 'character' R Shiny中类型为“ closure”的无效“ envir”参数 - invalid 'envir' argument of type 'closure' in R shiny get(as.character(FUN), mode = “function”, envir = envir) 中的错误: - Error in get(as.character(FUN), mode = “function”, envir = envir) : 获取错误(as.character(FUN),模式=“功能”,环境=环境) - Error in get(as.character(FUN), mode = “function”, envir = envir) R: get(as.character(FUN), mode = “function”, envir = envir) 中的错误: - R: Error in get(as.character(FUN), mode = “function”, envir = envir) : XLConnect&#39;envir&#39;错误 - XLConnect 'envir' error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM