I am trying to figure out why predict
does not show NA for the predictions based on combinations for which the coefficient on the interaction is NA. Instead it simply ignores the interaction (treat it as zero) and calculate a prediction based on all the other coefficients. It does provide a warning, but there is no information about which combinations are not present in the original.
# Predict with missing coefficients
model_results <- lm(mpg ~ wt + factor(am)*factor(carb), data = mtcars)
# Generate new data
wt <- c(3)
am <- c(0:1)
carb <- c(1:4, 6, 8)
# expand.grid provides all possible combinations of variables
prediction_data <- expand.grid(wt = wt,
am = am,
carb = carb)
# Get a prediction even if combination relies on an NA coefficient
predicted_mpg <- data.frame(
predict(
model_results, prediction_data, interval = "confidence"
),
prediction_data
)
#> Warning in predict.lm(model_results, prediction_data, interval =
#> "confidence"): prediction from a rank-deficient fit may be misleading
Created on 2019-06-13 by the reprex package (v0.2.1)
If there is no way of making predict
show NA when relying on NA coefficients, is there an easy way to check which interaction coefficients are NA and get rid of the predictions in those cases?