【发布时间】: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