Author: Hillary McAllister Date: 2026-10-03 Output: html_document
data(mtcars)
cars_lm <- lm(mpg ~ wt + hp, data = mtcars)
summary(cars_lm)
##
## Call:
## lm(formula = mpg ~ wt + hp, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.941 -1.600 -0.182 1.050 5.854
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 37.22727 1.59879 23.285 < 2e-16 ***
## wt -3.87783 0.63273 -6.129 1.12e-06 ***
## hp -0.03177 0.00903 -3.519 0.00145 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.593 on 29 degrees of freedom
## Multiple R-squared: 0.8268, Adjusted R-squared: 0.8148
## F-statistic: 69.21 on 2 and 29 DF, p-value: 9.109e-12
MPG = 37.23 - 3.88wt - .03hp
coef(cars_lm)
## (Intercept) wt hp
## 37.22727012 -3.87783074 -0.03177295
WT- If a car gets 1,000 pounds heavier, its mpg goes down by about 3.88 miles per gallon as long as the horsepower stays the same.
HP- If a car gets 1 more horsepower, its mpg goes down by about 0.03 miles per gallon as long as the weight stays the same.
When using linear regression, we assume 1. Linearity- relationship between wt, hp, and mpg is linear 2. Independence- observatiosn are independent 3. Constant variance- consistent variability 4. Normality- normal distribution
par(mfrow = c(2, 2))
plot(cars_lm)
Assumptions are mostly met. The relationship is mostly linear but not completely. The distribution of data is mostly consistent and normal with mostly consistent variability. The points remain relatively close to the line.
cars_mse <- mean(residuals(cars_lm)^2)
cars_mse
## [1] 6.095242
R-squared measures the proportion of variation in mpg explained by the predictors in the model.
cars_rs <- summary(cars_lm)$r.squared
cars_rs
## [1] 0.8267855
cars_it <- lm(mpg ~ wt * hp, data = mtcars)
summary(cars_it)
##
## Call:
## lm(formula = mpg ~ wt * hp, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.0632 -1.6491 -0.7362 1.4211 4.5513
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 49.80842 3.60516 13.816 5.01e-14 ***
## wt -8.21662 1.26971 -6.471 5.20e-07 ***
## hp -0.12010 0.02470 -4.863 4.04e-05 ***
## wt:hp 0.02785 0.00742 3.753 0.000811 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.153 on 28 degrees of freedom
## Multiple R-squared: 0.8848, Adjusted R-squared: 0.8724
## F-statistic: 71.66 on 3 and 28 DF, p-value: 2.981e-13
R-squared increases from .83 to .88. The weight on mpg now depends on hp. With a positive interaction coeff, weight negatively impacts mpg less as hp increases.
lower_bound_hp <- quantile(mtcars$hp, 0.05, na.rm = TRUE)
upper_bound_hp <- quantile(mtcars$hp, 0.95, na.rm = TRUE)
lower_bound_hp
## 5%
## 63.65
upper_bound_hp
## 95%
## 253.55
mtcars$hp_winsor <- mtcars$hp
mtcars$hp_winsor[mtcars$hp < lower_bound_hp] <- lower_bound_hp
mtcars$hp_winsor[mtcars$hp > upper_bound_hp] <- upper_bound_hp
cars_winhp <- lm(mpg ~ wt + hp_winsor, data = mtcars)
summary(cars_winhp)
##
## Call:
## lm(formula = mpg ~ wt + hp_winsor, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.8825 -1.6545 -0.0968 0.8367 5.7259
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 37.31722 1.56964 23.774 < 2e-16 ***
## wt -3.58279 0.66427 -5.394 8.5e-06 ***
## hp_winsor -0.03952 0.01059 -3.732 0.000824 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.546 on 29 degrees of freedom
## Multiple R-squared: 0.833, Adjusted R-squared: 0.8215
## F-statistic: 72.34 on 2 and 29 DF, p-value: 5.348e-12
R-squared increases from .827 to .83, a small change with winsorization. Wt is still signifiant but is now -3.58 vs. -3.88. Hp is slightly smaller from -.032 to -.04 The newly fitted model is not a huge improvement.
library(car)
## Loading required package: carData
vif(cars_lm)
## wt hp
## 1.766625 1.766625
VIF values for wt and hp are both close to 1 at 1.77 for each. The coefficients are very slightly correlated and are reliable.