# Error when predicting and plotting from brm model

**URL:** https://discourse.mc-stan.org/t/error-when-predicting-and-plotting-from-brm-model/35375
**Category:** brms
**Created:** [June 4, 2024, 10:36pm UTC](https://discourse.mc-stan.org/t/error-when-predicting-and-plotting-from-brm-model/35375 "2024-06-04T22:36:01Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Alejandra\_Quinones](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/alejandra_quinones/32/17348_2.png) [@Alejandra\_Quinones](https://discourse.mc-stan.org/u/Alejandra_Quinones)
#### Post date: [June 4, 2024, 10:36pm UTC](https://discourse.mc-stan.org/t/error-when-predicting-and-plotting-from-brm-model/35375/1 "2024-06-04T22:36:01Z")

</div>

I’m trying to plot growth curves I obtained after running a Gompertz model. The models ran smoothly, but when I try to either predict values or get conditional effects, I get the following error:

```no-highlight
Error in tcrossprod(b, X) : "tcrossprod" is not a BUILTIN function
Error: Something went wrong (see the error message above). Perhaps you transformed numeric variables to factors or vice versa within the model formula? If yes, please convert your variables beforehand. Or did you set a predictor variable to NA?

```

This same message happens if I run

```no-highlight
pred <- as.data.frame(predict(mod, probs = c(0.01, 0.99), re_formula = NULL))

```

or

```no-highlight
plot(conditional_effects(mod, effects = "day:treatment"))

```

I have only found [one other post](https://stackoverflow.com/questions/77419831/error-when-trying-to-plot-conditional-effects-of-brm-fit/78577952?noredirect=1#comment138531283_78577952) about this error saying that these functions used to work with a previous version of brms, it might be a problem with the most recent one (v 2.21.0).

Any ideas?

---

<div class="post-metadata">

### Author: ![zacho](https://avatars.discourse-cdn.com/v4/letter/z/ccd318/32.png) [@zacho](https://discourse.mc-stan.org/u/zacho)
#### Post date: [June 6, 2024, 9:49pm UTC](https://discourse.mc-stan.org/t/error-when-predicting-and-plotting-from-brm-model/35375/2 "2024-06-06T21:49:30Z")

</div>

Hi @Alejandra_Quinones , does anything in that first error message apply to your data? (The bits about factors, conversions within the model formula, NA’s?) Can you share anything about your data so we can diagnose? Hard to tell without a reprex.

---

<div class="post-metadata">

### Author: ![Alejandra\_Quinones](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/alejandra_quinones/32/17348_2.png) [@Alejandra\_Quinones](https://discourse.mc-stan.org/u/Alejandra_Quinones)
#### Post date: [June 10, 2024, 2:58pm UTC](https://discourse.mc-stan.org/t/error-when-predicting-and-plotting-from-brm-model/35375/3 "2024-06-10T14:58:31Z")

</div>

Hello,

None of those apply to my data. I have been trying to solve this using code I found in [this post](https://discourse.mc-stan.org/t/hierarchical-gompertz-model/13724/81), which I understand did work in the past for the poster hmeleiro. Here’s the dataset for it too.  
[spain\_data.csv](https://discourse.mc-stan.org/uploads/short-url/jsanfgTgc0uBKHgECPniawwKDQG.csv) (21.0 KB)

```no-highlight
library(tidyverse)
library(brms)

raw <- read_csv("spain_data.csv")

formula <- casos ~ A * exp( -exp( -(k * (days - delay) ) ) )

form_mult <- bf(formula,
                A ~ 1 + (1 | ccaa),
                k ~ 1 + (1 | ccaa),
                delay ~ 1 + (1 | ccaa), 
                nl = TRUE)

priors <- c(
  prior(normal(0, 13000), nlpar = "A", lb=200),
  prior(normal(.1, .05), nlpar = "k", lb=0),
  prior(normal(5, 20), nlpar = "delay", lb=0),
  prior(student_t(3, 0, 5000), class = "sigma"),
  prior(student_t(4, 3000, 5000), class = "sd", group = "ccaa", nlpar = "A"),
  prior(student_t(4, 0, 0.03), class = "sd", group = "ccaa", nlpar = "k")
)

mod <- brm(form_mult, 
           data = raw,
           prior = priors, 
           seed = 1234,
           family = gaussian("identity"),
           iter = 4000,
           chains = 4, 
           cores=4, 
           sample_prior = "no", 
           control = list(adapt_delta = 0.99,
                          max_treedepth = 12))

# Fitted
pred <- as.data.frame(predict(mod, probs = c(0.01, 0.99), re_formula = NULL))

colnames(pred)[3] <- "low"
colnames(pred)[4] <- "upp"
pred$ccaa <- raw$ccaa
pred$days <- raw$days
pred$observed <- raw$casos
pred$pob <- raw$pob
pred$low[pred$low < 0] <- 0

pred %>% 
  ggplot(aes(x = days, y = observed)) + 
  geom_point(aes(color = dentro), size = 0.4) +
  geom_ribbon(aes(ymin = low, ymax = upp ), alpha = 0.3) +
  facet_wrap(~ccaa, scales = "free_y") 

```

The error happens when I call the predict function:

```no-highlight
Error in tcrossprod(b, X) : "tcrossprod" is not a BUILTIN function
Error: Something went wrong (see the error message above). Perhaps you transformed numeric variables to factors or vice versa within the model formula? If yes, please convert your variables beforehand. Or did you set a predictor variable to NA?

```

---

<div class="post-metadata">

### Author: ![zacho](https://avatars.discourse-cdn.com/v4/letter/z/ccd318/32.png) [@zacho](https://discourse.mc-stan.org/u/zacho)
#### Post date: [June 11, 2024, 5:56pm UTC](https://discourse.mc-stan.org/t/error-when-predicting-and-plotting-from-brm-model/35375/4 "2024-06-11T17:56:57Z")

</div>

Hmm. I’m not getting that error, got the model to run (edit: and the `predict` call worked) on my machine. It could be package version issues. My versions (windows 10) are:

- R v 4.3.2
- rstan 2.32.6 (Stan version 2.32.2)
- brms 2.21.0

Maybe share your system specs and someone could help (if you’re up-to-date already).

Also, some of that code after the `brm` call gives me errors because of missing information (e.g. `raw$pob` doesn’t exist).

---

<div class="post-metadata">

### Author: ![Alejandra\_Quinones](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/alejandra_quinones/32/17348_2.png) [@Alejandra\_Quinones](https://discourse.mc-stan.org/u/Alejandra_Quinones)
#### Post date: [June 11, 2024, 7:02pm UTC](https://discourse.mc-stan.org/t/error-when-predicting-and-plotting-from-brm-model/35375/5 "2024-06-11T19:02:39Z")

</div>

Thanks for the reply @zacho, here’s some info on my session:

R version 4.3.2 (2023-10-31)  
Platform: aarch64-apple-darwin20 (64-bit)  
Running under: macOS Sonoma 14.4.1

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.3-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0

locale:  
[1] en\_US.UTF-8/en\_US.UTF-8/en\_US.UTF-8/C/en\_US.UTF-8/en\_US.UTF-8

time zone: America/Chicago  
tzcode source: internal

other attached packages:  
[1] rstan\_2.32.6 brms\_2.21.0 tidyverse\_2.0.0

---

<div class="post-metadata">

### Author: ![zacho](https://avatars.discourse-cdn.com/v4/letter/z/ccd318/32.png) [@zacho](https://discourse.mc-stan.org/u/zacho)
#### Post date: [June 12, 2024, 1:42pm UTC](https://discourse.mc-stan.org/t/error-when-predicting-and-plotting-from-brm-model/35375/6 "2024-06-12T13:42:47Z")

</div>

I’m hoping someone else sees this thread because I sure don’t know why you would get an error. I’m gonna try running the example again later this afternoon when I have access to a linux machine (e.g. I wonder if it’s the Matrix products somehow).  
Two experiments you could try:

- What if you didn’t specify `sample_prior = 'no'`?
- What if you used `backend = 'cmdstanr'` ?

No idea if those will change anything.

---

<div class="post-metadata">

### Author: ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)
#### Post date: [June 12, 2024, 2:07pm UTC](https://discourse.mc-stan.org/t/error-when-predicting-and-plotting-from-brm-model/35375/7 "2024-06-12T14:07:49Z")

</div>

This happens with R4.3 when installing binary versions of packages built by a more recent version of R, since there were internal changes in R’s handling of some matrix functions.

Try reinstalling `brms` from source:

```r
install.packages("brms", type="source")

```

---

<div class="post-metadata">

### Author: ![Alejandra\_Quinones](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/alejandra_quinones/32/17348_2.png) [@Alejandra\_Quinones](https://discourse.mc-stan.org/u/Alejandra_Quinones)
#### Post date: [June 12, 2024, 2:25pm UTC](https://discourse.mc-stan.org/t/error-when-predicting-and-plotting-from-brm-model/35375/8 "2024-06-12T14:25:13Z")

</div>

I re-installed both brms and lme4 from source and now it’s working fine. Thank you both!

---

<div class="post-metadata">

### Author: ![desperate\_fellow](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@desperate\_fellow](https://discourse.mc-stan.org/u/desperate_fellow)
#### Post date: [June 14, 2024, 7:50pm UTC](https://discourse.mc-stan.org/t/error-when-predicting-and-plotting-from-brm-model/35375/9 "2024-06-14T19:50:42Z")

</div>

hey! I got the same error some days ago after updating r and packages. I solved it by reinstalling both brms and rstan, maybe it could work. here’s the (admittedly not too enigamtic) code:

```no-highlight
install.packages("brms")
install.packages("rstan")

```
