Saving ulam model with cmdstanr .csv

Hello everyone,
I’m struggeling with the ulam() function from the rethinking package. The created objects seem to miss the fitted model itself and if I save those and reload them later the .csv files from cmdstanr (or rstan?) are missing. I hope someone can show me a way how to save those models with all the files otherwise saved in some temp folder.

data(Howell1)
d <- Howell1
d <- d[ d$age>=18 , ]
dat <- list(
  W = d$weight,
  S = d$male + 1 ) # S=1 female, S=2 male

m_SW <- ulam(
  alist(
    W ~ dnorm(mu,sigma),
    mu <- a[S],
    a[S] ~ dnorm(60,10),
    sigma ~ dunif(0,10)
  ), data=dat, 
  cores = 2, chains = 2,
  file = "saved_objects/test")
precis(m_SW)

The error message:

Error in read_cmdstan_csv(files = self$output_files(include_failed = FALSE), : Assertion on 'files' failed: File does not exist: 'C:/Users/Simon/AppData/Local/Temp/RtmpCwCaxQ/ulam_cmdstanr_81cc7da0997f2160a75009e902acb596-202407290957-1-2c429d.csv'.

Operating System: Win11
Interface Version: RStudio 2024.04.2; R 4.4.1

I don’t know about the ulam package, but CmdStanR’s sample function has argument output_dir which will put the set of CSV files to whatever directory you specify.

in the above error message, the file path C:/Users/Simon/AppData/Local/Temp/RtmpCwCaxQ indicates that the CSV files are being saved to temporary storage - this is the default because during model development, it’s possible to do many many runs of a model and really clutter up your workspace w/ tons of CSV files. OTOH, if you’ve got an analysis that works, you know where you want to put the output files, and CmdStanR doesn’t, so you need to specify the output directory explicitly.

2 Likes

I’m not sure how ulam() from the rethinking package handles CmdStanR fitted model objects internally, but if you’re using CmdStanR to fit the model (I think you need to set cmdstan=TRUE when calling ulam() or set set_ulam_cmdstan(TRUE) in your R session), then like @mitzimorris said you can specify output_dir to save the CSV files somewhere. But I don’t know if the rethinking package will then use those CSV files when you reload a saved object. If that doesn’t work properly I would open an issue at the GitHub repository for the rethinking package (GitHub - rmcelreath/rethinking: Statistical Rethinking course and book package).

Either way, if you specify output_dir (via the ... in ulam() that lets you pass arguments to Stan) you should be able to read in the CSV files using functions from the CmdStanR interface. You can point cmdstanr::read_cmdstan_csv() or cmdstanr::as_cmdstan_fit() at the CSV files saved in output_dir. The former returns a list, the latter returns a fitted model object created by CmdStanR.

1 Like

The ulam comand loads the csv files saved with output_dir and the model is recovered fully operational. Thanks for your advice.

2 Likes