Get Started →
Modeling & Regression Errors

Factor has new levels in predict

Matches: factor .* has new levels

WHAT THIS ERROR MEANS

Your prediction data contains factor levels that weren't present in the training data. The model doesn't know how to handle unseen categories.

HOW TO FIX IT

1. Ensure test data only contains factor levels present in training data.

2. Combine rare levels into an 'Other' category before training.

3. Remove rows with unseen factor levels from prediction data.

CODE EXAMPLES

BAD — THIS CAUSES THE ERROR
# Training had sectors: Tech, Finance
# Test has sector: Healthcare
predict(model, new_data)
GOOD — CORRECT APPROACH
# Ensure consistent factor levels
new_data$sector <- factor(new_data$sector,
  levels = levels(train_data$sector))

Still stuck?

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

Try RChat Free →

RELATED MODELING & REGRESSION ERRORS