Working with MCMCChains?

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…

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:

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

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

???

Just Wrap with DataFrame , then you can get some columns

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

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

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”

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

mychain.value[var = [:a, :b, :c, :d]]
1 Like

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:

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.

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

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.

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)

For reference here you should be able to simply do

c = chain[["parameter_1", "parameter_2"]]
3 Likes

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.