Bayesian Regression for Survey Data with PyMC#
Allen Downey and Alexander Fengler
In this tutorial, we explore Bayesian regression using PyMC – the primary library for Bayesian sampling in Python – focusing on survey data and other datasets with categorical outcomes. Starting with logistic regression, we’ll build up to categorical and ordered logistic regression, showcasing how Bayesian approaches provide versatile tools for developing and evaluating complex models. Participants will leave with practical skills for implementing Bayesian regression models in PyMC, along with a deeper appreciation for the power of Bayesian inference in real-world data analysis.
The examples use data from the General Social Survey (GSS). We presented this tutorial at ODSC East 2025. Its companion, Survey Data with Pandas, uses the same GSS data.
Level: intermediate. You should be comfortable with Python, NumPy and pandas, and basic statistics. You don’t need any experience with Bayesian methods or PyMC.
Length: about 90 minutes.
The notebooks#
The pages on this site show the notebooks with solutions; click on a hidden cell to see it. To do the exercises, run the versions with blanks on Colab:
Introduction to Bayesian sampling: using PyMC as a random number generator, then building up a model from an uncertain probability, a latent propensity, and observed data. Exercises on Colab
Logistic regression: whether people say other people can be trusted, as a function of time and political party. Priors, the posterior distribution, sampling diagnostics, centering, and predictions. Exercises on Colab
Categorical regression: the 2020 presidential vote as a function of age and gender. Exercises on Colab
Ordinal regression: how happy people say they are, as a function of age and birth cohort. Exercises on Colab
Running the notebooks#
The easiest way to run the notebooks is on Colab: click one of the links above. You need a Google account, but you don’t have to install anything. The notebooks install nutpie, the sampler they use, and download the data they need.
To run them on your own computer, clone the repository:
git clone https://github.com/AllenDowney/SurveyDataPyMC.git
cd SurveyDataPyMC
Then install the packages with pip:
pip install -r requirements.txt jupyter
Or create a conda environment with mamba (or conda):
mamba env create -f environment.yml
conda activate SurveyDataPyMC
Then run jupyter lab and open the notebooks in the notebooks folder.
Next steps#
Survey Data with Pandas, the companion tutorial: exploring the same GSS data with pandas and StatsModels.
Think Bayes, which is free online.
The PyMC documentation and example gallery.
The code is available under the MIT License. The text is available under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.