# Issues running kfold() after saving and reloading a brmsfit object

**URL:** <https://discourse.mc-stan.org/t/issues-running-kfold-after-saving-and-reloading-a-brmsfit-object/24878>\
**Category:** brms\
**Created:** [October 17, 2021, 5:58pm UTC](https://discourse.mc-stan.org/t/issues-running-kfold-after-saving-and-reloading-a-brmsfit-object/24878 "2021-10-17T17:58:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Herman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/herman/32/11210_2.png) [@Herman](https://discourse.mc-stan.org/u/Herman)\
**Post date:** [October 17, 2021, 5:58pm UTC](https://discourse.mc-stan.org/t/issues-running-kfold-after-saving-and-reloading-a-brmsfit-object/24878/1 "2021-10-17T17:58:52Z")

</div>

I am having an issue running brms::kfold() after running a brms fit object with cmdrstan backend (and threads), saving and then reloading from file. A reproducible example:

```
library(brms)

data("epilepsy")

m <- brm(formula = count ~ 1 + (1|patient), data = epilepsy, backend = "cmdstanr", threads = threading(2), file = "m.Rds")

#restart R or exit and reopen R
#reload brms
library(brms)
m <- readRDS("m.Rds")

kfold(m)

Fitting model 1 out of 10
Fitting model 2 out of 10
Fitting model 3 out of 10
Fitting model 4 out of 10
Fitting model 5 out of 10
Fitting model 6 out of 10
Fitting model 7 out of 10
Fitting model 8 out of 10
Fitting model 9 out of 10
Fitting model 10 out of 10
Start sampling
Error: Model not compiled. Try running the compile() method first.

```

The issue here is that my brmsfit objects must be loaded from file as they are not run on my machine.

Any tips/tricks/workarounds would be greatly appreciated!

Thanks in advance!

- Operating System: macOS catalina
- R version 4.0.3
- brms Version: 2.16.2

---

<div class="post-metadata">

**Author:** ![avehtari](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avehtari/32/5935_2.png) [@avehtari](https://discourse.mc-stan.org/u/avehtari)\
**Post date:** [October 25, 2021, 5:17pm UTC](https://discourse.mc-stan.org/t/issues-running-kfold-after-saving-and-reloading-a-brmsfit-object/24878/2 "2021-10-25T17:17:03Z")

</div>

> [@Herman](#):
>
> The issue here is that my brmsfit objects must be loaded from file as they are not run on my machine.

The `m` object stores path to the executable model binary which I assume is stored in a temporary directory and when you restart R it can’t found anymore. That hint `brms` gives you `Try running the compile() method first.` might work, but I haven’t tested. I’m pinging @paul.buerkner in case if he can confirm whether it is possible to re-compile the model based on the information available in the brms object.
