title: 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?
body: <p>In the R script <code>Fit12_for_stack.R</code>, I call <code>rstan</code> package's <code>stan()</code> function. When I run the <code>Fit12_for_stack.R</code> code in an interactive R session, I get these warning messages from <code>stan()</code>:</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>When I run the script <code>Fit12_for_stack.R</code> on the command line with the command:</p>
  
  <pre><code>Rscript Fit12_for_stack.R 
  </code></pre>
  
  <p>I get output, but not the warning messages. <strong>How can I capture the <code>stan()</code> warning messages when running the R script that calls <code>stan()</code> on the command line?</strong></p>
  
  <p>From the post <a href="https://stackoverflow.com/questions/7096989/how-to-save-all-console-output-to-file-in-r">How to save all console output to file in R?</a>, I tried adding </p>
  
  <pre><code>con &lt;- file("test.log")
  sink(con, append=TRUE)
  sink(con, append=TRUE, type="message")
  </code></pre>
  
  <p>to the top of the script, but <code>test.log</code> again showed output, without <code>stan()</code> warning messages.</p>
  
  <p>This is what <code>Fit12_for_stack.R</code> looks like:</p>
  
  <pre><code>con &lt;- file("test.log")
  sink(con, append=TRUE)
  sink(con, append=TRUE, type="message")
  
  library("rstan")
  
  J &lt;- 2
  L &lt;- 3
  
  X &lt;- matrix(c(98, 22, 42, 99, 68, 61), nrow = L, ncol = J)
  N &lt;- matrix(100, nrow = L, ncol = J)
  
  fit &lt;- stan(file="try8.stan",
              data=list(J, L, X, N),
              iter=100, chains=4, seed = 1)
  </code></pre>
  
  <p>This is what <code>try8.stan</code> looks like:</p>
  
  <pre><code>data{
  
     int&lt;lower=0&gt; J;
     int&lt;lower=0&gt; 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&lt;lower=0,upper=1&gt;[L] pi_vec;
  
     vector&lt;lower=0,upper=1&gt;[J] C_vec;
  
     matrix&lt;lower=0,upper=1&gt;[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);
  
          }
      }
  
  }
  </code></pre>