Prediction and model comparison in R
Using a fitted model and testing it against a simpler one.
model <- lm(mpg ~ wt + hp, data = mtcars)
new_cars <- data.frame(wt = c(2.5, 3.5), hp = c(110, 180))
point <- predict(model, newdata = new_cars)
with_interval <- predict(model, new_cars, interval = "confidence")
rmse <- sqrt(mean(resid(model)^2))
aic <- AIC(model)
reduced <- update(model, . ~ . - hp)
comparison <- anova(reduced, model)
How it works
predictwithnewdatascores unseen rows.interval = "confidence"adds uncertainty bounds.anovacompares nested models;AICscores fit.
Keywords and builtins used here
AICanovaclmmeanpredictresidsqrtupdate
The run, in numbers
- Lines
- 10
- Characters to type
- 339
- Tokens
- 99
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 59 seconds.
Step 4 of 4 in Correlation & regression, step 9 of 11 in Statistics.