# Categorical Regression#

In this example, we will use the categorical family to model outcomes with more than two categories. The examples in this notebook were constructed by Tomás Capretto, and assembled into this example by Tyler James Burch (@tjburch on GitHub).

[1]:

import arviz as az
import bambi as bmb
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

from matplotlib.lines import Line2D

[2]:

SEED = 1234
az.style.use("arviz-darkgrid")


When modeling binary outcomes with Bambi, the Bernoulli family is used. The multivariate generalization of the Bernoulli family is the Categorical family, and with it, we can model an arbitrary number of outcome categories.

## Example with toy data#

To start, we will create a toy dataset with three classes.

[3]:

rng = np.random.default_rng(SEED)
x = np.hstack([rng.normal(m, s, size=50) for m, s in zip([-2.5, 0, 2.5], [1.2, 0.5, 1.2])])
y = np.array(["A"] * 50 + ["B"] * 50 + ["C"] * 50)

colors = ["C0"] * 50 + ["C1"] * 50 + ["C2"] * 50
plt.scatter(x, np.random.uniform(size=150), color=colors)
plt.xlabel("x")
plt.ylabel("y");


Here we have 3 classes, generated from three normal distributions: $$N(-2.5, 1.2)$$, $$N(0, 0.5)$$, and $$N(2.5, 1.2)$$. Creating a model to fit these distributions,

[4]:

data = pd.DataFrame({"y": y, "x": x})
model = bmb.Model("y ~ x", data, family="categorical")
idata = model.fit()

