Get Started →
Modeling & Regression Errors

Contrasts require 2+ factor levels

Matches: contrasts can be applied only to factors with 2 or more levels

WHAT THIS ERROR MEANS

A factor variable in your model has only one level (or zero after removing NAs). R needs at least two levels to create contrast (dummy) variables.

HOW TO FIX IT

1. Check factor levels with levels() and table().

2. Remove single-level factors from the model.

3. Filter out NA values that may be reducing levels.

CODE EXAMPLES

BAD — THIS CAUSES THE ERROR
# region has only one unique value after filtering
lm(price ~ region + size, data = df)
GOOD — CORRECT APPROACH
# Remove constant factors
df_clean <- df[df$region %in% names(which(table(df$region) > 0)), ]
lm(price ~ size, data = df_clean)

Still stuck?

Paste your code in RChat and the AI will fix this error in context.

Try RChat Free →

RELATED MODELING & REGRESSION ERRORS