typestar

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

  1. predict with newdata scores unseen rows.
  2. interval = "confidence" adds uncertainty bounds.
  3. anova compares nested models; AIC scores fit.

Keywords and builtins used here

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.

Type this snippet

Step 4 of 4 in Correlation & regression, step 9 of 11 in Statistics.

← Previous Next →