Prediction of mpg for mtcars dataset

Senthil Kumar V.
Feb 22nd 2015

Minimal model & Its prediction

data(mtcars)

mtcars$cyl <- factor(mtcars$cyl)
mtcars$vs <- factor(mtcars$vs)
mtcars$gear <- factor(mtcars$gear)
mtcars$carb <- factor(mtcars$carb)
mtcars$am <- factor(mtcars$am,levels=c(0,1))

mod_min <- lm(mpg ~ wt, data = mtcars)
summary(mod_min)

res_min <- predict(mod_min,data.frame(wt=3.22),
                   interval="confidence")

Optimal model & Its prediction

mod_full <- lm(mpg ~ ., data = mtcars)
mod_opt <- step(mod_full, direction = "both")
summary(mod_opt)
res_opt <- predict(mod_opt,
            data.frame(cyl=factor(4,levels=c(4,6,8)),
            hp=147,wt=3.22,
            am=factor(1,levels=c(0,1))),
            interval="confidence")

Sample results from the two models

round(res_min,2)
    fit   lwr   upr
1 20.08 18.98 21.18
round(res_opt,2)
    fit   lwr   upr
1 22.76 20.07 25.45

Stat. significane of the Optimal model

anova(mod_min,mod_opt)
Analysis of Variance Table

Model 1: mpg ~ wt
Model 2: mpg ~ cyl + hp + wt + am
  Res.Df    RSS Df Sum of Sq      F   Pr(>F)   
1     30 278.32                                
2     26 151.03  4     127.3 5.4787 0.002456 **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1