/home/osvaldo/anaconda3/lib/python3.9/site-packages/aesara/tensor/nnet/basic.py:1116: FutureWarning: Softmax now accepts an axis argument. For backwards-compatibility it defaults to -1 when not specified, but in the future the default will be None.
To suppress this warning specify axis explicitly.
warnings.warn(
Auto-assigning NUTS sampler...
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [Intercept, x]

100.00% [8000/8000 00:02<00:00 Sampling 4 chains, 0 divergences]
Sampling 4 chains for 1_000 tune and 1_000 draw iterations (4_000 + 4_000 draws total) took 3 seconds.


Note that we pass the family="categorical" argument to Bambi’s Model method in order to call the categorical family. Here, the response variable are strings (“A”, “B”, “C”), however they can also be pd.Categorical objects.

Next we will use posterior predictions to visualize the mean class probability across the $$x$$ spectrum.

[6]:

x_new = np.linspace(-5, 5, num=200)
model.predict(idata, data=pd.DataFrame({"x": x_new}))
p = idata.posterior["y_mean"].sel(draw=slice(0, None, 10))

[8]:

x_new = np.linspace(-5, 5, num=200)
model.predict(idata, data=pd.DataFrame({"x": x_new}))
p = idata.posterior["y_mean"].sel(draw=slice(0, None, 10))

for j, g in enumerate("ABC"):
plt.plot(x_new, p.sel({"y_mean_dim":g}).stack(samples=("chain", "draw")), color=f"C{j}", alpha=0.2)

plt.xlabel("x")
plt.ylabel("y");


Here, we can notice that the probability phases between classes from left to right. At all points across $$x$$, sum of the class probabilities is 1, since in our generative model, it must be one of these three outcomes.

## The iris dataset#

Next, we will look at the classic “iris” dataset, which contains samples from 3 different species of iris plants. Using properties of the plant, we will try to model its species.

[9]:

iris = sns.load_dataset("iris")

[9]:

sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa

The dataset includes four different properties of the plants: it’s sepal length, sepal width, petal length, and petal width. There are 3 different class possibilities: setosa, versicolor, and virginica.

[10]:

sns.pairplot(iris, hue="species");

/home/osvaldo/anaconda3/lib/python3.9/site-packages/seaborn/axisgrid.py:88: UserWarning: This figure was using constrained_layout, but that is incompatible with subplots_adjust and/or tight_layout; disabling constrained_layout.
self._figure.tight_layout(*args, **kwargs)


We can see the three species have several distinct characteristics, which our linear model can capture to distinguish between them.

[11]:

model = bmb.Model(
"species ~ sepal_length + sepal_width + petal_length + petal_width",
iris,
family="categorical",
)
idata = model.fit()
az.summary(idata)

/home/osvaldo/anaconda3/lib/python3.9/site-packages/aesara/tensor/nnet/basic.py:1116: FutureWarning: Softmax now accepts an axis argument. For backwards-compatibility it defaults to -1 when not specified, but in the future the default will be None.
To suppress this warning specify axis explicitly.
warnings.warn(
Auto-assigning NUTS sampler...
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [Intercept, sepal_length, sepal_width, petal_length, petal_width]

100.00% [8000/8000 00:13<00:00 Sampling 4 chains, 0 divergences]
Sampling 4 chains for 1_000 tune and 1_000 draw iterations (4_000 + 4_000 draws total) took 14 seconds.

[11]:

mean sd hdi_3% hdi_97% mcse_mean mcse_sd ess_bulk ess_tail r_hat
Intercept[versicolor] -6.505 8.000 -20.472 9.049 0.181 0.128 1959.0 2437.0 1.0
Intercept[virginica] -22.408 9.626 -40.654 -4.470 0.197 0.140 2369.0 2424.0 1.0
sepal_length[versicolor] 3.052 1.725 -0.313 6.163 0.045 0.034 1517.0 1437.0 1.0
sepal_length[virginica] 2.285 1.807 -0.960 5.771 0.046 0.034 1553.0 1841.0 1.0
sepal_width[versicolor] -4.722 1.887 -8.352 -1.299 0.046 0.033 1701.0 1975.0 1.0
sepal_width[virginica] -6.609 2.291 -10.921 -2.401 0.054 0.039 1790.0 2111.0 1.0
petal_length[versicolor] 1.075 0.887 -0.556 2.733 0.021 0.015 1841.0 1748.0 1.0
petal_length[virginica] 3.999 1.014 2.017 5.881 0.021 0.015 2237.0 1957.0 1.0
petal_width[versicolor] 1.961 2.035 -1.830 5.893 0.049 0.037 1754.0 1775.0 1.0
petal_width[virginica] 9.086 2.247 5.023 13.443 0.050 0.036 1989.0 2218.0 1.0
[12]:

az.plot_trace(idata);


We can see that this has fit quite nicely. You’ll notice there are $$n-1$$ parameters to fit, where $$n$$ is the number of categories. In the minimal binary case, recall there’s only one parameter set, since it models probability $$p$$ of being in a class, and probability $$1-p$$ of being in the other class. Using the categorical distribution, this extends, so we have $$p_1$$ for class 1, $$p_2$$ for class 2, and $$1-(p_1+p_2)$$ for the final class.

## Using numerical and categorical predictors#

Next we will look at an example from chapter 8 of Alan Agresti’s Categorical Data Analysis, looking at the primary food choice for 64 alligators caught in Lake George, Florida. We will use their length (a continuous variable) and sex (a categorical variable) as predictors to model their food choice.

First, reproducing the dataset,

[13]:

length = [
1.3, 1.32, 1.32, 1.4, 1.42, 1.42, 1.47, 1.47, 1.5, 1.52, 1.63, 1.65, 1.65, 1.65, 1.65,
1.68, 1.7, 1.73, 1.78, 1.78, 1.8, 1.85, 1.93, 1.93, 1.98, 2.03, 2.03, 2.31, 2.36, 2.46,
3.25, 3.28, 3.33, 3.56, 3.58, 3.66, 3.68, 3.71, 3.89, 1.24, 1.3, 1.45, 1.45, 1.55, 1.6,
1.6, 1.65, 1.78, 1.78, 1.8, 1.88, 2.16, 2.26, 2.31, 2.36, 2.39, 2.41, 2.44, 2.56, 2.67,
2.72, 2.79, 2.84
]
choice = [
"I", "F", "F", "F", "I", "F", "I", "F", "I", "I", "I", "O", "O", "I", "F", "F",
"I", "O", "F", "O", "F", "F", "I", "F", "I", "F", "F", "F", "F", "F", "O", "O",
"F", "F", "F", "F", "O", "F", "F", "I", "I", "I", "O", "I", "I", "I", "F", "I",
"O", "I", "I", "F", "F", "F", "F", "F", "F", "F", "O", "F", "I", "F", "F"
]

sex = ["Male"] * 32 + ["Female"] * 31
data = pd.DataFrame({"choice": choice, "length": length, "sex": sex})
data["choice"]  = pd.Categorical(
data["choice"].map({"I": "Invertebrates", "F": "Fish", "O": "Other"}),
["Other", "Invertebrates", "Fish"],
ordered=True
)

[13]:

choice length sex
0 Invertebrates 1.30 Male
1 Fish 1.32 Male
2 Fish 1.32 Male

Next, constructing the model,

[14]:

model = bmb.Model("choice ~ length + sex", data, family="categorical")
idata = model.fit()

/home/osvaldo/anaconda3/lib/python3.9/site-packages/aesara/tensor/nnet/basic.py:1116: FutureWarning: Softmax now accepts an axis argument. For backwards-compatibility it defaults to -1 when not specified, but in the future the default will be None.
To suppress this warning specify axis explicitly.
warnings.warn(
Auto-assigning NUTS sampler...
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [Intercept, length, sex]

100.00% [8000/8000 00:04<00:00 Sampling 4 chains, 0 divergences]
Sampling 4 chains for 1_000 tune and 1_000 draw iterations (4_000 + 4_000 draws total) took 5 seconds.


We can then look at how the food choices vary by length for both male and female alligators.

[15]:

new_length = np.linspace(1, 4)
new_data = pd.DataFrame({"length": np.tile(new_length, 2), "sex": ["Male"] * 50 + ["Female"] * 50})
model.predict(idata, data=new_data)
p = idata.posterior["choice_mean"]

fig, axes = plt.subplots(1, 2, figsize=(12, 5))
choices = ["Other", "Invertebrates", "Fish"]

for j, choice in enumerate(choices):
males = p.sel({"choice_mean_dim":choice, "choice_obs":slice(0, 49)})
females = p.sel({"choice_mean_dim":choice, "choice_obs":slice(50, 100)})
axes[0].plot(new_length, males.mean(("chain", "draw")), color=f"C{j}", lw=2)
axes[1].plot(new_length, females.mean(("chain", "draw")), color=f"C{j}", lw=2)
az.plot_hdi(new_length, males, color=f"C{j}", ax=axes[0])
az.plot_hdi(new_length, females, color=f"C{j}", ax=axes[1])

axes[0].set_title("Male")
axes[1].set_title("Female")

handles = [Line2D([], [], color=f"C{j}", label=choice) for j, choice in enumerate(choices)]

fig.legend(
handles,
choices,
loc="center right",
ncol=3,
bbox_to_anchor=(0.99, 0.95),
bbox_transform=fig.transFigure
);

/tmp/ipykernel_66835/2612338171.py:21: UserWarning: This figure was using constrained_layout, but that is incompatible with subplots_adjust and/or tight_layout; disabling constrained_layout.


Here we can see that the larger male and female alligators are, the less of a taste they have for invertebrates, and far prefer fish. Additionally, males seem to have a higher propensity to consume “other” foods compared to females at any size. Of note, the posterior means predicted by Bambi contain information about all $$n$$ categories (despite having only $$n-1$$ coefficients), so we can directly construct this plot, rather than manually calculating $$1-(p_1+p_2)$$ for the third class.

Last, we can make a posterior predictive plot,

[16]:

model.predict(idata, kind="pps")

ax = az.plot_ppc(idata)
ax.set_xticks([0.5, 1.5, 2.5])
ax.set_xticklabels(model.response.levels)
ax.set_xlabel("Choice");
ax.set_ylabel("Probability");


which depicts posterior predicted probability for each possible food choice for an alligator, which reinforces fish being the most likely food choice, followed by invertebrates.

### References#

Agresti, A. (2013) Categorical Data Analysis. 3rd Edition, John Wiley & Sons Inc., Hoboken.