# Rhat and ESS from the print(stan\_fit) different from the functions

**URL:** <https://discourse.mc-stan.org/t/rhat-and-ess-from-the-print-stan-fit-different-from-the-functions/18334>\
**Category:** General\
**Created:** [October 1, 2020, 8:13pm UTC](https://discourse.mc-stan.org/t/rhat-and-ess-from-the-print-stan-fit-different-from-the-functions/18334 "2020-10-01T20:13:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZJL](https://avatars.discourse-cdn.com/v4/letter/z/f475e1/32.png) [@ZJL](https://discourse.mc-stan.org/u/ZJL)\
**Post date:** [October 1, 2020, 8:13pm UTC](https://discourse.mc-stan.org/t/rhat-and-ess-from-the-print-stan-fit-different-from-the-functions/18334/1 "2020-10-01T20:13:55Z")

</div>

Hello, I estimated a model using Rstan and with function stan(), then checked the results with function print(stan\_fit, pars="…"). It returned back very small ESS and large Rhat. However, when I used the function Rhat(), ess\_bulk(), and ess\_tail() to compute these indices again, the results seemed much more normal. I don’t understand why I got different values and which one I should believe. Could anyone help me with it? Thanks very much!

---

<div class="post-metadata">

**Author:** ![avehtari](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avehtari/32/5935_2.png) [@avehtari](https://discourse.mc-stan.org/u/avehtari)\
**Post date:** [October 2, 2020, 1:04pm UTC](https://discourse.mc-stan.org/t/rhat-and-ess-from-the-print-stan-fit-different-from-the-functions/18334/2 "2020-10-02T13:04:41Z")

</div>

> [@ZJL](#):
>
> function print(stan\_fit, pars=“…”). It returned back very small ESS and large Rhat. However, when I used the function Rhat(), ess\_bulk(), and ess\_tail() to compute these indices again, the results seemed much more normal.

It’s likely that you have different versions of these statistics called. Can you specify how did you call Rhat and ESS functions, so I can tell the specific versions. See [Search 2.5 million pages of mathematics and statistics articles](https://projecteuclid.org/euclid.ba/1593828229) for description of different versions.

Can you tell the values that you consider small, large and normal?

---

<div class="post-metadata">

**Author:** ![ZJL](https://avatars.discourse-cdn.com/v4/letter/z/f475e1/32.png) [@ZJL](https://discourse.mc-stan.org/u/ZJL)\
**Post date:** [October 2, 2020, 2:13pm UTC](https://discourse.mc-stan.org/t/rhat-and-ess-from-the-print-stan-fit-different-from-the-functions/18334/3 "2020-10-02T14:13:57Z")

</div>

Thanks for your reply! Here is how I called Rhat and ESS functions:  
I first used stan\_est = extract(stan\_fit) to get the parameter estimates in all iterations. Then used theta\_est = stan\_est$theta to get estimates of a specific parameter, theta.  
Because I had 2 chains and each with 1000 iterations, so I formatted theta\_est as: theta\_est2 = matrix(theta\_est, ncol=2). I guess this may let me have iterations from 1 chain in each column, but I can be wrong. Lastly, I called Rhat and ESS functions as: Rhat(theta\_est2); ess\_bulk(theta\_est2); ess\_tail(theta\_est2).

---

<div class="post-metadata">

**Author:** ![avehtari](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avehtari/32/5935_2.png) [@avehtari](https://discourse.mc-stan.org/u/avehtari)\
**Post date:** [October 2, 2020, 2:31pm UTC](https://discourse.mc-stan.org/t/rhat-and-ess-from-the-print-stan-fit-different-from-the-functions/18334/4 "2020-10-02T14:31:31Z")

</div>

> [@ZJL](#):
>
> I first used stan\_est = extract(stan\_fit) to get the parameter estimates in all iterations.

Ah this explains it. The bad design choice in extract strikes again. If look at the help of `extract`

```no-highlight
     extract(object, pars, permuted = TRUE, inc_warmup = FALSE, 
       include = TRUE) 

```

you see that by default it permutes the iterations which discards the needed information about chains and autocorrelations for Rhat and ESS. Use  
`stan_est = extract(stan_fit, permuted=FALSE)`  
or  
`stan_est = as.matrix(stan_fit)`  
or even better, use [`posterior` package](https://mc-stan.org/posterior/)

> [@ZJL](#):
>
> Because I had 2 chains

We also recommend to run at least 4 chains.

---

<div class="post-metadata">

**Author:** ![ZJL](https://avatars.discourse-cdn.com/v4/letter/z/f475e1/32.png) [@ZJL](https://discourse.mc-stan.org/u/ZJL)\
**Post date:** [October 3, 2020, 4:44pm UTC](https://discourse.mc-stan.org/t/rhat-and-ess-from-the-print-stan-fit-different-from-the-functions/18334/6 "2020-10-03T16:44:20Z")

</div>

Thank you very much!
