代码之家  ›  专栏  ›  技术社区  ›  Scransom

抑制R中的JAGS“值超出范围”警告

  •  2
  • Scransom  · 技术社区  · 7 年前

    我在r中运行了大量jags模型,使用 jags r2jags包的功能(它使用rjags包运行jags)。

    控制台上有很多警告:

    value out of range in 'lgamma'

    打印这些警告似乎严重影响了计算时间。我该如何抑制?

    警告打印为输出,而不是R警告。

    我试过的不起作用的东西包括:

    • 结束我的通话 try(..., silent = TRUE) , suppressWarnings , invisible ,或者 capture.output .

    • 改变 jags.model 内部呼叫 贾格斯 jags.model(..., quiet = TRUE) .

    这种现象也是 noted elsewhere ,我只想关闭它以减少从不必要的打印到控制台的大量计算负载。

    有什么建议吗?


    下面是一个很长但可重复的例子,基于 an example of the same issue on sourceforge . 很抱歉,这个长度,但我不能复制它在任何较小的玩具模型。我对这个特殊的模型不太在意,但它复制了这个问题 合理的 简单地说:

    模型

    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 = ''
    )
    

    数据

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

    呼叫

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

    产量 (大量重复的警告修剪-同样,这些是作为函数输出而不是作为警告消息打印的)

    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).
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   Technophobe01    7 年前

    问题在于 jags 把它打包 R2Jags 依靠。

    • JAGS使用 printf fprintf 显示警告。jags不会把警告发送给 stderr ,它将警告发送到控制台而不是stderr。因此,R控制台无法筛选警告。

    R2JAGS 依赖于 贾格斯 申请。我从sourceforge下载了jags源代码 JAGS-4.3.0 ,编译并安装库。这让我可以追踪代码并识别 jags 通过以下方式发出警告:

    • src/jrmath/lgamma.c:74 通过 ML_ERROR(ME_RANGE, "lgamma");

    这就解决了

    • src/jrmath/nmath.h:138 通过 MATHLIB_WARNING(msg, s);

    决定

    • src/jrmath/nmath.h:81
    • #define MATHLIB_WARNING(fmt,x) printf(fmt,x)

    问题是 普林特 不使用 fprint(stderr,...) ,因此可以修补:


    快速解决方案:

    如果希望快速解决,可以下载源并应用以下修复程序:

    $ diff nmath.h.orig nmath.h
    81c81
    < #define MATHLIB_WARNING(fmt,x)        printf(fmt,x)
    ---
    > #define MATHLIB_WARNING(fmt,x)        fprintf(stderr,fmt,x)
    

    现在您可以编译并安装jags库:

    >./configure
    >sudo make uninstall && sudo make install 
    

    完成后,我们可以卸载r2jags库,重新安装它,并使用带有stderr重定向的r cmd来抑制stderr…

    R CMD ./50635735.R 2> /dev/null

    代码示例

    #!/usr/bin/env Rscript
    
    library("R2jags")
    
    source("./model.R") # Source Model
    source("./simbits.R") # Source simBinTS code...
    
    jags.data <- list("X.obs", "n", "N", "nMissing", "ones1", "ones2", "missRate")
    
    model <- jags(data = jags.data,
                  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")
    
    model
    

    控制台输出:

    未经改装的Jags

    $ R CMD ./50635735.R 2> /dev/null
    1 checking for pkg-config... /usr/local/bin/pkg-config
    2 configure: Setting compile and link flags according to pkg-config
    3 configure: Compile flags are -I/usr/local/include/JAGS
    4 configure: Link flags are -L/usr/local/lib -ljags
    5 checking for gcc... ccache clang
    6 checking whether we are using the GNU C compiler... no
    7 checking whether ccache clang accepts -g... no
    8 checking for ccache clang option to accept ISO C89... unsupported
    9 checking for jags_version in -ljags... yes
    10 checking version of JAGS library... OK
    11 configure: creating ./config.status
    12 config.status: creating src/Makevars
    13 configure: creating ./config.status
    14 config.status: creating src/Makevars
    15 config.status: creating R/unix/zzz.R
    16 ccache clang++ -I"/usr/local/Cellar/r/3.5.0_1/lib/R/include" -DNDEBUG -I/usr/local/include/JAGS   -I/usr/local/opt/gettext/include -I/usr/
    17 ccache clang++ -I"/usr/local/Cellar/r/3.5.0_1/lib/R/include" -DNDEBUG -I/usr/local/include/JAGS   -I/usr/local/opt/gettext/include -I/usr/
    18 ccache clang++ -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/loc
    19 Compiling model graph
    20    Resolving undeclared variables
    21    Allocating nodes
    22 Graph information:
    23    Observed stochastic nodes: 21
    24    Unobserved stochastic nodes: 11
    25    Total graph size: 174
    26
    27 Initializing model
    28
    29 value out of range in 'lgamma'
    30 value out of range in 'lgamma'
    31 value out of range in 'lgamma'
    32 value out of range in 'lgamma'
    ...
    ...
    ...
    10089 value out of range in 'lgamma'
    10090 Inference for Bugs model at "/var/folders/md/03gdc4c14z18kbqwpfh4jdfc0000gp/T//Rtmp3P3FrI/file868156b0697", fit using jags,
    10091  3 chains, each with 1000 iterations (first 500 discarded)
    10092  n.sims = 1500 iterations saved
    10093          mu.vect sd.vect    2.5%     25%     50%     75%   97.5%  Rhat n.eff
    10094 p          0.333   0.027   0.281   0.315   0.332   0.350   0.391 1.003   590
    10095 deviance 812.168   2.720 808.036 810.199 811.778 813.737 818.236 1.036    66
    10096
    10097 For each parameter, n.eff is a crude measure of effective sample size,
    10098 and Rhat is the potential scale reduction factor (at convergence, Rhat=1).
    10099
    10100 DIC info (using the rule, pD = var(deviance)/2)
    10101 pD = 3.6 and DIC = 815.8
    10102
    10103
    10104
    10105
    10106
    10107
    10108
    10109 BDIC is an estimate of expected predictive error (lower deviance is better).
    

    改进的jags框架

    $ R CMD ./50635735.R 2> /dev/null
    checking for pkg-config... /usr/local/bin/pkg-config
    configure: Setting compile and link flags according to pkg-config
    configure: Compile flags are -I/usr/local/include/JAGS
    configure: Link flags are -L/usr/local/lib -ljags
    checking for gcc... ccache clang
    checking whether we are using the GNU C compiler... no
    checking whether ccache clang accepts -g... no
    checking for ccache clang option to accept ISO C89... unsupported
    checking for jags_version in -ljags... yes
    checking version of JAGS library... OK
    configure: creating ./config.status
    config.status: creating src/Makevars
    configure: creating ./config.status
    config.status: creating src/Makevars
    config.status: creating R/unix/zzz.R
    ccache clang++ -I"/usr/local/Cellar/r/3.5.0_1/lib/R/include" -DNDEBUG -I/usr/local/include/JAGS   -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c jags.cc -o jags.o
    ccache clang++ -I"/usr/local/Cellar/r/3.5.0_1/lib/R/include" -DNDEBUG -I/usr/local/include/JAGS   -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c parallel.cc -o parallel.o
    ccache clang++ -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/opt/gettext/lib -L/usr/local/opt/readline/lib -L/usr/local/lib -L/usr/local/Cellar/r/3.5.0_1/lib/R/lib -L/usr/local/opt/gettext/lib -L/usr/local/opt/readline/lib -L/usr/local/lib -o rjags.so jags.o parallel.o -L/usr/local/lib -ljags -L/usr/local/opt/icu4c/lib -L/usr/local/lib -L/usr/local/Cellar/r/3.5.0_1/lib/R/lib -lR -lintl -Wl,-framework -Wl,CoreFoundation
    Compiling model graph
       Resolving undeclared variables
       Allocating nodes
    Graph information:
       Observed stochastic nodes: 21
       Unobserved stochastic nodes: 11
       Total graph size: 174
    
    Initializing model
    
    Inference for Bugs model at "/var/folders/md/03gdc4c14z18kbqwpfh4jdfc0000gp/T//RtmpI80TnH/file8e70516d6f34", 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 n.eff
    p          0.333   0.027   0.281   0.315   0.332   0.350   0.391 1.003   590
    deviance 812.168   2.720 808.036 810.199 811.778 813.737 818.236 1.036    66
    
    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.6 and DIC = 815.8
    DIC is an estimate of expected predictive error (lower deviance is better).
    

    长期解决方案

    提交一个bug并通过sourceforge建议修复。

    推荐文章