# Working with MCMCChains?

**URL:** https://discourse.julialang.org/t/working-with-mcmcchains/99014
**Category:** General Usage
**Tags:** turing
**Created:** [May 17, 2023, 7:28pm UTC](https://discourse.julialang.org/t/working-with-mcmcchains/99014 "2023-05-17T19:28:56Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [May 17, 2023, 7:28pm UTC](https://discourse.julialang.org/t/working-with-mcmcchains/99014/1 "2023-05-17T19:28:56Z")

</div>

Suppose I’ve fit a model with Turing and I have a “Chains” object. Suppose my model has a bunch of nuisance parameters, and I’m just interested in looking at several particular parameters. I want to extract a chains object that has just the parameters say `(:a,:b,:c,:d)`… How am I supposed to do it?

Things I’ve tried that don’t make sense…

```julia
get(chain,[:a,:b,:c,:d]) # returns tuples not chains

group(chain,...) # designed to collapse multidimensional parameters

```

Thing I’m thinking is the “right” way:

```julia
mysample = sample(...) # get a Chains object

subvars = set_sections(mysample,Dict(:myparams => [:a,:b,:c,:d]))

mysubvarsample = Chains(subvars,:myparams)

```

That seems really awkward… why isn’t there something like

```julia
subchain(mychain,[:a,:b,:c,:d])

```

???

---

<div class="post-metadata">

### Author: ![math4mad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math4mad/32/49486_2.png) [@math4mad](https://discourse.julialang.org/u/math4mad)
#### Post date: [May 17, 2023, 7:54pm UTC](https://discourse.julialang.org/t/working-with-mcmcchains/99014/2 "2023-05-17T19:54:00Z")

</div>

Just Wrap with `DataFrame` , then you can get some columns

```julia
df=DataFrame(chain)
df[:,[:a,:b]]

```

but `subset(df,:a)` can’t work, I don’t why

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [May 17, 2023, 10:27pm UTC](https://discourse.julialang.org/t/working-with-mcmcchains/99014/3 "2023-05-17T22:27:24Z")

</div>

I want to pass a Chains object to things that summarize or plot chains etc, but I don’t want to summarize or plot all ~100s of parameters just the “important ones”

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [May 18, 2023, 8:39am UTC](https://discourse.julialang.org/t/working-with-mcmcchains/99014/4 "2023-05-18T08:39:45Z")

</div>

The `Chain` object is an `AxisArray` at its core, so you could do

```julia
mychain.value[var = [:a, :b, :c, :d]]

```

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [May 18, 2023, 3:49pm UTC](https://discourse.julialang.org/t/working-with-mcmcchains/99014/5 "2023-05-18T15:49:02Z")

</div>

This would give me the data, but it’d be an AxisArray right? Not an Chains object, and hence functions designed to summarize or plot chains may or may not understand it.

I’m kind of flabbergasted that there doesn’t appear to be a simple way to get a subchain as a Chains object. It’s trivial to write this function:

```julia
function subchain(ch,secname,vars)
    Chains(set_sections(ch,Dict(secname => vars)),secname)
end

```

But it seems like this sort of thing should be part of the API with some kind of consensus as to how it should work.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [May 18, 2023, 3:53pm UTC](https://discourse.julialang.org/t/working-with-mcmcchains/99014/6 "2023-05-18T15:53:56Z")

</div>

Sorry I misread your question I guess, I think it’s less of a question and more of a github issue/PR

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [May 18, 2023, 4:17pm UTC](https://discourse.julialang.org/t/working-with-mcmcchains/99014/7 "2023-05-18T16:17:36Z")

</div>

Yeah, I wanted to be sure I hadn’t just overlooked something before opening an issue. I’ll open an issue if the consensus is there’s nothing that I missed, it’s just not a thing in the API.

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [May 18, 2023, 4:36pm UTC](https://discourse.julialang.org/t/working-with-mcmcchains/99014/8 "2023-05-18T16:36:58Z")

</div>

If you do `a = Array(chain)`, select the variables you want, e.g.` b=a[:,inds]`, then `c=Chains(b)` will create a chain with the selected variables. I don’t know how to extract and select the variable names from the first chain, but there must be a way. If you have them, then the last step would be `c=Chains(b,names)`

---

<div class="post-metadata">

### Author: ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)
#### Post date: [May 18, 2023, 5:41pm UTC](https://discourse.julialang.org/t/working-with-mcmcchains/99014/9 "2023-05-18T17:41:58Z")

</div>

For reference here you should be able to simply do

```julia
c = chain[["parameter_1", "parameter_2"]]

```

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [May 18, 2023, 6:52pm UTC](https://discourse.julialang.org/t/working-with-mcmcchains/99014/10 "2023-05-18T18:52:58Z")

</div>

Is there a place in the docs where this is explained? If not, I’ll modify my issue I filed to make it an issue with the docs.
