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.
RELATED MODELING & REGRESSION ERRORS
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...
Factor has new levels in predict Your prediction data contains factor levels that weren't present in the training...