Error when predicting and plotting from brm model

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:

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

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

or

plot(conditional_effects(mod, effects = "day:treatment"))

I have only found one other post 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?

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.

Hello,

None of those apply to my data. I have been trying to solve this using code I found in this post, which I understand did work in the past for the poster hmeleiro. Here’s the dataset for it too.
spain_data.csv (21.0 KB)

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:

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?

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

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

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.

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:

install.packages("brms", type="source")
3 Likes

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

1 Like

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:

install.packages("brms")
install.packages("rstan")