Mixed Effects Modeling in Python: country-level random effects with Bambi

A paper I helped with is now in print, Comfort et al, Association Between Subnational Vaccine Coverage, Migration, and Incident Cases of Measles, Mumps, and Rubella in Iraq, 2001–2016.

Figure 1. (A) Measles incidence per 100,000 persons in Iraq by governorate, 2001–2016


It is a good chance to test out a new python package for regression modeling that I have been excited about, the BAyesian Model-Building Interface (Bambi).

In the past, it has sometimes been too much work to include random effects in a regression model in Python. The heart of the methods section in this paper, for example, is this “In our linear mixed effects regression model, we set vaccine coverage as the independent variable and disease incidence per 100,000 as the dependent variable. We also included governorate as a random effect term to control for any correlation in incidence within governorates.”

This sort of model is standard enough that there are multiple R and Stata methods to apply it in one line once you have your data all prepped and loaded. But before Bambi, I didn’t know an easy way to do it in Python. I could code up a custom PyMC model, but that seems like more work than it should be.

So I was pleasantly surprised when I went to replicate some of Haley’s findings that the Bambi code to do this is super-simple:

model = bmb.Model("incidence ~ lagged_coverage + (1|gov)", df, dropna=True)
results = model.fit()

If you want to run it yourself, here is an IPython Notebook that includes the data wrangling and shows that code in context: https://gist.github.com/aflaxman/3fad36937c2d082abf99314061e16db1

Comments Off on Mixed Effects Modeling in Python: country-level random effects with Bambi

Filed under Uncategorized

Comments are closed.