Extract model runtime from already fitted model using brms with cmdstanr backend

How can I extract the model runtime from a model that was already fitted using brms with the cmdstanr backend? For instance, let’s say I load a brms model object that had been previously estimated using the cmdstanr backend. How can I find how long it took to estimate the model? When fitting the model, it shows how long it took to fit. But I’d like to extract that information from a previously estimated model.

Here’s some example code of a model that has already been estimated:

library("lme4")
library("cmdstanr")
library("brms")

data("sleepstudy", package = "lme4")

model_fit <- brm(
  Reaction ~ 1 + Days + (1 + Days | Subject), 
  data = sleepstudy,
  backend = "cmdstanr",
  threads = threading(4),
  seed  = 12345
)

Now, how can I extract the model runtime from the model_fit object?

Thanks in advance!

The rstan package has the get_elapsed_time() function for this. So you would run rstan::get_elapsed_time(model_fit$fit).

The one subtlety is that get_elapsed_time() has to be run on the stanfit object returned by stan, which actually does the model fitting. For models fit with brms, the stanfit object is stored in the fit element of the brmsfit object returned by brm. That’s why the argument to get_elapsed_time is model_fit$fit instead of just model_fit.

library("lme4")
library("cmdstanr")
library("brms")

data("sleepstudy", package = "lme4")

model_fit <- brm(
  Reaction ~ 1 + Days + (1 + Days | Subject), 
  data = sleepstudy,
  backend = "cmdstanr",
  threads = threading(4),
  seed  = 12345)

rstan::get_elapsed_time(model_fit$fit)
#>         warmup sample
#> chain:1  1.970  0.824
#> chain:2  1.967  1.019
#> chain:3  1.908  0.877
#> chain:4  1.390  0.862

Created on 2024-04-08 with reprex v2.1.0

5 Likes

This perfectly answered my question–thank you!