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.
RELATED MODELING & REGRESSION ERRORS
NA/NaN/Inf in model fit Your data contains NA, NaN, or Inf values that the modeling function cannot hand...
Singular or rank-deficient fit Your model has perfect multicollinearity — some predictors are exact linear comb...
GLM did not converge The iterative algorithm used to fit the generalized linear model failed to find ...
Fitted probabilities 0 or 1 In logistic regression, some predicted probabilities are extremely close to 0 or...