Unexpected error when running Rstan in Windows computer


Recently when I tried to run some Stan files through Rstan, after running for some time the error message is like:
Error: cannot allocate vector of size 16 Kb

Similar errors also happen when I tried to load multiple .rds files generated by Rstan through the command readRDS() in R.

May I ask that is this a problem with my computer memory capacity and how could I solve that?

Thanks so much for your help!

I’m guessing that you’re running a large model with many parameters and/or iterations? You could try reducing the number of iterations/chains of the model. Additionally, try to make use of local scoped variables if you need to construct transformations, but you’re not interested in saving the results.

Yes, the model is quite large with many parameters as well as many items defined in generated quantities. If all these parameters are of research interest so that the local scoped variables might not be a good idea, instead of using the default 4 chains with 2000 iterations per chain, may I ask that to which degree I could reduce the number of iterations/chains of the model?

Thanks so much for your help!

Hm, there are a few options I can think of.

You could try reducing the iterations to 1500, but adding thinning (try using thin=10 to start), so that the reduced iterations have still sufficiently explored the posterior.

You could also try using cmdstanr, since the estimates are written directly the disk during sampling and not stored in memory. However, you might still run into memory issues when the results are loaded into R.

If the main contributor is the generated quantities, you could also run the model without the generated quantities block, and then construct the quantities in R from the posterior. That would let you construct each quantity separately, so that you’re not running out of memory constructing all of them at once

Thanks so much for your kind reply!

May I ask that does every item defined in the generated quantities block could be constructed from the posterior?


Yes, the generated quantities block uses the quantities defined in the parameters and transformed parameters blocks, and these quantities are all returned from sampling.

If you have specific functions that you need for constructing your quantities that aren’t available in R, then you can use the expose_stan_functions() function in rstan to export the functions you need to R