Saving a cmdstan model, so that cmdstan_diagnose() can be run on the loaded model

TL;DR: If I save a cmdstanr model, cmdstan_diagnose() is not available from the loaded model

Context: I am writing my cognitive modeling course notes for my students as a bookdown (I know, I know, I’ll learn quarto for next year) and I don’t want to refit all models any time I render the book. So I need to save the cmdstanr models in a way that all the functions are still available on the loaded model.

The issue. I tried several ways of saving the models, but cmdstan_diagnose() is not available for any of the saved and then loaded models. What am I missing?

Reproducible example:

## Saving and loading cmdstanr object

pacman::p_load(cmdstanr)

### Simulating a simple model (mean and sd)
n <- 30

data <- list(
  n = n,
  y = rnorm(n, 1, 2)
)

stan_model <- "
data {
  int<lower = 1> n;
  array[n] real y;
}

parameters {
  real mu;
  real<lower=0> sigma;
}

model {
  target += normal_lpdf(mu | 0, 1);
  target += normal_lpdf(sigma | 0, 1)-
    normal_lccdf(0 | 0, 1);

  target += normal_lpdf(y | mu, sigma);
}

"

write_stan_file(
  stan_model,
  dir = "",
  basename = "stan_model.stan")

file <- file.path("stan_model.stan")
mod <- cmdstan_model(file, cpp_options = list(stan_threads = TRUE),
                          stanc_options = list("O1"), 
                          pedantic = TRUE)

samples <- mod$sample(
  data = data,
  seed = 123,
  chains = 2,
  parallel_chains = 2,
  threads_per_chain = 2,
  iter_warmup = 2000,
  iter_sampling = 2000,
  refresh = 1000,
  max_treedepth = 20,
  adapt_delta = 0.99,
)

save(samples, data, file = "sampleModel.RData")
samples$save_object(file = "sampleModel.RDS")
samples$save_output_files(dir = ".", basename = "sampleModel") 
# N.B: jot down the specific output filenames saved. For me:
# sampleModel-202303212211-1-8804be.csv
# sampleModel-202303212211-2-8804be.csv

## Restart session
pacman::p_load(cmdstanr)
load("sampleModel.RData")
samples$cmdstan_diagnose()

## Restart session
pacman::p_load(cmdstanr)
samples <- readRDS("sampleModel.RDS")
samples$cmdstan_diagnose()

## Restart session
samples <- as_cmdstan_fit(c("sampleModel-202303212211-1-8804be.csv", "sampleModel-202303212211-2-8804be.csv"))
samples$cmdstan_diagnose()

The first two attempts at loading the model (load and readRDS) cannot find the csv files and therefore the cmdstan_diagnose() is not available.
The last attempt gives a “Error: This method is not available for objects created using as_cmdstan_fit().”

When you run the $sample() method, can you try adding the output_dir = ... argument to save the .csv files in a particular location, before using the save_object() method?

For example, the following works for me:

stanmodel <- "
data {
  real y_mean;
}
parameters {
  real y;
}
model {
  y ~ normal(y_mean, 1);
}
"
writeLines(stanmodel, con="main.stan")

mod <- cmdstanr::cmdstan_model("main.stan")
fit <- mod$sample(
  data = list(y_mean = 1),
  output_dir = getwd(),
  parallel_chains = 4
)
fit$save_object("main.rds")

# Restart session
library(cmdstanr)
fit <- readRDS("main.rds")
fit$cmdstan_diagnose()
2 Likes

thanks, that works!