# "Error: Model not compiled." when using brms::kfold with cmdstanr backend

**URL:** <https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784>\
**Category:** brms\
**Tags:** loo, cmdstanr\
**Created:** [August 3, 2021, 10:09am UTC](https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784 "2021-08-03T10:09:59Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![nkreimer](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nkreimer/32/3222_2.png) [@nkreimer](https://discourse.mc-stan.org/u/nkreimer)\
**Post date:** [August 3, 2021, 10:09am UTC](https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784/1 "2021-08-03T10:09:59Z")

</div>

I am having trouble using the `brms::kfold` function with models that have been fit with ` brms.backend = "cmdstanr"`. I get an error telling me that the model [is] not compiled. I only get this error when saving and loading models in two different R sessions.

Here is a minimal, reproducible example:

```no-highlight
# Load packages
library(tidyverse)
library(brms)

# Options
options(
  mc.cores = parallel::detectCores(),
  brms.backend = "cmdstanr"
)

# Fit models
m1_fit <- brm(
  count ~ 1 + (1|patient),
  data = epilepsy,
  family = poisson()
)
m2_fit <- brm(
  count ~ 1 + zAge + zBase * Trt + (1|patient),
  data = epilepsy,
  family = poisson()
)

# Save results
write_rds(m1_fit, "m1_fit.rds")
write_rds(m2_fit, "m2_fit.rds")

```

After running this code, I restart R and run the following code in a new R session:

```no-highlight
# Load packages
library(tidyverse)
library(brms)

# Options
options(
  mc.cores = parallel::detectCores(),
  brms.backend = "cmdstanr"
)

# Load results
m1_fit <- read_rds("m1_fit.rds")
m2_fit <- read_rds("m2_fit.rds")

# Compare models
kfold(
  m1_fit,
  m2_fit,
  chains = 1
)

```

I get the following error message:

```no-highlight
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.

```

I suspect that it has something to do with the issues described in the `cmdstanr` documentation on the `save_object` function:

> This method is a wrapper around `base::saveRDS()` that ensures that all posterior draws and diagnostics are saved when saving a fitted model object. Because the contents of the CmdStan output CSV files are only read into R lazily (i.e., as needed), the `$save_object()` method is the safest way to guarantee that everything has been read in before saving.

- Operating System: Windows 10 x64 (build 19043)
- brms Version: 2.15.0

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [August 11, 2021, 3:25am UTC](https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784/2 "2021-08-11T03:25:40Z")

</div>

Hi @nkreimer, sorry for taking a while to get to you. Can you by any chance provide a true reproducible example, by including data or code to simulate data that reproduces the issue for you? Also, just in case, you might try upgrading to the latest brms with `remotes::install_github("paul-buerkner/brms")`.

---

<div class="post-metadata">

**Author:** ![nkreimer](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nkreimer/32/3222_2.png) [@nkreimer](https://discourse.mc-stan.org/u/nkreimer)\
**Post date:** [August 11, 2021, 4:41pm UTC](https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784/3 "2021-08-11T16:41:51Z")

</div>

Hi @jsocolar, thanks for your response. Is the example I provided not reproducible? I use the `epilepsy` dataset that comes included with the `brms` package.

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [August 11, 2021, 4:55pm UTC](https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784/4 "2021-08-11T16:55:38Z")

</div>

> [@nkreimer](#):
>
> ```no-highlight
> # Load packages
> library(tidyverse)
> library(brms)
> 
> # Options
> options(
> mc.cores = parallel::detectCores(),
> brms.backend = "cmdstanr"
> )
> 
> # Fit models
> m1_fit <- brm(
> count ~ 1 + (1|patient),
> data = epilepsy,
> family = poisson()
> )
> m2_fit <- brm(
> count ~ 1 + zAge + zBase * Trt + (1|patient),
> data = epilepsy,
> family = poisson()
> )
> 
> ```

Oh jeez, I’m sorry, it is! Sorry for not running. I didn’t see `epilepsy` defined in the script and assumed it was external. Seeing if I can reproduce your behavior now.

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [August 11, 2021, 5:00pm UTC](https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784/5 "2021-08-11T17:00:14Z")

</div>

I can run your code without problems on my system, which uses the development version of brms. Can you try installing the development version of brms with `remotes::install_github('paul-buerkner/brms')` and see if the problem persists?

---

<div class="post-metadata">

**Author:** ![nkreimer](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nkreimer/32/3222_2.png) [@nkreimer](https://discourse.mc-stan.org/u/nkreimer)\
**Post date:** [August 11, 2021, 5:18pm UTC](https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784/6 "2021-08-11T17:18:03Z")

</div>

I installed the development version of brms of GitHub (2.15.9) but the problem persits.

```
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.

```

Did you restart R before running the second chunk of code?

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [August 11, 2021, 5:26pm UTC](https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784/7 "2021-08-11T17:26:13Z")

</div>

> [@nkreimer](#):
>
> Did you restart R before running the second chunk of code?

yep! And I just did it again for good measure.

I don’t know why this would be the issue, but perhaps you could also try upgrading `cmdstanr` to the development version (`remotes::install_github('stan-dev/cmdstanr')`)?

---

<div class="post-metadata">

**Author:** ![nkreimer](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nkreimer/32/3222_2.png) [@nkreimer](https://discourse.mc-stan.org/u/nkreimer)\
**Post date:** [August 11, 2021, 5:39pm UTC](https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784/8 "2021-08-11T17:39:04Z")

</div>

> [@jsocolar](#):
>
> I don’t know why this would be the issue, but perhaps you could also try upgrading `cmdstanr` to the development version ( `remotes::install_github('stan-dev/cmdstanr')` )?

How peculiar! I did that and still get the same error. For good measure, I rewrote the example so that it doesn’t depend on the `tidyverse` but am still getting the same error.

```no-highlight
# Load packages
library(brms)

# Options
options(
  mc.cores = parallel::detectCores(),
  brms.backend = "cmdstanr"
)

# Fit models
m1_fit <- brm(
  count ~ 1 + (1|patient),
  data = epilepsy,
  family = poisson()
)
m2_fit <- brm(
  count ~ 1 + zAge + zBase * Trt + (1|patient),
  data = epilepsy,
  family = poisson()
)

# Save results
saveRDS(m1_fit, "m1_fit.rds")
saveRDS(m2_fit, "m2_fit.rds")

```

```no-highlight
# Load packages
library(brms)

# Options
options(
  mc.cores = parallel::detectCores(),
  brms.backend = "cmdstanr"
)

# Load results
m1_fit <- readRDS("m1_fit.rds")
m2_fit <- readRDS("m2_fit.rds")

# Compare models
kfold(
  m1_fit,
  m2_fit,
  chains = 1
)

```

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [August 11, 2021, 8:17pm UTC](https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784/9 "2021-08-11T20:17:12Z")

</div>

@rok_cesnovar @paul.buerkner any ideas here?

---

<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 2, 2021, 12:15pm UTC](https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784/10 "2021-10-02T12:15:07Z")

</div>

Have there been any developments on this? or workarounds? I am having the exact same issue.

---

<div class="post-metadata">

**Author:** ![aisa2](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@aisa2](https://discourse.mc-stan.org/u/aisa2)\
**Post date:** [October 28, 2021, 3:50am UTC](https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784/11 "2021-10-28T03:50:55Z")

</div>

I am having a similar issue recently when using LOO with reloo = TRUE; but only on one type of multivariate model I’m running.

Environment:

```
R version 4.1.1 (2021-08-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base     

other attached packages:
 [1] posterior_1.1.0 cmdstanr_0.4.0 kableExtra_1.3.4 ggExtra_0.9 tidybayes_3.0.1 forcats_0.5.1 stringr_1.4.0 dplyr_1.0.7 purrr_0.3.4 readr_2.0.2 tidyr_1.1.4     
[12] tibble_3.1.5 ggplot2_3.3.5 tidyverse_1.3.1 brms_2.16.1 Rcpp_1.0.7      

```

Model:

```
mod.BayesNASA0_wslopes <- brm(mvbind(NASAtlx_performance_round, NASAtlx_mental_round,
                                     NASAtlx_effort_round, NASAtlx_temporal_round,
                                     NASAtlx_frustration_round, NASAtlx_physical_round) ~ 1 +
                                  scale(iblock) + (1 + scale(iblock) | p | ID),
                              data=blockData,
                              family = cumulative(link = "logit"),
                              prior = mypriorsNT, sample_prior = "yes",
                              warmup = 750, iter = 4000, chains = 2,
                              inits= "0", seed = 123,
                              backend="cmdstanr",
                              file = "./modelcache/mod.BayesNASA0_wslopes")

```

LOO code:

```
loo_nullNASA_wslopes <- loo(mod.BayesNASA0_wslopes, save_psis = TRUE, moment_match = TRUE, reloo = TRUE)

```

Traceback:

```
Error: Model not compiled. Try running the compile() method first.
27. stop(condition)
26. signalConditions(future, exclude = getOption("future.relay.immediate", 
"immediateCondition"), resignal = TRUE)
25. value.Future(futures[[j]])
24. future::value(futures[[j]])
23. reloo.brmsfit(x = .x1, loo = .x2, newdata = .x3, resp = .x4, 
k_threshold = .x5, check = .x6)
22. reloo(x = .x1, loo = .x2, newdata = .x3, resp = .x4, k_threshold = .x5, 
check = .x6)
21. eval(expr, envir, ...)
20. eval(expr, envir, ...)
19. eval2(call, envir = args, enclos = envir)
18. do_call("reloo", reloo_args)
17. .loo(x = .x1, newdata = .x2, resp = .x3, model_name = .x4, pointwise = .x5, 
k_threshold = .x6, save_psis = .x7, moment_match = .x8, reloo = .x9, 
moment_match_args = .x10, reloo_args = .x11)
16. eval(expr, envir, ...)
15. eval(expr, envir, ...)
14. eval2(call, envir = args, enclos = envir)
13. do_call(paste0(".", criterion), args)
12. .fun(criterion = .x1, pointwise = .x2, resp = .x3, k_threshold = .x4, 
save_psis = .x5, moment_match = .x6, reloo = .x7, moment_match_args = .x8, 
reloo_args = .x9, x = .x10, model_name = .x11, use_stored = .x12)
11. eval(expr, envir, ...)
10. eval(expr, envir, ...)
9. eval2(call, envir = args, enclos = envir)
8. do_call(compute_loo, args)
7. .fun(models = .x1, criterion = .x2, pointwise = .x3, compare = .x4, 
resp = .x5, k_threshold = .x6, save_psis = .x7, moment_match = .x8, 
reloo = .x9, moment_match_args = .x10, reloo_args = .x11)
6. eval(expr, envir, ...)
5. eval(expr, envir, ...)
4. eval2(call, envir = args, enclos = envir)
3. do_call(compute_loolist, args)
2. loo.brmsfit(mod.BayesNASA0_wslopes, save_psis = TRUE, moment_match = TRUE, 
reloo = TRUE)
1. loo(mod.BayesNASA0_wslopes, save_psis = TRUE, moment_match = TRUE, 
reloo = TRUE)

```

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [October 28, 2021, 6:49pm UTC](https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784/12 "2021-10-28T18:49:05Z")

</div>

I added some functionality in the github version of brms that automatically performs recompilation, if necessary. You can also avoid repeated recompilations within the same session by running `recompile_model` once on your brmsfit object.

---

<div class="post-metadata">

**Author:** ![aisa2](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@aisa2](https://discourse.mc-stan.org/u/aisa2)\
**Post date:** [October 28, 2021, 10:07pm UTC](https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784/13 "2021-10-28T22:07:55Z")

</div>

Thanks Paul; I have tried moving to the BRMS version and running `recompile_model`, but it is still giving me some problems. Though I can pull up the man page successfully with `?recompile_model`, it seems like it’s not recognizing it as a function?

```
> recompile_model(mod.BayesNASA0_wslopes, recompile = TRUE)
Error in recompile_model(mod.BayesNASA0_wslopes, recompile = TRUE) : 
  could not find function "recompile_model"

```

Traceback for the loo command also changes after installing github brms version:

```
Error in FUN(X[[i]], ...) : 
lazy-load database '/Library/Frameworks/R.framework/Versions/4.1/Resources/library/brms/data/Rdata.rdb' is corrupt
32. FUN(X[[i]], ...)
31. vapply(li, exists, NA, where = i, mode = mode, inherits = FALSE)
30. apropos(model_re, mode = "S4", ignore.case = FALSE)
29. stan_model(file, model_name = model_name, model_code = model_code, 
stanc_ret = NULL, boost_lib = boost_lib, eigen_lib = eigen_lib, 
save_dso = save_dso, verbose = verbose)
28. rstan::stan(model_code = stancode(x, threads = threading()), 
data = standata(x), chains = 0)
27. withCallingHandlers(expr, message = function(c) if (inherits(c, 
classes)) tryInvokeRestart("muffleMessage"))
26. suppressMessages(rstan::stan(model_code = stancode(x, threads = threading()), 
data = standata(x), chains = 0))
25. add_rstan_model(x)
24. update_misc_env(x)
23. loo_moment_match.brmsfit(x = .x1, loo = .x2, newdata = .x3, resp = .x4, 
k_threshold = .x5, check = .x6)
22. loo_moment_match(x = .x1, loo = .x2, newdata = .x3, resp = .x4, 
k_threshold = .x5, check = .x6)
21. eval(expr, envir, ...)
20. eval(expr, envir, ...)
19. eval2(call, envir = args, enclos = envir)
18. do_call("loo_moment_match", moment_match_args)
17. .loo(x = .x1, newdata = .x2, resp = .x3, model_name = .x4, pointwise = .x5, k_threshold = .x6, save_psis = .x7, moment_match = .x8, reloo = .x9, moment_match_args = .x10, reloo_args = .x11)
16. eval(expr, envir, ...)
15. eval(expr, envir, ...)
14. eval2(call, envir = args, enclos = envir)
13. do_call(paste0(".", criterion), args)
12. .fun(criterion = .x1, pointwise = .x2, resp = .x3, k_threshold = .x4, save_psis = .x5, moment_match = .x6, reloo = .x7, moment_match_args = .x8, reloo_args = .x9, x = .x10, model_name = .x11, use_stored = .x12)
11. eval(expr, envir, ...)
10. eval(expr, envir, ...)
9. eval2(call, envir = args, enclos = envir)
8. do_call(compute_loo, args)
7. .fun(models = .x1, criterion = .x2, pointwise = .x3, compare = .x4, resp = .x5, k_threshold = .x6, save_psis = .x7, moment_match = .x8, reloo = .x9, moment_match_args = .x10, reloo_args = .x11)
6. eval(expr, envir, ...)
5. eval(expr, envir, ...)
4. eval2(call, envir = args, enclos = envir)
3. do_call(compute_loolist, args)
2. loo.brmsfit(mod.BayesNASA0_wslopes, save_psis = TRUE, moment_match = TRUE, reloo = TRUE)
1. loo(mod.BayesNASA0_wslopes, save_psis = TRUE, moment_match = TRUE, reloo = TRUE)

```

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [October 29, 2021, 6:12am UTC](https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784/14 "2021-10-29T06:12:26Z")

</div>

perhaps R needs to be restarted again?

I realize I implemented the internal auto checking only for kfold but not for reloo. will add this today.

---

<div class="post-metadata">

**Author:** ![aisa2](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@aisa2](https://discourse.mc-stan.org/u/aisa2)\
**Post date:** [November 2, 2021, 5:00pm UTC](https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784/15 "2021-11-02T17:00:28Z")

</div>

I re-installed cmdstan and brms from the github sources, updated any other packages that could be updated, and restarted R-- not sure which one did the trick, but things seem to be working now with LOO; it recompiles without me having to call it again explicitly. Thanks Paul! @nkreimer are you still stuck with the same error?

---

<div class="post-metadata">

**Author:** ![nkreimer](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nkreimer/32/3222_2.png) [@nkreimer](https://discourse.mc-stan.org/u/nkreimer)\
**Post date:** [November 2, 2021, 6:11pm UTC](https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784/16 "2021-11-02T18:11:57Z")

</div>

I ended up circumventing the problem by running the kfold cross-validation in the same script instead of saving the intermediate results.

---

<div class="post-metadata">

**Author:** ![Maxime](https://avatars.discourse-cdn.com/v4/letter/m/a9adbd/32.png) [@Maxime](https://discourse.mc-stan.org/u/Maxime)\
**Post date:** [December 15, 2021, 4:41pm UTC](https://discourse.mc-stan.org/t/error-model-not-compiled-when-using-brms-kfold-with-cmdstanr-backend/23784/17 "2021-12-15T16:41:25Z")

</div>

Hi all,

I don’t know if there has been a follow-up with this error as to why it happens, but I also encountered it and reported it in this thread [[Multiple attemps to run k-fold cross-validation fail with brms](https://discourse.mc-stan.org/t/multiple-attemps-to-run-k-fold-cross-validation-fail-with-brms/25684)]. I tried to do the solution suggested by @nkreimer but it did not work for me and returned a new error.

Thank you and have a good day.
