How to add Generative Quantity block in .rds file?


I am working on Bayesian analysis and save it using saveRDS(resStan, "resStan.rds") script. Unfortunately, I forgot the generative quantity block in the script. Now, I want to add it as new, therefore, is it possible to add the generative Quantity block on the resStan.rds file with out repeating the whole steps?

Although the version on CRAN apparently only works if everything in the generated quantities block is a scalar. The version on GitHub fixes this.

I have resStan.rds file and want to use the following script as GQ block. My question is, how to integrate my resStan.rds file to the following GQ?

generated quantities{
  int y_hat[N];
  for(i in 1:N) {
      int which = bernoulli_rng(pi[i]);
       y_hat[i] = poisson_rng(mu[which + 1, i]);

If you don’t want to rerun the whole model, you could extract mu from the stanfit object, then compute y_hat in R. Alternatively, use the gqs function as suggested by @bgoodri.

If what you want is to have the y_hat inside the same stanfit object that you already created, I don’t know if that’s doable, but perhaps someone will chime in with a way to do it.