I'm running a large number of JAGS models in R using the jags
function of the R2jags package (which uses the rjags package to run JAGS).
I get a lot of warnings printed in the console:
value out of range in 'lgamma'
Printing these warnings seems to heavily impinge on computing time. How do I suppress this?
The warnings are printed as output, rather than an R warning.
Thing's I've tried that don't work include:
Wrapping my call in
try(..., silent = TRUE)
,suppressWarnings
,invisible
, orcapture.output
.Altering the
jags.model
call withinjags
tojags.model(..., quiet = TRUE)
.
The phenomenon is also noted elsewhere, I just want to shut it up to reduce computation load from squillions of unnecessary prints to console.
Any suggestions?
Here's a long but reproducible example based on an example of the same issue on sourceforge. Apologies for the length of this but I couldn't replicate it in any smaller toy models. I couldn't care less about this particular model, but it replicates the problem reasonably simply:
Model
cat('
model {
K <- 1.1
K.mvhypgeom <- exp( logfact(sum(n[])) - logfact(nMissing) - logfact( sum(n[]) - nMissing))
p ~ dunif(0,1)
for (t in 1:N) {
X.missing[t] ~ dpois( missRate )
}
nMissing ~ dsum(X.missing[1],X.missing[2],X.missing[3],X.missing[4],X.missing[5],X.missing[6],X.missing[7],X.missing[8],X.missing[9],X.missing[10])
for (t in 1:N) {
pX.missing[t] <- exp(logfact(n[t]) - logfact( X.missing[t]) - logfact( n[t] - X.missing[t]))
ones2[t] ~ dbern(pX.missing[t]/K.mvhypgeom)
}
for (t in 1:N) {
X[t] <- X.obs[t] + X.missing[t]
likX[t] <- dbin( X[t], p, n[t])
ones1[t] ~ dbern( likX[t] / K)
}
}
',
file = {example.model <- tempfile()},
sep = ''
)
Data
simBinTS <- function(n, p , nMissing) {
X.full <- X <- rbinom(N, size = n, prob = p)
for (i in seq_len(nMissing)) {
idx <- sample(1:N, size = 1, prob = X)
X[idx] <- X[idx] - 1
}
return(data.frame(n = n, X = X, X.full = X.full))
}
N <- 10
p <- 0.3
set.seed(123)
n <- rpois(N, lambda = 30)
nMissing <- 10
missRate <- 1/10
ts <- simBinTS(p = p, n = n, nMissing = nMissing)
X.obs <- ts$X
n <- ts$n
X.full <- ts$X.full
ones1 <- rep(1,N)
ones2 <- rep(1,N)
jags.inits <- function(){
list(X.missing = X.full-X.obs)
}
Call
library("R2jags")
jags(data = list("X.obs", "n", "N", "nMissing", "ones1", "ones2", "missRate"),
inits = jags.inits,
parameters.to.save = "p",
model.file = example.model,
n.chains = 3,
n.iter = 1000,
n.burnin = 500,
n.thin = 1,
progress.bar = "none")
Output (large number of repeats of warning trimmed - again these are printed as function output rather than as warning messages)
value out of range in 'lgamma'
value out of range in 'lgamma'
value out of range in 'lgamma'
value out of range in 'lgamma'
value out of range in 'lgamma'
value out of range in 'lgamma'
Inference for Bugs model at "D:\Users\fish\AppData\Local\Temp\RtmpWufTIC\file1614244456e1", fit using jags,
3 chains, each with 1000 iterations (first 500 discarded)
n.sims = 1500 iterations saved
mu.vect sd.vect 2.5% 25% 50% 75% 97.5% Rhat
p 0.331 0.027 0.280 0.312 0.330 0.348 0.388 1.006
deviance 812.379 2.761 808.165 810.345 811.941 814.103 818.729 1.007
n.eff
p 1300
deviance 670
For each parameter, n.eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor (at convergence, Rhat=1).
DIC info (using the rule, pD = var(deviance)/2)
pD = 3.8 and DIC = 816.2
DIC is an estimate of expected predictive error (lower deviance is better).
The issue is with the jags package that R2Jags relies upon.
printf
notfprintf
to display warnings. Jags doesn't send the warning tostderr
, it sends the warning to console not stderr. Hence, the R console cannot filter the warnings.R2Jags relies upon the jags application. I downloaded the jags source code from Sourceforge for
JAGS-4.3.0
, compiled and installed the library. This allowed me to trace through the code and identify thatjags
throws a warning via:src/jrmath/lgamma.c:74
viaML_ERROR(ME_RANGE, "lgamma");
this resolves through to
src/jrmath/nmath.h:138
viaMATHLIB_WARNING(msg, s);
which resolves to
src/jrmath/nmath.h:81
#define MATHLIB_WARNING(fmt,x) printf(fmt,x)
the issue here is that
printf
is used notfprint(stderr,...)
, this can be patched thus:Quick Solution:
If you wish to resolve quickly you can download the source and apply the following fix:
now you can compile and install the jags library:
with this done we can uninstall the R2jags library, reinstall it and repress stderr using R CMD with stderr redirect...
Code Example
Console Output:
Unmodified jags
Modified Jags framework
Long-Term Solution
Submit a bug and propose fix via SourceForge.