In the R script Fit12_for_stack.R
, I call rstan
package's stan()
function. When I run the Fit12_for_stack.R
code in an interactive R session, I get these warning messages from stan()
:
Warning messages: 1: There were 13 divergent transitions after warmup. Increasing adapt_delta above 0.8 may help. 2: Examine the pairs() plot to diagnose sampling problems
When I run the script Fit12_for_stack.R
on the command line with the command:
Rscript Fit12_for_stack.R
I get output, but not the warning messages. How can I capture the stan()
warning messages when running the R script that calls stan()
on the command line?
From the post How to save all console output to file in R?, I tried adding
con <- file("test.log")
sink(con, append=TRUE)
sink(con, append=TRUE, type="message")
to the top of the script, but test.log
again showed output, without stan()
warning messages.
This is what Fit12_for_stack.R
looks like:
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)
This is what try8.stan
looks like:
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);
}
}
}