# Plotting posterior predictive plot for time series data using Pystan

**URL:** <https://discourse.mc-stan.org/t/plotting-posterior-predictive-plot-for-time-series-data-using-pystan/23469>\
**Category:** PyStan\
**Tags:** arviz, techniques\
**Created:** [July 15, 2021, 2:57am UTC](https://discourse.mc-stan.org/t/plotting-posterior-predictive-plot-for-time-series-data-using-pystan/23469 "2021-07-15T02:57:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ants007](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@ants007](https://discourse.mc-stan.org/u/ants007)\
**Post date:** [July 15, 2021, 2:57am UTC](https://discourse.mc-stan.org/t/plotting-posterior-predictive-plot-for-time-series-data-using-pystan/23469/1 "2021-07-15T02:57:10Z")

</div>

Hello, Pystan champions,

I have fitted a structural time series model in Stan and used arviz to plot posterior predictive distribution.

here is my code:

_import arviz as az_  
_idata = az.from\_pystan(posterior=fit\_TOUR\_model, posterior\_predictive=[“y\_predict”], observed\_data=[“y”])_  
_az.plot\_ppc(idata, num\_pp\_samples=2000,data\_pairs={“y”: “y\_predict”},mean=False)_

![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/0/0ef5f2b1e8f6a0cc6133a69739288c503de964c4.png)

But I would like to see the posterior plot with time in the horizontal axis and the actual and predictive draws in the vertical axis. Is there any way this can be done using arviz?

Thanks Champs,

Antony

---

<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:** [July 15, 2021, 7:05am UTC](https://discourse.mc-stan.org/t/plotting-posterior-predictive-plot-for-time-series-data-using-pystan/23469/2 "2021-07-15T07:05:18Z")

</div>

Our GSOC student has started working on regression plots, but currently, these are not yet merged into the codebase.

> <https://github.com/arviz-devs/arviz/pull/1727>
>
> \## Description
> Here is my initial prototype for linear regression plot. I am ch…ecking if input data is correct in the the user API function. Backend function is quite small, only does the plotting part.
> Example Notebook : https://gist.github.com/utkarsh-maheshwari/8d4cd2fd84c763bf85291c3f0881d588
> 
> High level design:
> We accept 5 args
> 
> 1. y\_model : (Necessary) linear relationship in the form of \`y = m1\*x1 + m2\*x2 + c\`
> 2. idata : (Necessary if any of other args are string) should contain all the necessary variable names in any of the groups.
> 3. x\_data : (Necessary because we cannot identify the x variable name from y\_model) seq/str
> 4. y\_data : (Necessary if y\_model is Sequence. We can identify the name from y\_model) seq/str
> 5. y\_ppc : (Necessary is y\_model is sequence. Assumed to be same as y\_data if absent) seq/str 
> 
> Function call:
> 
> \`plot\_lm(y\_model = "kid\_score ~ Intercept + slope \* mom\_iq", idata = idata, x\_data = "mom\_iq")
> \`
> 
> Output : 
> !\[image\](https://user-images.githubusercontent.com/32706356/122080753-27f97c80-ce1c-11eb-8979-062a3dc4b923.png)
> 
> Shows uncertainty in y\_data in orange, uncertainty in y\_model in black, OLS regression line in yellow.
> Should regression lines be plotted instead of filling the area?
> 
> \-----------------------------------------------------------------------------------------------------
> 
> If y\_model is sequence, we're good with plotting it. 
> If y\_model is string like \`y ~ slope \* x\_name + Intercept\`, it should be converted to sequence by 
> \- correctly identifying the variable names present in the string. 
> \- look for variable names in inferenceData groups.
> \- Compute y\_model term by term to form desired sequence.
> 
> To begin with, we can do it like : 
> 
> \`\`\`
> rhs, lhs = y\_model.split('~')
> terms = rhs.split('+') : gives terms of the expression. 
> var\_name = terms.split('\*') : gives variable names to be searched in data
> 
> \`\`\`
> 
> Iterate over terms
> calculate value of each term
> add it to the y\_model\_values
> 
> It works well with parsing GLM because I am keeping track of functions in function stack.
> Any better way to parse \`y\_model\` to extract the variable names from the string?
> 
> Love to know suggestions/reccomendations.
> 
> 
> \<!--
> Thank you so much for your PR! To help us review your contribution, please consider the following points:
> 
> \- The PR title should summarize the changes, for example "Add new group argument for the pair plot".
> Avoid non-descriptive titles such as "Addresses issue #348".
> 
> \- The description should provide at least 1-2 sentences describing the pull request in detail and
> link to any relevant issues.
> 
> \- Please prefix the title of incomplete contributions with \[WIP\] (to indicate a work in
> progress). WIPs may be useful to (1) indicate you are working on something to avoid
> duplicated work, (2) request broad review of functionality or API, or (3) seek collaborators. --\>
> 
> \## Checklist
> 
> 
> \- \[\] Follows \[official\](https://github.com/arviz-devs/arviz/blob/main/CONTRIBUTING.md#pull-request-checklist) PR format
> \- \[\] Includes a sample plot to visually illustrate the changes (only for plot-related functions)
> \- \[\] New features are properly documented (with an example if appropriate)?
> \- \[\] Includes new or updated tests to cover the new feature
> \- \[\] Code style correct (follows pylint and black guidelines)
> \- \[\] Changes are listed in \[changelog\](https://github.com/arviz-devs/arviz/blob/main/CHANGELOG.md#v0xx-unreleased)
> 
> \<!--
> Also, please consider reading the contributing guidelines and code of conduct carefully before submitting the PR. They are available at
> \- https://github.com/arviz-devs/arviz/blob/main/CONTRIBUTING.md
> \- https://github.com/arviz-devs/arviz/blob/main/CODE\_OF\_CONDUCT.md
> 
> We understand that PRs can sometimes be overwhelming, especially as the
> reviews start coming in. Please let us know if the reviews are unclear or
> the recommended next step seems overly demanding, if you would like help in
> addressing a reviewer's comments, or if you have been waiting too long to hear
> back on your PR.
> \--\>

---

<div class="post-metadata">

**Author:** ![ants007](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@ants007](https://discourse.mc-stan.org/u/ants007)\
**Post date:** [July 16, 2021, 10:35am UTC](https://discourse.mc-stan.org/t/plotting-posterior-predictive-plot-for-time-series-data-using-pystan/23469/3 "2021-07-16T10:35:53Z")

</div>

Thank you, I will keep an eye on the updates.  
Thanks for your work in PyStan.
