Course contentsShow
← Data Science
Lesson 646 of 2,14517. Categorical Predictors and InteractionsFree lesson

Reference Categories in Statistical Software

How Python, R, and other tools handle default reference selection and methods to override defaults.

Reference Categories in Statistical Software

You'll learn how Python, R, and other tools automatically pick a reference category—and how to override that default when needed.

How Software Handles Reference Categories

When you feed categorical variables into regression models, statistical software must convert them to dummy variables—and that means choosing one category as the reference. Different tools have different defaults:

Python (pandas/statsmodels):
By default, Python uses alphabetical order to select the reference category. If your variable has categories ['high', 'low', 'medium'], 'high' becomes the reference because it comes first alphabetically. This can be counterintuitive!

R:
R also defaults to alphabetical order for character variables, but if you use factors (R's special categorical type), you can control the order explicitly using factor() with the levels argument.

Think of it like this: Software is like an assistant organizing files—it needs a rule to decide which folder to put first. Alphabetical is simple and consistent, but not always meaningful for your analysis.

Overriding the Defaults

In Python:

# Explicitly set reference category
df['category'] = pd.Categorical(df['category'], 
                                categories=['low', 'medium', 'high'])

In R:

# Make 'low' the reference
df$category <- relevel(factor(df$category), ref = "low")

Why override? You might want the most common category, a natural baseline (like "control" in experiments), or simply something interpretable as your reference.

Key Takeaway: Statistical software defaults to alphabetical ordering for reference categories, but you should actively choose a meaningful baseline using pd.Categorical() in Python or relevel() in R to make your coefficients interpretable.