R スクリプトでは、パッケージの関数Fit12_for_stack.R
を呼び出します。インタラクティブな R セッションでコードを実行すると、次のような警告メッセージが表示されます。rstan
stan()
Fit12_for_stack.R
stan()
警告メッセージ: 1: ウォームアップ後に 13 回の発散遷移がありました。adapt_delta を 0.8 より大きくすると役立つ場合があります。2: サンプリングの問題を診断するためにpairs()プロットを調べます
Fit12_for_stack.R
コマンドラインで次のコマンドを使用してスクリプトを実行すると:
Rscript Fit12_for_stack.R
出力は得られますが、警告メッセージは得られません。コマンド ラインで呼び出す R スクリプトを実行しているときに、警告メッセージをキャプチャするにはどうすればよいですか?stan()
stan()
投稿からすべてのコンソール出力を 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);
}
}
}