Brms: rename_pars with backend = "cmdstanr"

Hi,
It seems rename_pars (Rename parameters) does not work with backend = “cmdstanr”. Or perhaps I am missing something here.

It works well with default backend = “rstan”. Please see below an example.

cmdstanr version 0.2.0
cmdstan version 2.25.0
brms version 2.14.4

library(cmdstanr)
library(brms)

scode <- make_stancode(count ~ Trt, data = epilepsy)
sdata <- make_standata(count ~ Trt, data = epilepsy)

# rstan fit 
stanfit <- rstan::stan(model_code = scode, data = sdata)

# feed the Stan model back into brms
fit <- brm(count ~ Trt, data = epilepsy, empty = TRUE)
fit$fit <- stanfit
fit <- rename_pars(fit)
summary(fit)


# cmdstanr fit 
csdata <- list()
for (t in names(sdata)) {
  csdata[[t]] <- sdata[[t]]
}

cmod <- cmdstan_model(write_stan_file(scode))
cfit <- cmod$sample(data= csdata)

# feed the Stan model back into brms
fit <- brm(count ~ Trt, data = epilepsy, empty = TRUE, backend = "cmdstanr")
fit$fit <- cfit
fit <- rename_pars(fit)
summary(fit)

Error in rename_pars(fit) : 
  trying to get slot "sim" from an object (class "CmdStanMCMC") that is not an S4 object 

If you need to use math formula, use Latex syntax:

Y \sim N(\mu, \sigma)

Don’t forget to attach tags (top right of this form) for application area/class of models or other general subject areas your topic touches on.

You have to transform the cmdstanr-fit object into a stanfit object first. brms itself does it via

# transform into stanfit object for consistent output structure
out <- rstan::read_stan_csv(out$output_files())
# allow updating the model without recompilation
attributes(out)$CmdStanModel <- model

where out is initially the fitted cmdstanr model.

1 Like

Thanks Paul.
I could successfully use rename_pars but couldn’t update the model. Perhaps I didn’t understand the attributes(out)$CmdStanModel <- model

library(cmdstanr)
library(brms)

scode <- make_stancode(count ~ Trt, data = epilepsy)
sdata <- make_standata(count ~ Trt, data = epilepsy)

# cmdstanr fit 
csdata <- list()
for (t in names(sdata)) {
  csdata[[t]] <- sdata[[t]]
}

cmod <- cmdstan_model(write_stan_file(scode))
out <- cmod$sample(data= csdata, chains = 1)

# transform into stanfit object for consistent output structure
out <- rstan::read_stan_csv(out$output_files())

# allow updating the model without recompilation
# attributes(fit)$CmdStanModel <- fit



# feed the Stan model back into brms
fit <- brm(count ~ Trt, data = epilepsy, empty = TRUE, backend = "cmdstanr")
fit$fit <- out
fit <- rename_pars(fit)
summary(fit)

# following update fails 
fit2 <- update(fit, backend = "cmdstanr", recompile = F)

Start sampling
Error: Cannot coerce 'what' to a single character value.

Thanks for your help.

I am sorry, I did not provide enough context for the code. “model” is the compiled model object (before sampling) from cmdstanr obtained from running cmdstanr::cmdstan_model or by extracting it from the fitted model object. If you don’t need to update, you don’t need that part of the code.

2 Likes

Thank you for the quick reply. It’s clear now.