(GLM-poisson-regression)=

GLM: Poisson Regression

:::{post} November 30, 2022 :tags: regression, poisson :category: intermediate :author: Jonathan Sedar, Benjamin Vincent :::

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:

This very basic model is inspired by a project by Ian Osvald, which is concerned with understanding the various effects of external environmental factors upon the allergic sneezing of a test subject.

Local Functions

Generate Data

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.

Assumptions:

  • The subject sneezes N times per day, recorded as nsneeze (int)
  • The subject may or may not drink alcohol during that day, recorded as alcohol (boolean)
  • The subject may or may not take an antihistamine medication during that day, recorded as the negative action nomeds (boolean)
  • We postulate (probably incorrectly) that sneezing occurs at some baseline rate, which increases if an antihistamine is not taken, and further increased after alcohol is consumed.
  • The data is aggregated per day, to yield a total count of sneezes on that day, with a boolean flag for alcohol and antihistamine usage, with the big assumption that nsneezes have a direct causal relationship.

Create 4000 days of data: daily counts of sneezes which are Poisson distributed w.r.t alcohol consumption and antihistamine usage

View means of the various combinations (Poisson mean values)

Briefly Describe Dataset

Observe:

  • This looks a lot like poisson-distributed count data (because it is)
  • With nomeds == False and alcohol == False (top-left, akak antihistamines WERE used, alcohol was NOT drunk) the mean of the poisson distribution of sneeze counts is low.
  • Changing alcohol == True (top-right) increases the sneeze count nsneeze slightly
  • Changing nomeds == True (lower-left) increases the sneeze count nsneeze further
  • Changing both alcohol == True and nomeds == True (lower-right) increases the sneeze count nsneeze a lot, increasing both the mean and variance.

Poisson Regression

Our model here is a very simple Poisson regression, allowing for interaction of terms:

θ=exp(βX) \theta = exp(\beta X)

Ysneeze_countPoisson(θ) Y_{sneeze\_count} \sim Poisson(\theta)

Create linear model for interaction of terms

1. Manual method, create design matrices and manually specify model

Create Design Matrices

Create Model

Sample Model

View Diagnostics

Observe:

  • The model converges quickly and traceplots looks pretty well mixed

Transform coeffs and recover theta values

Observe:

  • The contributions from each feature as a multiplier of the baseline sneezecount appear to be as per the data generation:

    1. exp(Intercept): mean=1.05 cr=[0.98, 1.10]

      Roughly linear baseline count when no alcohol and meds, as per the generated data:

      theta_noalcohol_meds = 1 (as set above) theta_noalcohol_meds = exp(Intercept) = 1

    2. exp(alcohol): mean=2.86 cr=[2.67, 3.07]

      non-zero positive effect of adding alcohol, a ~3x multiplier of baseline sneeze count, as per the generated data:

      theta_alcohol_meds = 3 (as set above) theta_alcohol_meds = exp(Intercept + alcohol) = exp(Intercept) * exp(alcohol) = 1 * 3 = 3

    3. exp(nomeds): mean=5.73 cr=[5.34, 6.08]

      larger, non-zero positive effect of adding nomeds, a ~6x multiplier of baseline sneeze count, as per the generated data:

      theta_noalcohol_nomeds = 6 (as set above) theta_noalcohol_nomeds = exp(Intercept + nomeds) = exp(Intercept) * exp(nomeds) = 1 * 6 = 6

    4. exp(alcohol:nomeds): mean=2.10 cr=[1.96, 2.28]

      small, positive interaction effect of alcohol and meds, a ~2x multiplier of baseline sneeze count, as per the generated data:

      theta_alcohol_nomeds = 36 (as set above) theta_alcohol_nomeds = exp(Intercept + alcohol + nomeds + alcohol:nomeds) = exp(Intercept) * exp(alcohol) * exp(nomeds * alcohol:nomeds) = 1 * 3 * 6 * 2 = 36

2. Alternative method, using bambi

Create Model

Alternative automatic formulation using bambi

Fit Model

View Traces

Transform coeffs

Observe:

  • The traceplots look well mixed
  • The transformed model coeffs look moreorless the same as those generated by the manual model
  • Note that the posterior predictive samples have an extreme skew

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 {ref}pymc:posterior_predictive.

Authors

  • Example originally contributed by Jonathan Sedar 2016-05-15.
  • Updated to PyMC v4 by Benjamin Vincent May 2022.
  • Notebook header and footer updated November 2022.

Watermark

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