rmcelreath / rethinking

Statistical Rethinking course and book package
2.14k stars 601 forks source link

Saving ulam models won't save cmdstanr csv #441

Closed Famondir closed 1 month ago

Famondir commented 2 months ago

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

Famondir commented 1 month ago

The problem can be solved using the cmdstanr engine and saving the csv files at a permanent folder with the output_dir argument that is passed to cmdstanr via the .... Solution got from the Stan forum.

Famondir commented 1 month ago

It still seems to exist a problem that should be fixed. If one tries to save the model via the file argument at the same time as saving the csv files via output_dir I get the error:

Error in gsub("#", "", line, fixed = TRUE) : 
  input string 1 is invalid in this locale
In addition: Warning messages:
1: In grepl("# Diagonal elements of inverse mass matrix:", line, perl = TRUE) :
  input string 1 is invalid UTF-8
2: In grepl("# Elements of inverse mass matrix:", line, perl = TRUE) :
  input string 1 is invalid UTF-8

The .csv files get created but the .rds file does not. Furthermore, the object is not created in the environment. Thus one has to run every model twice. Onceto get the ulam model and once again to save the .csv files somewhere for later loading of the model.

Famondir commented 1 month ago

Just take care none of the folders in the hierarchy contains special cases like German ü and so on and it works fine.

Famondir commented 1 month ago

A problem that asrose is that the path to the .csv files is saved as an absolute path. This is problematic if the project is worked on multiple computers with different folder structures.

It would be nice to have the posibility to save all the data necassary in an .rds from within the ulam function one can load on another computer as well.