# Preservation of MCMC sample results

**URL:** <https://discourse.mc-stan.org/t/preservation-of-mcmc-sample-results/15252>\
**Category:** General\
**Tags:** techniques\
**Created:** [May 20, 2020, 6:00am UTC](https://discourse.mc-stan.org/t/preservation-of-mcmc-sample-results/15252 "2020-05-20T06:00:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodi321](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/xiaodi321/32/9386_2.png) [@xiaodi321](https://discourse.mc-stan.org/u/xiaodi321)\
**Post date:** [May 20, 2020, 6:00am UTC](https://discourse.mc-stan.org/t/preservation-of-mcmc-sample-results/15252/1 "2020-05-20T06:00:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Jean\_Billie](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jean_billie/32/9186_2.png) [@Jean\_Billie](https://discourse.mc-stan.org/u/Jean_Billie)\
**Post date:** [May 20, 2020, 6:05am UTC](https://discourse.mc-stan.org/t/preservation-of-mcmc-sample-results/15252/2 "2020-05-20T06:05:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Stephen\_Martin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stephen_martin/32/7879_2.png) [@Stephen\_Martin](https://discourse.mc-stan.org/u/Stephen_Martin)\
**Post date:** [May 20, 2020, 6:09am UTC](https://discourse.mc-stan.org/t/preservation-of-mcmc-sample-results/15252/3 "2020-05-20T06:09:18Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![Jean\_Billie](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jean_billie/32/9186_2.png) [@Jean\_Billie](https://discourse.mc-stan.org/u/Jean_Billie)\
**Post date:** [May 20, 2020, 6:12am UTC](https://discourse.mc-stan.org/t/preservation-of-mcmc-sample-results/15252/4 "2020-05-20T06:12:12Z")

</div>

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