# Combine Fit Objects

**URL:** <https://discourse.mc-stan.org/t/combine-fit-objects/7262>\
**Category:** PyStan\
**Created:** [January 11, 2019, 7:28pm UTC](https://discourse.mc-stan.org/t/combine-fit-objects/7262 "2019-01-11T19:28:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Calen\_Pennington](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/calen_pennington/32/3777_2.png) [@Calen\_Pennington](https://discourse.mc-stan.org/u/Calen_Pennington)\
**Post date:** [January 11, 2019, 7:28pm UTC](https://discourse.mc-stan.org/t/combine-fit-objects/7262/1 "2019-01-11T19:28:15Z")

</div>

Is it possible to combine multiple `StanFit4Model` objects? I’ve been struggling with running out of memory (and subsequently hanging my machine) when I use enough chains (and enough samples) to get accurate data. I’d love to be able to run a smaller number of chains at a time, and then dump them with pickle to disk, and then at a later point load them all up and merge them to do analysis on.

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [January 11, 2019, 9:17pm UTC](https://discourse.mc-stan.org/t/combine-fit-objects/7262/2 "2019-01-11T21:17:13Z")

</div>

It is doable, but I don’t recommend it. You would need to touch the internals (`fit.sim`) and then make sure everything is in order.

I would recommend using ArviZ and doing some array combination with xarray. I actually made an issue today to address this problem (combining multiple InferenceData objects).

Currently the easiest way is probably first to use `az.from_pystan(posterior=fit)` and then stacking arrays with numpy tools and recreating `InferenceData` object with `convert_to_inference_data` where you would input a dictionary.

edit.

[https://arviz-devs.github.io/arviz/](https://arviz-devs.github.io/arviz/)

> <https://github.com/arviz-devs/arviz/issues/519>

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [January 11, 2019, 9:19pm UTC](https://discourse.mc-stan.org/t/combine-fit-objects/7262/3 "2019-01-11T21:19:50Z")

</div>

ArviZ also supports CmdStan outputs and automatically stacks the results.

---

<div class="post-metadata">

**Author:** ![caesoma](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/caesoma/32/19767_2.png) [@caesoma](https://discourse.mc-stan.org/u/caesoma)\
**Post date:** [January 11, 2019, 10:26pm UTC](https://discourse.mc-stan.org/t/combine-fit-objects/7262/4 "2019-01-11T22:26:21Z")

</div>

I was meaning to ask on the [other thread](https://discourse.mc-stan.org/t/any-way-to-retrieve-the-best-fitting-parameters-for-a-failed-optimizing/7166/3) but I thought it would be off-topic. Maybe it’s less so here since it’s basically the same but for cmdStan so here it goes:

Is it possible to extract the raw trace arrays from an `InferenceData` object (i.e. the .csv output of cmdstan minus the comment lines)?

I know ArviZ can combine chains from separate cmdStan-generated files, but except for the basic diagnostics I would like to be able to process the results as numpy arrays.  
I’m guessing this could also be an option for combining separate PyStan outputs as the original question, without moving everything into ArviZ from then on.

Alternatively, my suggestion would be to just extract the bidimensional sample x parameter `fit.get_sampler_params()` and `fit.extract(permuted=False, inc_warmup=True)`, and stack them into a three-dimensional sample x parameter x chain. That is what I’m doing now, but I guess this is the kind of extra work that is trying to be avoided.

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [January 11, 2019, 11:59pm UTC](https://discourse.mc-stan.org/t/combine-fit-objects/7262/5 "2019-01-11T23:59:42Z")

</div>

Raw as numpy array? Yes (either manually or `.to_dataframe()`. But if you need your samples in a table format, `pd.read_csv` works fine, and then you could stack them with numpy.

For rhat etc calculations you would still need to translate to InferenceData unless you would implement those externally.

ps. Currently ArviZ throws away warm-up samples.

---

<div class="post-metadata">

**Author:** ![Calen\_Pennington](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/calen_pennington/32/3777_2.png) [@Calen\_Pennington](https://discourse.mc-stan.org/u/Calen_Pennington)\
**Post date:** [January 12, 2019, 1:27am UTC](https://discourse.mc-stan.org/t/combine-fit-objects/7262/6 "2019-01-12T01:27:57Z")

</div>

Ok, that works. I’m already converting them to DataFrames and InferenceDatas, so as long as I can merge the latter, then that should work fine.

---

<div class="post-metadata">

**Author:** ![caesoma](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/caesoma/32/19767_2.png) [@caesoma](https://discourse.mc-stan.org/u/caesoma)\
**Post date:** [January 12, 2019, 4:39pm UTC](https://discourse.mc-stan.org/t/combine-fit-objects/7262/7 "2019-01-12T16:39:42Z")

</div>

`pandas.read_csv` will either include the comment lines or raise an error, depending on the platform, maybe it works in some other platform speciffically, but it doesn’t seem to be applicable in all cases. I had to either have pandas skip lines (which change depending on the number of samples), or read the file line by line and exclude commented ones.

For me the array in the format `fit.extract(permuted=False, inc_warmup=True)` works fine, so combining either separate PyStan runs or individual cmdStan .csv into an array like that would be close to an ideal solution, but it seems that pandas doesn’t work as expected. I couldn’t find how to manually extract that kind of object from ArviZ, but maybe I need to read the documentation more closely.  
Thanks again.

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [January 12, 2019, 4:48pm UTC](https://discourse.mc-stan.org/t/combine-fit-objects/7262/8 "2019-01-12T16:48:19Z")

</div>

> <https://github.com/arviz-devs/arviz/blob/master/arviz/data/io_cmdstan.py>

```
with open(path, "r") as f_obj:
    df = pd.read_csv(f_obj, comment="#")

```

with line is optional
