Convert brmsfit to cmdstanpy?

I have some old models I fit in brms using cmdstanr as backend. But my team mostly uses Python. The brms models are numerous and quite large, so rather than refit them I’d like to convert the brmsfits to cmdstanpy fit objects. Is there an easy way to do this, eg. by exporting and processing the posterior draws?

I’ve tried exporting the draws from R like so, and then reading them into python. But this has not worked:

# Load the brms model from the .rds file
model <- readRDS(rds_file)

# Extract all parameter names from the brmsfit object
all_vars <- posterior::variables(model)
    
# Remove the random effect terms 
keep_vars <- all_vars[!grepl("^r_", all_vars)]

# Write to csv
model |> 
    posterior::as_draws_matrix(variable = keep_vars) |>
    cmdstanr::draws_to_csv(dir = 'fits')

Thanks in advance for your guidance!

You should be able to read any raw cmdstan csv files straight into cmdstanpy. Depending on what the filtering you’re doing does to the final csv files, it’s likely they’re missing the extra comments needed to be read as stan outputs. You could still read them in using e.g. pandas, but you’d lose any nice helpers from cmdstanpy if you do

This will write CSVs compatible with running standalone generated quantities but (if I recall correctly) they probably don’t have all the comments needed that @WardBrian mentioned. I’m not sure how long these particular brms models take to run, but if you can run them again and save the CSV files those should be usable by cmdstanpy. That is, if brms lets you save the original CmdStan CSV files. cmdstanr’s sample method has an output_dir argument that you can use to specify where to save them if brms will let you pass that argument. You can also set options(cmdstanr_output_dir = "path/to/directory") globally in an R session. If the modes take a while to run I would test this with a toy model first to make sure brms will use output_dir.