# How to save mcmc resulte

**URL:** <https://discourse.pymc.io/t/how-to-save-mcmc-resulte/1249>\
**Category:** Questions\
**Created:** [May 20, 2018, 3:07am UTC](https://discourse.pymc.io/t/how-to-save-mcmc-resulte/1249 "2018-05-20T03:07:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cabbagee](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cabbagee/32/660_2.png) [@Cabbagee](https://discourse.pymc.io/u/Cabbagee)\
**Post date:** [May 20, 2018, 3:07am UTC](https://discourse.pymc.io/t/how-to-save-mcmc-resulte/1249/1 "2018-05-20T03:07:09Z")

</div>

Hi,  
I want to save the sample to the local file,

```
with model:
    start=pm.find_MAP(model=model, fmin=optimize.fmin_powell)
    trace=pm.sample(50,start=start,njobs=1,trace=pm.backends.text.Text('try'))

```

but there was a mistake

```
ValueError: Chains are not unique.

```

I hope to get your advice, thank you.

---

<div class="post-metadata">

**Author:** ![narendramukherjee](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/narendramukherjee/32/753_2.png) [@narendramukherjee](https://discourse.pymc.io/u/narendramukherjee)\
**Post date:** [May 20, 2018, 3:38am UTC](https://discourse.pymc.io/t/how-to-save-mcmc-resulte/1249/2 "2018-05-20T03:38:55Z")

</div>

I think the issue pertains to the fact that you are starting all your sampling runs from the same starting point - checking lack of convergence on the basis of the rhat statistic later doesn’t make sense if all the chains started from the same starting point. In addition, you are sampling just 50 samples - likely those 50 samples end up being very similar in all your chains. Usually, it is not advisable to start sampling at the MAP (especially in high dimensions) - it seems like the recommendation is to random starts and run several chains and check lack of convergence with the rhat (which pymc3 automatically does).

> [@About sampling start with find\_MAP](https://discourse.pymc.io/t/about-sampling-start-with-find-map/936):
>
> I have been looking for solutions to my model because didn’t give good convergence. When I was searching for information I saw some people using start = pm.find\_MAP() train\_trace = pm.sample( train\_samp, tune= train\_tune, start=start, njobs=4 ) and on the documentation it states map : Use the MAP as starting point. This is discouraged. My questions are Is pm.sample( …,start=start, …) equivalent to pm.sample( …,init= ‘map’, …)? Why is it discouraged? I tried with and without start= p…

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [May 20, 2018, 5:44am UTC](https://discourse.pymc.io/t/how-to-save-mcmc-resulte/1249/3 "2018-05-20T05:44:01Z")

</div>

Unfortunately this is caused by a bug in the text backend:

> <https://github.com/pymc-devs/pymc3/issues/2856>

@colcarroll recently implemented a new load/save mechanism, we dont have an example yet but you can try follow the test case:

> <https://github.com/pymc-devs/pymc3/blob/cf636dcc36ff3c10ff3ec3685543da1ca2aa25d9/pymc3/tests/test_ndarray_backend.py#L170-L193>

---

<div class="post-metadata">

**Author:** ![Cabbagee](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cabbagee/32/660_2.png) [@Cabbagee](https://discourse.pymc.io/u/Cabbagee)\
**Post date:** [May 20, 2018, 11:50am UTC](https://discourse.pymc.io/t/how-to-save-mcmc-resulte/1249/4 "2018-05-20T11:50:09Z")

</div>

Thank you two for helping me to puzzle out, and If I want to save the sampling results to the Excel file, how do I operate it?Is there such an example here？

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [May 20, 2018, 12:48pm UTC](https://discourse.pymc.io/t/how-to-save-mcmc-resulte/1249/5 "2018-05-20T12:48:28Z")

</div>

You can use `pm.trace_to_dataframe` to save the trace into a dataframe, and then save it as a .csv file using pandas.

---

<div class="post-metadata">

**Author:** ![Cabbagee](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cabbagee/32/660_2.png) [@Cabbagee](https://discourse.pymc.io/u/Cabbagee)\
**Post date:** [May 22, 2018, 10:01am UTC](https://discourse.pymc.io/t/how-to-save-mcmc-resulte/1249/6 "2018-05-22T10:01:31Z")

</div>

I want to save multiple sampling results in different workbooks of the same Excel file：

```
write1=pd.ExcelWriter('w.xlsx')
df=[]
for i in range(M):
    with pm.Model() as model:
        packed_l=pm.LKJCholeskyCov('packed_l',n=2,eta=2,sd_dist=pm.HalfCauchy.dist(2.5))
        L=pm.expand_packed_triangular(2,packed_l)
        Sigma=pm.Deterministic('Sigma',tt.dot(L,L.T))
        mu=pm.Normal('mu',0.,10,shape=2,testval=dataSet[i].mean(axis=0))
        obs=pm.MvNormal('obs',mu=mu,chol=L,observed=dataSet[i])
        trace = pm.sample(random_seed=seed,model=model,njobs=1)
    
    df.append(pm.trace_to_dataframe(trace).to_excel(write1,sheet_name='sheet'+str(i)))

```

But after the sampling, the `w.xlsx` is empty, and the result is not saved.

---

<div class="post-metadata">

**Author:** ![Nadheesh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/nadheesh/32/650_2.png) [@Nadheesh](https://discourse.pymc.io/u/Nadheesh)\
**Post date:** [May 22, 2018, 11:14am UTC](https://discourse.pymc.io/t/how-to-save-mcmc-resulte/1249/7 "2018-05-22T11:14:52Z")

</div>

First, Do you call write1.close() ?

If you call that, then this should work, unless your trace it empty. Did you debug and check the trace?

I tried reproducing the issue but, it is difficult to generate data for your model. The logic you used to save the trace works. I tried the same logic with different model and it worked. May be the problem is with the model.

---

<div class="post-metadata">

**Author:** ![Cabbagee](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cabbagee/32/660_2.png) [@Cabbagee](https://discourse.pymc.io/u/Cabbagee)\
**Post date:** [May 23, 2018, 1:03am UTC](https://discourse.pymc.io/t/how-to-save-mcmc-resulte/1249/8 "2018-05-23T01:03:43Z")

</div>

Thank you very much for your reminding， I called `write1.close()` before. It’s really a problem with the model， after I modify it, I can store the sampling results.
