You can pass in the timestep and metric as arguments to sample there, so if you can get an estimate of those from a previous run and then pass them in to cmdstanr with a bunch of one sample calls.
It would be slow and awkward but might work. If this is of interest to you, let me know, and I can find some example code on how to do it. This is not something Stan was designed for though, so it’ll be a really really rough solution.
The function is here. Note that the last line says return iter_count-24;. If the header section of the sample file has changed, 24 might no longer be the correct number here.
If you have it in the same folder as your original stan file, you just need to include
functions {
int get_iter(int id);
}
id is just a number used to identify the correct sample file, in case there are multiple in the folder. (in rstan i would use the option sample_file = paste0('tmp',id,'csv') )
When I used it to incorporate uncertainty about weights into a regression model, convergence was good, there were no divergent iterations, but there were BMFI warnings.
I should also say that I myself don’t think this was a 100% good way to do things, but my model was so large (high N) and I already needed to fit each model 50 times for 50 imputed data set, and so I decided against also fitting one model per set of weights.
All this just to say that in case this is about using weights, there is a way to do this without accessing iteration numbers, which I think is preferable when computationally feasible.
I also found myself wanting to access the mcmc iteration number. I found a simple way to do it, which I share here just in case it is useful to someone. What bbbales2 suggested almost works. The key idea is to call the C++ function in the generated quantities block since such block is executed only once per mcmc iteration. We can define the following two C++ functions:
functions{
void add_iter();
int get_iter();
}
model{
print(get_iter()); // this will return the mcmc iteration number
}
generated quantities{
add_iter(); // increment the counter after each iteration
}
You place the “add_iter()” function within the generated quantities block. Then, you can call the “get_iter()” function in any of the other blocks whenever needed and this will return the iteration number.
If you’re running multiple chains and you would like to discriminate among chains, then you could use something like the C++ function “getpid()” to get the PID for each chain/process.