在R脚本 Fit12_for_stack.R
中,我调用了 rstan
package的 stan()
函数 . 当我在交互式R会话中运行 Fit12_for_stack.R
代码时,我从 stan()
收到以下警告消息:
警告信息:1:预热后有13个不同的转换 . 将adapt_delta提高到0.8以上可能有所帮助 . 2:检查pair()图以诊断采样问题
当我使用命令在命令行上运行脚本 Fit12_for_stack.R
时:
Rscript Fit12_for_stack.R
我得到输出,但不是警告消息 . How can I capture the stan() warning messages when running the R script that calls stan() on the command line?
从帖子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);
}
}
}
2 回答
我尝试添加
stan(..., cores = 2)
并记录了警告消息 . 我浏览了source,我的猜测(我可能错了)是,当cores = 1
时,如果R
处于交互模式(正好在脚本的最后),则仅抛出警告 .当
cores
大于1时,sink()
似乎没有记录工作人员的输出,但重定向输出似乎有效,例如所以我删除了这些行
从
并使用该命令运行程序
这产生了一个输出文件
包括stan()警告 .