August 31, 2025

1) The Idea

2) Data & Model (executed)

data(mtcars)
mt <- within(mtcars, {
  am <- factor(am, levels = c(0,1), labels = c("Automatic","Manual"))
})
fit <- lm(mpg ~ wt + hp + am, data = mt)
summary(fit)
## 
## Call:
## lm(formula = mpg ~ wt + hp + am, data = mt)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.4221 -1.7924 -0.3788  1.2249  5.5317 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 34.002875   2.642659  12.867 2.82e-13 ***
## wt          -2.878575   0.904971  -3.181 0.003574 ** 
## hp          -0.037479   0.009605  -3.902 0.000546 ***
## amManual     2.083710   1.376420   1.514 0.141268    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.538 on 28 degrees of freedom
## Multiple R-squared:  0.8399, Adjusted R-squared:  0.8227 
## F-statistic: 48.96 on 3 and 28 DF,  p-value: 2.908e-11

3) Quick Demo (executed)

# Example inputs: 3.0 (1000 lbs), 110 hp, Automatic
nd <- data.frame(
  wt = 3.0,
  hp = 110,
  am = factor("Automatic", levels = c("Automatic","Manual"))
)
predict(fit, newdata = nd, interval = "prediction", level = 0.95)
##        fit     lwr      upr
## 1 21.24449 15.8155 26.67348

4) Links & How to Run