Get Started →
Modeling & Regression Errors

NA/NaN/Inf in model fit

Matches: na\/nan\/inf in ['"]?(x|y)|NA.*NaN.*Inf.*foreign function

WHAT THIS ERROR MEANS

Your data contains NA, NaN, or Inf values that the modeling function cannot handle. Most R model fitting functions don't tolerate missing or infinite values.

HOW TO FIX IT

1. Remove rows with missing values: na.omit(df) or drop_na().

2. Replace Inf with NA: df[is.infinite(df)] <- NA.

3. Use the na.action parameter in the model call.

CODE EXAMPLES

BAD — THIS CAUSES THE ERROR
lm(y ~ x, data = df)  # df contains NA values
GOOD — CORRECT APPROACH
lm(y ~ x, data = na.omit(df))
# or
lm(y ~ x, data = df, na.action = na.exclude)

Still stuck?

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

Try RChat Free →

RELATED MODELING & REGRESSION ERRORS