# How to extract sampled values from a Turing model

**URL:** <https://discourse.julialang.org/t/how-to-extract-sampled-values-from-a-turing-model/92157>\
**Category:** General Usage\
**Created:** [December 26, 2022, 5:47pm UTC](https://discourse.julialang.org/t/how-to-extract-sampled-values-from-a-turing-model/92157 "2022-12-26T17:47:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [December 26, 2022, 5:47pm UTC](https://discourse.julialang.org/t/how-to-extract-sampled-values-from-a-turing-model/92157/1 "2022-12-26T17:47:00Z")

</div>

I have implemented a Bayesian model and I am interested in extracting the results. I’d like to first assess convergence of the MCMC chains but also extract the values for the parameters.

```julia
chain = sample(model_fun, sampler, MCMCThreads(), 10000, 4; progress=false);
group(chain, :y)

```

This prints out the following summary:

```julia
Chains MCMC chain (10000×41×4 Array{Float64, 3}):

Iterations = 1:1:10000
Number of chains = 4
Samples per chain = 10000
Wall duration = 507.61 seconds
Compute duration = 2027.93 seconds
parameters = y[12], y[13], y[14], y[15], y[16], y[17], y[18], y[19], y[20], y[21], y[22], y[23], y[24], y[25], y[26], y[27], y[28], y[29], y[30], y[31], y[32], y[33], y[34], y[35], y[36], y[37], y[38], y[39], y[40], y[41], y[42], y[43], y[44], y[45], y[46], y[47], y[48], y[49], y[50], y[51], y[52]
internals = 

Summary Statistics
  parameters mean std naive_se mcse ess rhat ess_per_sec 
      Symbol Float64 Float64 Float64 Float64 Float64 Float64 Float64 

       y[12] 0.0004 0.0006 0.0000 0.0000 815.1909 1.0082 0.4020
       y[13] 0.0004 0.0007 0.0000 0.0000 684.4333 1.0042 0.3375
       y[14] 0.0005 0.0008 0.0000 0.0000 667.7636 1.0055 0.3293
       y[15] 0.0008 0.0010 0.0000 0.0000 703.1255 1.0026 0.3467
       y[16] 0.0016 0.0015 0.0000 0.0000 611.2506 1.0052 0.3014
       y[17] 0.0028 0.0022 0.0000 0.0001 501.4286 1.0057 0.2473
       y[18] 0.0036 0.0027 0.0000 0.0001 459.1139 1.0063 0.2264
       y[19] 0.0027 0.0022 0.0000 0.0001 488.3284 1.0078 0.2408
       y[20] 0.0067 0.0045 0.0000 0.0002 397.7043 1.0122 0.1961
       y[21] 0.0106 0.0068 0.0000 0.0003 348.9909 1.0176 0.1721
       y[22] 0.0133 0.0082 0.0000 0.0003 340.5442 1.0195 0.1679
       y[23] 0.0130 0.0079 0.0000 0.0003 342.7045 1.0200 0.1690
       y[24] 0.0100 0.0063 0.0000 0.0003 362.7006 1.0188 0.1789
       y[25] 0.0067 0.0045 0.0000 0.0002 400.3231 1.0165 0.1974
       y[26] 0.0054 0.0038 0.0000 0.0001 392.4336 1.0165 0.1935
       y[27] 0.0046 0.0033 0.0000 0.0001 425.4097 1.0128 0.2098
       y[28] 0.0030 0.0024 0.0000 0.0001 446.8808 1.0130 0.2204
      ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮
                                                                           24 rows omitted

Quantiles
  parameters 2.5% 25.0% 50.0% 75.0% 97.5% 
      Symbol Float64 Float64 Float64 Float64 Float64 

       y[12] 0.0000 0.0000 0.0001 0.0005 0.0021
       y[13] 0.0000 0.0000 0.0001 0.0005 0.0023
       y[14] 0.0000 0.0000 0.0002 0.0007 0.0027
       y[15] 0.0000 0.0001 0.0005 0.0012 0.0035
       y[16] 0.0000 0.0005 0.0012 0.0022 0.0054
       y[17] 0.0000 0.0011 0.0023 0.0038 0.0084
       y[18] 0.0001 0.0016 0.0030 0.0049 0.0105
       y[19] 0.0000 0.0011 0.0022 0.0038 0.0083
       y[20] 0.0001 0.0035 0.0058 0.0088 0.0177
       y[21] 0.0001 0.0061 0.0095 0.0139 0.0271
       y[22] 0.0004 0.0078 0.0120 0.0174 0.0328
       y[23] 0.0004 0.0076 0.0117 0.0169 0.0317
       y[24] 0.0002 0.0057 0.0089 0.0132 0.0250
       y[25] 0.0001 0.0036 0.0060 0.0090 0.0177
       y[26] 0.0000 0.0027 0.0048 0.0073 0.0144
       y[27] 0.0001 0.0022 0.0040 0.0063 0.0127
       y[28] 0.0000 0.0013 0.0025 0.0042 0.0091

```

I would like to extract the two dataframes: (1) the means of `y[i]` and (2) the quantiles of `y[i]`. I could do this manually but converting the `chain` object to a dataframe by `DataFrame(chain)` and then take the `mean` (and `quantile`) of `eachcol` but I was wondering if there was a way to just extract this data from the `chain` object.

And follow up question, how exactly do I check for MCMC convergence? I’d like to extract the data for the associated traceplots.

---

<div class="post-metadata">

**Author:** ![Storopoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/storopoli/32/209278_2.png) [@Storopoli](https://discourse.julialang.org/u/Storopoli)\
**Post date:** [December 27, 2022, 9:58am UTC](https://discourse.julialang.org/t/how-to-extract-sampled-values-from-a-turing-model/92157/2 "2022-12-27T09:58:56Z")

</div>

> [@affans](#):
>
> I would like to extract the two dataframes: (1) the means of `y[i]` and (2) the quantiles of `y[i]`

```julia
summaries, quantiles = describe(chain)

```

There’s a tutorial (shameless self-promotion) here: [How to use Turing](https://storopoli.github.io/Bayesian-Julia/pages/04_Turing/).

> [@affans](#):
>
> how exactly do I check for MCMC convergence?

It’s right there: `ess` and `rhat` columns.
