Preservation of MCMC sample results

I’m using the Stan function to estimate the model parameters, but how do I save the results in order to make them reusable for later review?Currently r software and rstan software packages are being used to estimate model parameters。Can you give me some advice?

To save any R object, e.g. the following code is available.

 fit <- stan()
 save( fit,file =paste0(file.path(Sys.getenv("USERPROFILE"),"Desktop"),"\\fit") )

Then a file named fit is created in Desktop and if you put it in the working directory, then the code load("fit") makes the R object.

If you specify Working directory, then the codes save( fit,file ="fit" ) creates a file in Working directory in whcih the desired object is.

I prefer:

fit <- stan(...) # Stan call

# Saving it individually:
saveRDS(fit, "path/to/folder/fit.Rds")

The benefit of saveRDS is that when reloaded (via readRDS("path/to/folder/fit.Rds"), you can rename it on the fly:

myPreviousFit <- readRDS("path/to/folder/fit.Rds")

Whereas with save/load, it keeps the same name. It makes it easier to avoid clashes, and prevents overwriting a current variable named fit when calling load.

1 Like

I did not know the benefit of rename. That’s really helpful !

1 Like