Qian Zhang
Qian Zhang

Reputation: 41

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?

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);

        }
    }

}

Upvotes: 2

Views: 1029

Answers (2)

Qian Zhang
Qian Zhang

Reputation: 41

So I removed the lines

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

from

Fit12_for_stack.R

and ran the program with the command

R CMD BATCH Fit12_for_stack.R

This produced an output file

Fit12_for_stack.Rout

with the stan() warnings included.

Upvotes: 0

Weihuang Wong
Weihuang Wong

Reputation: 13118

I tried adding stan(..., cores = 2) and the warning messages were logged. I skimmed through the source, and my guess (I could be wrong) is that when cores = 1, the warning is only thrown if R is in interactive mode (right at the very end of the script).

When cores is more than 1, sink() does not appear to log the output from the workers, but redirecting the output seems to work, e.g.

Rscript Fit12_for_stack.R  > test.Rout

Upvotes: 1

Related Questions