Skip to content

notebook: GLM Poisson regression, update header and footer #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
309 changes: 167 additions & 142 deletions examples/generalized_linear_models/GLM-poisson-regression.ipynb

Large diffs are not rendered by default.

51 changes: 33 additions & 18 deletions myst_nbs/generalized_linear_models/GLM-poisson-regression.myst.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,40 @@ jupytext:
format_version: 0.13
jupytext_version: 1.13.7
kernelspec:
display_name: pymc_env
display_name: pymc-ex
language: python
name: pymc_env
name: pymc-ex
---

+++ {"papermill": {"duration": 0.043172, "end_time": "2021-02-23T11:26:55.064791", "exception": false, "start_time": "2021-02-23T11:26:55.021619", "status": "completed"}, "tags": []}

(GLM-poisson-regression)=
# GLM: Poisson Regression

:::{post} November 12, 2022
:tags: regression, poisson
:category: Intermediate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The flying dove in the preview: https://pymcio--462.org.readthedocs.build/projects/examples/en/462/generalized_linear_models/GLM-poisson-regression.html is not green because tagss and categories are case sensitive. it should be everything lowercase.

:author: Jonathan Sedar, Benjamin Vincent
:::

+++ {"papermill": {"duration": 0.069202, "end_time": "2021-02-23T11:27:01.489628", "exception": false, "start_time": "2021-02-23T11:27:01.420426", "status": "completed"}, "tags": []}

This is a minimal reproducible example of Poisson regression to predict counts using dummy data.

This Notebook is basically an excuse to demo Poisson regression using PyMC, both manually and using `bambi` to demo interactions using the `formulae` library. We will create some dummy data, Poisson distributed according to a linear model, and try to recover the coefficients of that linear model through inference.

For more statistical detail see:

+ Basic info on [Wikipedia](https://en.wikipedia.org/wiki/Poisson_regression)
+ GLMs: Poisson regression, exposure, and overdispersion in Chapter 6.2 of [ARM, Gelmann & Hill 2006](http://www.stat.columbia.edu/%7Egelman/arm/)
+ This worked example from ARM 6.2 by [Clay Ford](http://www.clayford.net/statistics/poisson-regression-ch-6-of-gelman-and-hill/)

This very basic model is inspired by [a project by Ian Osvald](http://ianozsvald.com/2016/05/07/statistically-solving-sneezes-and-sniffles-a-work-in-progress-report-at-pydatalondon-2016/), which is concerned with understanding the various effects of external environmental factors upon the allergic sneezing of a test subject.

```{code-cell} ipython3
#!pip install seaborn
```

```{code-cell} ipython3
---
papermill:
Expand Down Expand Up @@ -53,20 +78,6 @@ rng = np.random.default_rng(RANDOM_SEED)
az.style.use("arviz-darkgrid")
```

+++ {"papermill": {"duration": 0.069202, "end_time": "2021-02-23T11:27:01.489628", "exception": false, "start_time": "2021-02-23T11:27:01.420426", "status": "completed"}, "tags": []}

This is a minimal reproducible example of Poisson regression to predict counts using dummy data.

This Notebook is basically an excuse to demo Poisson regression using PyMC3, both manually and using `bambi` to demo interactions using the `formulae` library. We will create some dummy data, Poisson distributed according to a linear model, and try to recover the coefficients of that linear model through inference.

For more statistical detail see:

+ Basic info on [Wikipedia](https://en.wikipedia.org/wiki/Poisson_regression)
+ GLMs: Poisson regression, exposure, and overdispersion in Chapter 6.2 of [ARM, Gelmann & Hill 2006](http://www.stat.columbia.edu/%7Egelman/arm/)
+ This worked example from ARM 6.2 by [Clay Ford](http://www.clayford.net/statistics/poisson-regression-ch-6-of-gelman-and-hill/)

This very basic model is inspired by [a project by Ian Osvald](http://ianozsvald.com/2016/05/07/statistically-solving-sneezes-and-sniffles-a-work-in-progress-report-at-pydatalondon-2016/), which is concerned with understanding the various effects of external environmental factors upon the allergic sneezing of a test subject.

+++ {"papermill": {"duration": 0.06268, "end_time": "2021-02-23T11:27:01.615645", "exception": false, "start_time": "2021-02-23T11:27:01.552965", "status": "completed"}, "tags": []}

## Local Functions
Expand Down Expand Up @@ -489,7 +500,7 @@ posterior_predictive = model.predict(inf_fish_alt, kind="pps")

We can use `az.plot_ppc()` to check that the posterior predictive samples are similar to the observed data.

For more information on posterior predictive checks, we can refer to https://docs.pymc.io/notebooks/posterior_predictive.html.
For more information on posterior predictive checks, we can refer to {class}`pymc.posterior_predictive`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For more information on posterior predictive checks, we can refer to {class}`pymc.posterior_predictive`.
For more information on posterior predictive checks, we can refer to {ref}`pymc:posterior_predictive`.


```{code-cell} ipython3
az.plot_ppc(inf_fish_alt);
Expand All @@ -498,8 +509,9 @@ az.plot_ppc(inf_fish_alt);
+++ {"papermill": {"duration": 0.106366, "end_time": "2021-02-23T11:31:34.956844", "exception": false, "start_time": "2021-02-23T11:31:34.850478", "status": "completed"}, "tags": []}

## Authors
- Example originally contributed by Jonathan Sedar 2016-05-15 [github.com/jonsedar](https://github.com/jonsedar)
- Example originally contributed by [Jonathan Sedar](https://github.com/jonsedar) 2016-05-15.
- Updated to PyMC v4 by [Benjamin Vincent](https://github.com/drbenvincent) May 2022.
- Notebook header and footer updated November 2022.

+++

Expand All @@ -518,3 +530,6 @@ tags: []
%load_ext watermark
%watermark -n -u -v -iv -w -p aesara,aeppl
```

:::{include} ../page_footer.md
:::