Accessing parameter values during run



I would like to know if there is a way to access parameter values while rstan is running.

The problem I am facing is that I have a model where some chains get stuck, and I would like to know in which regions of the parameter space that is, to be able to modify the model accordingly. Running the model with a small number, say 200, iterations usually goes well, but when I increase the number of iterations I regularly get one of four chains stuck and I have no choice but to cancel the run. I would like to at least know where the chain(s) get stuck in order to adapt my model.

Is there any temporary output file?
Or is there a way to output parameter values to the console every X iterations?



You would have had to specify the sample_file argument to stan or sampling and then call rstan::read_stan_csv on each chain’s output individually in another R process.


When doing this while stan has not terminated I get the following warning and can’t access the samples using read_stan_csv:

s <- read_stan_csv("temp_samples_2.csv")

Warning messages:
1: In FUN(X[[i]], ...) : line with "Elapsed Time" not found
2: In read_stan_csv("temp_samples_2.csv") :
  the number of iterations after warmup found (-192) does not match iter/warmup/thin from CSV comments (200)

> s
Stan model 'temp_samples' does not contain samples.


I had forgotten it checks for that. You can just do

s <- read.csv("temp_samples_2.csv", comment.char = "#")

and look at it as a data.frame rather than a stanfit object.