These are the answers to every question in Problem Set 8, written out in full. Read it alongside the problem set rather than instead of it, and attempt each question before you look here.
With the indicator alone, the fitted value is \(17.15\) for every automatic car (\(D = 0\)) and \(17.15 + 7.24\) for every manual car (\(D = 1\)). So the intercept estimates the
mean fuel economy of the automatic cars, \(17.15\) mpg, and the coefficient of
am estimates the difference between the two means: manual
cars do \(7.24\) mpg more on average.
The mean of the manual cars is \(17.1474 +
7.2449 = 24.39\) mpg. The \(t\)
test of this coefficient (\(t^* =
4.11\)) is the two-sample \(t\)
test with equal variances.
\[\text{automatic } (D = 0):\ \hat Y =
37.32 - 5.35\,X, \qquad
\text{manual } (D = 1):\ \hat Y = (37.3216 - 0.0236) - 5.35\,X = 37.30 -
5.35\,X.\] The coefficient of am is the vertical
distance between two parallel lines: at the same
weight, a manual car is estimated to do \(0.02\) mpg less than an automatic car. With
a standard error of \(1.55\) (\(p = 0.99\)), there is no evidence of any
difference.
Why did the \(7.24\) mpg disappear? The manual cars are much lighter: \(2.41\) against \(3.77\) thousand pounds on average. A lighter car goes further on a gallon, by \(5.35\) mpg per thousand pounds according to Model 2. The difference between the two group means splits into two parts, as for the insurance firms of Lecture 16: \[\underbrace{7.24}_{\text{difference of the means}} \approx \underbrace{-0.02}_{\text{same weight}} + \underbrace{(-5.3528)\,(2.41 - 3.77)}_{\text{difference in weight}} = -0.02 + 7.28 = 7.26.\] (The small gap with \(7.24\) comes from the rounding of the two mean weights.) In Model 1 the transmission was standing in for the weight: the comparison mixed the two.
\[\text{automatic } (D = 0):\ \hat Y = 31.42 - 3.79\,X, \qquad \text{manual } (D = 1):\ \hat Y = (31.4161 + 14.8784) + (-3.7859 - 5.2984)\,X = 46.29 - 9.08\,X.\] The slopes are equal when the coefficient \(\beta_3\) of the product is 0. For \(H_0: \beta_3 = 0\) against \(H_a: \beta_3 \neq 0\), \(t^* = -5.2984/1.4447 = -3.67\), and \(|t^*| > t(0.975;\ 28) = 2.048\) (\(p = 0.001\)): we reject \(H_0\). One more thousand pounds costs a manual car about \(9.1\) mpg and an automatic car about \(3.8\) mpg. The parallel lines of Model 2 are not adequate: keep Model 3.
am isIn Model 3 the difference between a manual and an automatic car of weight \(X\) is \[\hat\beta_2 + \hat\beta_3 X = 14.88 - 5.30\,X,\] which depends on the weight. The coefficient \(14.88\) is this difference at \(X = 0\), for cars that weigh nothing: it is not “the effect of a manual transmission”, and its \(p\)-value answers a meaningless question: “what is the difference between a manual and an automatic car that weigh nothing”. This is the first of the three rules of Lecture 16: with an interaction in the model, a main effect is interpreted where the other predictor is 0.
The lines cross where \(14.8784 - 5.2984\,X = 0\), that is at \(X = 14.8784/5.2984 = 2.81\) thousand pounds, inside the range of both groups. Below this weight the manual cars do better; above it, the automatic cars do.
The difference at weight \(X\) is the linear combination \(\theta = \beta_2 + X\beta_3\), estimated by \(\hat\theta = \hat\beta_2 + X\hat\beta_3\), with \[s^2\{\hat\theta\} = s^2\{\hat\beta_2\} + X^2 s^2\{\hat\beta_3\} + 2X\,s\{\hat\beta_2, \hat\beta_3\} = 18.1821 + 2.0872\,X^2 - 2(5.8703)\,X.\]
| Weight \(X\) | \(\hat\theta\) | \(s^2\{\hat\theta\}\) | \(s\{\hat\theta\}\) | 95% interval \(\hat\theta \pm 2.048\,s\{\hat\theta\}\) |
|---|---|---|---|---|
| 2.5 | \(14.8784 - 5.2984(2.5) = 1.63\) | \(18.1821 + 13.0450 - 29.3515 = 1.8756\) | 1.370 | \((-1.17,\ 4.44)\) |
| 3.5 | \(14.8784 - 5.2984(3.5) = -3.67\) | \(18.1821 + 25.5682 - 41.0921 = 2.6582\) | 1.630 | \((-7.01,\ -0.33)\) |
At 2500 pounds the interval contains 0: no clear difference between the two transmissions. At 3500 pounds it lies entirely below 0: a manual car does between 0.3 and 7.0 mpg less than an automatic car of the same weight.
Why 2.5 and 3.5. A comparison “at the same weight” only makes sense at weights where both types of car were observed, here between about 2.5 and 3.6 thousand pounds. At 2 there is no automatic car, and at 5 there is no manual car: one of the two lines would be extrapolated.
One line for all the cars means \(H_0:
\beta_2 = \beta_3 = 0\) in Model 3, two restrictions, against
\(H_a\): at least one of them is not 0.
With the full model (Model 3) and the reduced model
(mpg ~ wt): \[F^* =
\frac{\big(SSE(R) - SSE(F)\big)/2}{SSE(F)/28} = \frac{(278.32 -
188.01)/2}{188.01/28}
= \frac{45.155}{6.715} = 6.72 > F(0.95;\ 2,\ 28) = 3.340.\] We
reject \(H_0\): the transmission
matters, and one line is not enough.
There is no contradiction with Model 2. Model 2 allows only one difference between the two types, the same at every weight, and the best single number is close to 0 because the true difference is positive for light cars and negative for heavy ones: the two cancel. “No difference on average” is not “no difference”.
You can check every number of this exercise in R:
fit3 <- lm(mpg ~ wt * am, data = mtcars)
anova(lm(mpg ~ wt, data = mtcars), fit3)
## Analysis of Variance Table
##
## Model 1: mpg ~ wt
## Model 2: mpg ~ wt * am
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 30 278.32
## 2 28 188.01 2 90.314 6.7253 0.004119 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
cells <- data.frame(
Y = c(150, 86, 49, 288, 157, 131, 184, 109, 279, 235, 224),
X1 = c(0.6, 1.0, 1.4, 0.6, 1.0, 1.0, 1.0, 1.4, 0.6, 1.0, 1.4),
X2 = c(10, 10, 10, 20, 20, 20, 20, 20, 30, 30, 30))
raw <- lm(Y ~ X1 + X2 + I(X1^2) + I(X2^2) + I(X1 * X2), data = cells)
summary(raw)
##
## Call:
## lm(formula = Y ~ X1 + X2 + I(X1^2) + I(X2^2) + I(X1 * X2), data = cells)
##
## Residuals:
## 1 2 3 4 5 6 7 8 9 10
## -21.465 9.263 12.202 41.930 -5.842 -31.842 21.158 -25.404 -20.465 7.263
## 11
## 13.202
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 337.7215 149.9616 2.252 0.0741 .
## X1 -539.5175 268.8603 -2.007 0.1011
## X2 8.9171 9.1825 0.971 0.3761
## I(X1^2) 171.2171 127.1255 1.347 0.2359
## I(X2^2) -0.1061 0.2034 -0.521 0.6244
## I(X1 * X2) 2.8750 4.0468 0.710 0.5092
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 32.37 on 5 degrees of freedom
## Multiple R-squared: 0.9135, Adjusted R-squared: 0.8271
## F-statistic: 10.57 on 5 and 5 DF, p-value: 0.01086
cor(cells$X1, cells$X1^2)
## [1] 0.9910312
The overall test rejects: \(F^* = 10.57\) on 5 and 5 degrees of freedom, \(p = 0.011\). Yet none of the five \(t\) tests is significant at the 5% level. The model explains the response, but the data cannot say which term does the work: this is multicollinearity. The correlation between \(X_1\) and \(X_1^2\) is 0.991. The factor \(1/(1 - r^2)\) of Lecture 16 is already 56 for this pair alone.
cells$x1 <- cells$X1 - mean(cells$X1)
cells$x2 <- cells$X2 - mean(cells$X2)
cen <- lm(Y ~ x1 + x2 + I(x1^2) + I(x2^2) + I(x1 * x2), data = cells)
round(summary(cen)$coefficients, 4)
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 162.8421 16.6076 9.8053 0.0002
## x1 -139.5833 33.0418 -4.2245 0.0083
## x2 7.5500 1.3217 5.7125 0.0023
## I(x1^2) 171.2171 127.1255 1.3468 0.2359
## I(x2^2) -0.1061 0.2034 -0.5214 0.6244
## I(x1 * x2) 2.8750 4.0468 0.7104 0.5092
c(cor(cells$x1, cells$x1^2), max(abs(fitted(raw) - fitted(cen))),
sigma(raw)^2, sigma(cen)^2)
## [1] -6.081730e-16 5.684342e-14 1.048088e+03 1.048088e+03
The linear coefficients change because they do not measure the same thing. In a second-order model the slope in the direction of \(X_1\) is \(\beta_1 + 2\beta_{11}X_1 + \beta_{12}X_2\): it depends on where you are. The coefficient \(\beta_1\) is this slope where the other terms vanish: at \(X_1 = 0\) and \(X_2 = 0\) on the raw scale, far outside the experiment, and at the centre of the design \((1.0, 20)\) on the centred scale. The data say a lot about the slope at the centre and very little about the slope at a charge rate of 0.
first <- lm(Y ~ x1 + x2, data = cells)
anova(first, cen)
## Analysis of Variance Table
##
## Model 1: Y ~ x1 + x2
## Model 2: Y ~ x1 + x2 + I(x1^2) + I(x2^2) + I(x1 * x2)
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 8 7700.3
## 2 5 5240.4 3 2459.9 0.7823 0.5527
By hand, with \(SSE(R) = 7700.3\) on 8 degrees of freedom and \(SSE(F) = 5240.4\) on 5: \[F^* = \frac{(7700.3 - 5240.4)/3}{5240.4/5} = \frac{820.0}{1048.1} = 0.78 < F(0.95;\ 3,\ 5) = 5.41,\] with \(p = 0.55\). We do not reject \(H_0\): there is no evidence that the squares and the product are needed.
It does not matter whether the predictors are centred: the full
models have the same fitted values, hence the same \(SSE\), and so have the two first-order
models (Y ~ X1 + X2 and Y ~ x1 + x2 differ
only by the intercept).
first_raw <- lm(Y ~ X1 + X2, data = cells)
round(summary(first_raw)$coefficients, 4)
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 160.5833 41.6155 3.8587 0.0048
## X1 -139.5833 31.6646 -4.4082 0.0023
## X2 7.5500 1.2666 5.9609 0.0003
confint(first_raw)
## 2.5 % 97.5 %
## (Intercept) 64.617930 256.54874
## X1 -212.602056 -66.56461
## X2 4.629251 10.47075
Ten more degrees: \(10\hat\beta_2 = 75.5\) cycles, with 95% interval \(10 \times (4.63,\ 10.47) = (46,\ 105)\) cycles. Multiplying a coefficient by 10 multiplies both ends of its interval by 10.
ship <- read.csv("ship_speed_fuel.csv")
line <- lm(fuel ~ speed, data = ship)
ggplot(ship, aes(speed, fuel)) + geom_point() +
geom_smooth(method = "lm", se = FALSE) +
labs(x = "Speed (knots)", y = "Fuel (tons per day)")
ship$e_line <- resid(line)
ggplot(ship, aes(speed, e_line)) + geom_hline(yintercept = 0, colour = "grey60") +
geom_point() + geom_smooth(method = "loess", se = FALSE, colour = "darkorange") +
labs(x = "Speed (knots)", y = "Residual")
summary(line)$r.squared
## [1] 0.9806942
The line has \(R^2 = 0.981\), and it is still not adequate. The residuals are positive at the slowest leg, negative around 17 knots, positive from about 17.6 to 19 knots and negative again at the fastest leg: the points follow an S shape around the line. A high \(R^2\) does not say that the shape is right. The residual plot does.
ship$x <- ship$speed - mean(ship$speed)
cubic <- lm(fuel ~ x + I(x^2) + I(x^3), data = ship)
round(summary(cubic)$coefficients, 4)
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 58.7020 0.2324 252.5657 0.0000
## x 13.3245 0.2993 44.5181 0.0000
## I(x^2) 0.7779 0.2152 3.6157 0.0023
## I(x^3) -1.1479 0.1384 -8.2939 0.0000
anova(lm(fuel ~ x, data = ship), cubic)
## Analysis of Variance Table
##
## Model 1: fuel ~ x
## Model 2: fuel ~ x + I(x^2) + I(x^3)
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 18 46.369
## 2 16 8.228 2 38.141 37.083 9.831e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
c(cor(ship$speed, ship$speed^2), cor(ship$x, ship$x^2))
## [1] 0.9996011 0.4402852
\(H_0: \beta_{11} = \beta_{111} = 0\) is rejected: \(F^* = 37.1\) on 2 and 16 degrees of freedom, \(p < 0.001\). The curvature is real, and the cubic term is needed (\(t^* = -8.29\)).
On the raw scale, speed and speed^2 have a
correlation of 0.9996. Centring brings it down to 0.44. It does not go
to 0 as for the power cells, because the speeds are not spread
symmetrically around their mean.
pow <- lm(log(fuel) ~ log(speed), data = ship)
round(summary(pow)$coefficients, 4)
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -5.4420 0.3452 -15.7663 0
## log(speed) 3.3143 0.1204 27.5305 0
confint(pow)
## 2.5 % 97.5 %
## (Intercept) -6.167214 -4.716867
## log(speed) 3.061352 3.567192
The model is \(\ln(\text{fuel}) = \beta_0 + \beta_1 \ln(\text{speed}) + \varepsilon\), that is \(\text{fuel} \propto \text{speed}^{\beta_1}\): both sides are in logs, the last row of the table of Lecture 18.
These statements are about the median fuel consumption. The 95% interval for the exponent is \((3.06,\ 3.57)\).
Test of the cube law, \(H_0: \beta_1 = 3\) against \(H_a: \beta_1 \neq 3\): \[t^* = \frac{3.3143 - 3}{0.1204} = 2.61, \qquad t(0.975;\ 18) = 2.101.\] \(|t^*|\) exceeds the critical value (and 3 is outside the interval): for this ship, over these speeds, the consumption grows a little faster than the cube of the speed. Note that the null value is 3, not 0, so the \(t\) value printed by R is not the one to use.
new <- data.frame(speed = c(18, 22))
new$x <- new$speed - mean(ship$speed)
data.frame(speed = new$speed,
line = predict(line, new),
cubic = predict(cubic, new),
power = exp(predict(pow, new)))
## speed line cubic power
## 1 18 63.31553 64.01579 62.64211
## 2 22 108.02971 34.84176 121.81640
At 18 knots, in the middle of the data, the three models agree within about 1.5 tons per day. At 22 knots, beyond the fastest leg (19.8 knots), they are far apart, and the cubic predicts 35 tons per day, little more than half of what the ship burns at 18 knots: its fitted curve turns down just after the last observation. This is the comic of Lecture 17: a polynomial follows the data inside their range and can do anything outside. The power law cannot turn, and its shape has a physical justification, so its extrapolation is the least unreasonable of the three. None of them is supported by data at 22 knots.
No: \(R^2\), adjusted \(R^2\) and AIC compare models for the
same response. The \(SST\) of fuel and the \(SST\) of log(fuel) are
different quantities, in different units, and so are the two values of
\(SSE\): \(R^2 = 0.997\) for one response and \(0.977\) for another cannot be ranked.
A fair comparison puts both models on the same scale, for instance the errors in tons per day:
c(line = sum(resid(line)^2),
cubic = sum(resid(cubic)^2),
power = sum((ship$fuel - exp(fitted(pow)))^2))
## line cubic power
## 46.368604 8.228089 69.282530
On the original scale, over the range of the data, the cubic is by far the closest to the observations. The power law is the furthest, further even than the straight line: a single exponent cannot follow the S shape seen in (a). The cubic also uses four parameters against two. Which to report depends on the aim: to describe the consumption between 16 and 20 knots, the cubic; to state a law that can be read (“1% faster, 3.3% more fuel”) and that behaves sensibly outside the data, the power law, while saying that it misses part of the pattern. The residual plot of each model, not shown here, should be part of the comparison.