# Stanfit object size too big for memory

**URL:** <https://discourse.mc-stan.org/t/stanfit-object-size-too-big-for-memory/1112>\
**Category:** General\
**Created:** [June 30, 2017, 11:59pm UTC](https://discourse.mc-stan.org/t/stanfit-object-size-too-big-for-memory/1112 "2017-06-30T23:59:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gregory](https://avatars.discourse-cdn.com/v4/letter/g/e56c9b/32.png) [@Gregory](https://discourse.mc-stan.org/u/Gregory)\
**Post date:** [June 30, 2017, 11:59pm UTC](https://discourse.mc-stan.org/t/stanfit-object-size-too-big-for-memory/1112/1 "2017-06-30T23:59:50Z")

</div>

Hello everyone,

I have a model that has 3 individual-level parameters for 60,000 observations (~200,000 parameters in total for the full model). The resulting stanfit object ends up being 7gb (warmup = 1000, iter =1500, chains = 4). I then save the object with saveRDS(). When I reload it, however, I get a memory allocation error and R crashes. Is there any way I can reduce the size of the object so I don’t run into this problem? I’ve already prevented all of the parameters that aren’t of interest from being saved in the object (include = FALSE, pars = c(…)), but I’m out of ideas otherwise.

Thanks!

---

<div class="post-metadata">

**Author:** ![aaronjg](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/aaronjg/32/3768_2.png) [@aaronjg](https://discourse.mc-stan.org/u/aaronjg)\
**Post date:** [July 1, 2017, 1:38am UTC](https://discourse.mc-stan.org/t/stanfit-object-size-too-big-for-memory/1112/2 "2017-07-01T01:38:10Z")

</div>

Just this morning I shared some code on github to strip out extra parameters from a stanfit object.

Maybe you can use that to split it up into two or three stanfit objects?

> <https://github.com/stan-dev/rstan/issues/426>
>
> Summary:
> I often find myself capturing the entire output of all parameters, transformed parameters, and generated data when running a stan model....

---

<div class="post-metadata">

**Author:** ![Gregory](https://avatars.discourse-cdn.com/v4/letter/g/e56c9b/32.png) [@Gregory](https://discourse.mc-stan.org/u/Gregory)\
**Post date:** [July 1, 2017, 3:14pm UTC](https://discourse.mc-stan.org/t/stanfit-object-size-too-big-for-memory/1112/3 "2017-07-01T15:14:13Z")

</div>

Thanks for this Aaron.

I’ve already stripped on the extra parameters using the arguments include = FALSE and pars = c(…) when calling sampling(). I wonder if there’s a way to remove the warmup iterations?

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [July 1, 2017, 3:40pm UTC](https://discourse.mc-stan.org/t/stanfit-object-size-too-big-for-memory/1112/4 "2017-07-01T15:40:03Z")

</div>

`save_warmup = FALSE`

---

<div class="post-metadata">

**Author:** ![Gregory](https://avatars.discourse-cdn.com/v4/letter/g/e56c9b/32.png) [@Gregory](https://discourse.mc-stan.org/u/Gregory)\
**Post date:** [July 1, 2017, 3:47pm UTC](https://discourse.mc-stan.org/t/stanfit-object-size-too-big-for-memory/1112/5 "2017-07-01T15:47:23Z")

</div>

Wow. That’s easy. When I looked through the help file for sampling(), it didn’t show a save\_warmup argument.

I see now that it’s included in the R documentation for stan(). I’ve been using the stan\_model() to sampling() route. Thanks a lot Ben. This is perfect.
