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 all 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
310 changes: 167 additions & 143 deletions examples/generalized_linear_models/GLM-poisson-regression.ipynb

Large diffs are not rendered by default.

53 changes: 34 additions & 19 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 30, 2022
:tags: regression, poisson
:category: intermediate
: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 All @@ -77,7 +88,7 @@ This very basic model is inspired by [a project by Ian Osvald](http://ianozsvald

+++ {"papermill": {"duration": 0.060542, "end_time": "2021-02-23T11:27:01.884617", "exception": false, "start_time": "2021-02-23T11:27:01.824075", "status": "completed"}, "tags": []}

This dummy dataset is created to emulate some data created as part of a study into quantified self, and the real data is more complicated than this. Ask Ian Osvald if you'd like to know more https://twitter.com/ianozsvald
This dummy dataset is created to emulate some data created as part of a study into quantified self, and the real data is more complicated than this. Ask Ian Osvald if you'd like to know more [@ianozvald](https://twitter.com/ianozsvald).


### Assumptions:
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 {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
:::