This might already be out there so if it is sorry for making a duplicate post but is there a preferred way to generate quantities. I’m in the process of building a package that has Stan models in it. I initially put the generated quantities block in the Stan file for each model but then was looking at brms and other known Stan packages and saw that they often calculate these outside of the Stan block in r using functions from rstan. Is there a preferred method? It seems like the outside method is a bit more flexible?
Some advantages of using the generated quantities block:
- It can be really fast
- It can use directly analogous functions to the model block (often the same ones) to reduce the possibility of mismatch between the model and the generated quantities.
- When needed, you can use specialized Stan functions to make the computation.
Some advantages of doing it outside Stan:
- You can call functions to generate the quantities when you need them, and skip generating quantities if the user doesn’t ultimately need them.
- Since you’re using Rstan, you need to hold all the generated quantities in memory. If there are a lot of quantities to generate, the memory footprint can get unwieldy and problematic.
Thank you this is great! Considering this is for a detection probability models that are meant for larger datasets it sounds like having it Stan makes the most sense given the issue with generating quantities in R can result in memory issues.
Sorry, I think I was unclear. When using Rstan, you have to hold everything in memory. This means that memory pressure can impede model fitting itself. If this is a concern, it’s better to generate the quantities outside of Stan, so that the user has flexibility to manage memory in R as they go.
Ahhha gotcha so then give the user the ability in the R functions to generate these if they need them in R after fitting the model otherwise it can be computational costly for the user if the Stan block has generate qunatities
in it. Makes sense.
I also found this related to cmdstanr which gives some other alternatives. Thanks for your quick response and info this is super helpful!