mpg wt hp
Min. :10.40 Min. :1.513 Min. : 52.0
1st Qu.:15.43 1st Qu.:2.581 1st Qu.: 96.5
Median :19.20 Median :3.325 Median :123.0
Mean :20.09 Mean :3.217 Mean :146.7
3rd Qu.:22.80 3rd Qu.:3.610 3rd Qu.:180.0
Max. :33.90 Max. :5.424 Max. :335.0
2.3 Scatterplots
Code
p_wt <-ggplot(d, aes(x = wt, y = mpg)) +geom_point() +labs(x ="Weight (1000 lbs)", y ="Fuel efficiency (mpg)")p_hp <-ggplot(d, aes(x = hp, y = mpg)) +geom_point() +labs(x ="Horsepower", y ="Fuel efficiency (mpg)")p_wt + p_hp
2.4 Single-predictor models
Code
m_wt <-lm(mpg ~ wt, data = d)m_hp <-lm(mpg ~ hp, data = d)summary(m_wt)
Call:
lm(formula = mpg ~ wt, data = d)
Residuals:
Min 1Q Median 3Q Max
-4.5432 -2.3647 -0.1252 1.4096 6.8727
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 37.2851 1.8776 19.858 < 2e-16 ***
wt -5.3445 0.5591 -9.559 1.29e-10 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 3.046 on 30 degrees of freedom
Multiple R-squared: 0.7528, Adjusted R-squared: 0.7446
F-statistic: 91.38 on 1 and 30 DF, p-value: 1.294e-10
Code
summary(m_hp)
Call:
lm(formula = mpg ~ hp, data = d)
Residuals:
Min 1Q Median 3Q Max
-5.7121 -2.1122 -0.8854 1.5819 8.2360
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 30.09886 1.63392 18.421 < 2e-16 ***
hp -0.06823 0.01012 -6.742 1.79e-07 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 3.863 on 30 degrees of freedom
Multiple R-squared: 0.6024, Adjusted R-squared: 0.5892
F-statistic: 45.46 on 1 and 30 DF, p-value: 1.788e-07
2.5 Multiple regression
Code
m_both <-lm(mpg ~ wt + hp, data = d)summary(m_both)
Call:
lm(formula = mpg ~ wt + hp, data = d)
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
2.5.1 Type II sums of squares (adjusted)
Code
Anova(m_both)
Anova Table (Type II tests)
Response: mpg
Sum Sq Df F value Pr(>F)
wt 252.627 1 37.561 1.12e-06 ***
hp 83.274 1 12.381 0.001451 **
Residuals 195.048 29
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
2.5.2 Type I sums of squares (sequential)
Code
anova(m_both)
Analysis of Variance Table
Response: mpg
Df Sum Sq Mean Sq F value Pr(>F)
wt 1 847.73 847.73 126.041 4.488e-12 ***
hp 1 83.27 83.27 12.381 0.001451 **
Residuals 29 195.05 6.73
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Code
m_both_swapped <-lm(mpg ~ hp + wt, data = d)anova(m_both_swapped)
Analysis of Variance Table
Response: mpg
Df Sum Sq Mean Sq F value Pr(>F)
hp 1 678.37 678.37 100.862 5.987e-11 ***
wt 1 252.63 252.63 37.561 1.120e-06 ***
Residuals 29 195.05 6.73
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1