【问题标题】:How can I capture warning messages from rstan R package's stan() function, when running an R script that calls stan() on the command line?在命令行上运行调用 stan() 的 R 脚本时,如何从 rstan R 包的 stan() 函数捕获警告消息?
【发布时间】:2016-08-24 23:53:30
【问题描述】:

在 R 脚本Fit12_for_stack.R 中,我调用了rstan 包的stan() 函数。当我在交互式 R 会话中运行 Fit12_for_stack.R 代码时,我从 stan() 收到这些警告消息:

警告信息: 1:热身后有13个不同的转变。将 adapt_delta 增加到 0.8 以上可能会有所帮助。 2:检查pairs()图来诊断抽样问题

当我在命令行上使用以下命令运行脚本Fit12_for_stack.R

Rscript Fit12_for_stack.R 

我得到输出,但没有警告消息。 在命令行上运行调用stan() 的R 脚本时如何捕获stan() 警告消息?

How to save all console output to file in R?的帖子中,我尝试添加

con <- file("test.log")
sink(con, append=TRUE)
sink(con, append=TRUE, type="message")

到脚本顶部,但test.log 再次显示输出,没有stan() 警告消息。

这是Fit12_for_stack.R 的样子:

con <- file("test.log")
sink(con, append=TRUE)
sink(con, append=TRUE, type="message")

library("rstan")

J <- 2
L <- 3

X <- matrix(c(98, 22, 42, 99, 68, 61), nrow = L, ncol = J)
N <- matrix(100, nrow = L, ncol = J)

fit <- stan(file="try8.stan",
            data=list(J, L, X, N),
            iter=100, chains=4, seed = 1)

这是try8.stan 的样子:

data{

   int<lower=0> J;
   int<lower=0> L;

   // Declare arrays with integer entries.

   int X[L,J];
   int N[L,J];

}

parameters {

   // Add parameters:
   // - pi_vec = [pi_1, ..., pi_L]
   // - C_vec = [C_11, ..., C_JJ]

   vector<lower=0,upper=1>[L] pi_vec;

   vector<lower=0,upper=1>[J] C_vec;

   matrix<lower=0,upper=1>[L,J] Alpha;

}

transformed parameters {

}

model {

    for (i in 1:L) {
        pi_vec[i] ~ uniform(0,1);
    }

    for (j in 1:J) {
        C_vec[j] ~ uniform(0,1);
    }

    for (i in 1:L) {
        for (j in 1:J) {

            Alpha[i,j] ~ normal(pi_vec[i], sqrt(C_vec[j]*(pi_vec[i])*(1 - pi_vec[i]))) T[0,1];

            // For the (Like = 1) test, have X[i,j] ~ U(0,1),
            // i.e. set the likelihood's density to 1 so that posterior density = prior density.

            X[i,j] ~ uniform(0,1);

        }
    }

}

【问题讨论】:

  • 最新更新(2016 年 9 月 10 日的 2.12.1)说 rstan 现在即使不在交互模式下也会引发警告。该更改可能让您现在捕获警告。
  • 谢谢 TJ。您是否在回复这篇文章时查看了最新版本的手册,或者有没有办法在他们每次发布新手册时自动提醒?
  • 每当我更新 RStan cran.r-project.org/web/packages/rstan/NEWS时,我都会阅读软件包新闻

标签: r command-line stan rstan


【解决方案1】:

我尝试添加stan(..., cores = 2) 并记录了警告消息。我浏览了source,我的猜测(我可能是错的)是当cores = 1 时,只有当R 处于交互模式时才会引发警告(就在脚本的最后)。

cores 大于 1 时,sink() 似乎不会记录工作人员的输出,但重定向输出似乎有效,例如

Rscript Fit12_for_stack.R  > test.Rout

【讨论】:

  • 感谢您的评论!我像这样尝试了您的解决方案:(1)“cp Fit12_for_stack.R Fit12_for_stack_Wong.R”。 (2) 我删除了 Fit12_for_stack_Wong.R 中涉及 con 和 sink() 的前 3 行。 (3) 我在 stan() 调用中添加了“cores = 2”。 (4) 我运行了 Rscript Fit12_for_stack_Wong.R > test_Wong.Rout,但 stan() 警告打印到控制台而不是 test_Wong.Rout。也许如果我使用 qsub 将“Rscript Fit12_for_stack_Wong.R > test_Wong.Rout”提交到集群,qsub 命令的输出文件会捕获 stan() 警告,但我没有尝试过。
  • 当我尝试它时,我保留了与sink() 相关的行(即没有执行上面的步骤 2)。不管怎样,看来你的方法行得通,所以一切都很好!
  • 是的,当 sink() 被移除时,我对你的方法的轻微调整也有效。我做了步骤 (1)-(4) 然后 qsub -q [此处的队列名称] -j yes -e [此处的文件夹名称] -o [此处的文件夹名称] run_Wong.sh,其中 run_Wong.sh 调用“Rscript Fit12_for_stack_Wong. R",并且输出文件 run_Wong.sh.o1551988 被保存到 [此处的文件夹名称] 并带有 stan() 警告。
【解决方案2】:

所以我删除了这些行

con <- file("test.log")
sink(con, append=TRUE)
sink(con, append=TRUE, type="message")

来自

Fit12_for_stack.R

并使用命令运行程序

R CMD BATCH Fit12_for_stack.R

这产生了一个输出文件

Fit12_for_stack.Rout

包含 stan() 警告。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 2015-02-04
    • 2021-05-17
    • 2021-06-10
    • 2021-11-02
    相关资源
    最近更新 更多