Full worked answers are in the solutions sheet.
Exercise 1 is pen-and-paper: work it out with a calculator first, as you would in a quiz, and only then check your numbers in R. Exercises 2 and 3 are done in R.
The file ship_speed_fuel.csv comes with this sheet: keep
it in the same folder. The other datasets ship with R or are typed in
the sheet. All figures are made with ggplot2.
The dataset mtcars, which ships with R, describes 32
cars (1973 and 1974 models) from the road tests of a magazine. We use
the fuel economy mpg (miles per gallon, the response \(Y\)), the weight wt (in
thousands of pounds, \(X\)) and the
transmission am (\(D = 0\)
for the 19 automatic cars, \(D = 1\)
for the 13 manual cars). The automatic cars weigh between 2.47 and 5.42
thousand pounds, with a mean of 3.77; the manual cars between 1.51 and
3.57, with a mean of 2.41. Three models are fitted:
> summary(lm(mpg ~ am, data = mtcars)) # Model 1
Estimate Std. Error t value Pr(>|t|)
(Intercept) 17.1474 1.1246 15.2475 0.0000
am 7.2449 1.7644 4.1061 0.0003
Residual standard error: 4.902 on 30 degrees of freedom
> summary(lm(mpg ~ wt + am, data = mtcars)) # Model 2
Estimate Std. Error t value Pr(>|t|)
(Intercept) 37.3216 3.0546 12.2180 0.0000
wt -5.3528 0.7882 -6.7908 0.0000
am -0.0236 1.5456 -0.0153 0.9879
Residual standard error: 3.098 on 29 degrees of freedom
> fit3 <- lm(mpg ~ wt * am, data = mtcars) # Model 3
> summary(fit3)
Estimate Std. Error t value Pr(>|t|)
(Intercept) 31.4161 3.0201 10.4023 0.0000
wt -3.7859 0.7856 -4.8188 0.0000
am 14.8784 4.2640 3.4893 0.0016
wt:am -5.2984 1.4447 -3.6674 0.0010
Residual standard error: 2.591 on 28 degrees of freedom
> round(vcov(fit3), 4)[3:4, 3:4]
am wt:am
am 18.1821 -5.8703
wt:am -5.8703 2.0872
The error sums of squares are \(SSE =
278.32\) for mpg ~ wt, a single line fitted to all
32 cars whatever their transmission, and \(SSE
= 188.01\) for Model 3. You may use \(t(0.975;\ 28) = 2.048\) and \(F(0.95;\ 2,\ 28) = 3.340\).
Questions.
am. The difference of 7.24 mpg
of Model 1 has disappeared: explain why, using the weights given
above.In Lecture 17 the second-order model for the power cells was fitted on the raw and on the centred predictors, and one question was left for this sheet. The data: \(Y\) is the number of cycles before the cell fails, \(X_1\) the charge rate (in amperes) and \(X_2\) the temperature (in degrees Celsius).
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))
Questions.
Y ~ X1 + X2 + I(X1^2) + I(X2^2) + I(X1 * X2). Compare the
overall \(F\) test with the \(t\) tests of the five coefficients. Compute
the correlation between X1 and X1^2, and
explain what you see.x1 = X1 - mean(X1) and
x2 = X2 - mean(X2) and fit the same model on them. Which of
the following change, and which do not: the fitted values, \(MSE\), \(R^2\), the coefficients of the squares and
of the product, the coefficients of the linear terms, their standard
errors? Explain why the linear coefficients change, from what they
measure.anova on a
reduced and a full model, and check the \(F^*\) by hand from the two values of \(SSE\). Does it matter whether you use the
raw or the centred predictors?The file ship_speed_fuel.csv gives, for 20 legs sailed
by a container ship, its speed (speed, in knots) and its
fuel consumption (fuel, in tons per day), from Wang and
Meng (2012). Physics suggests that the fuel burnt per day grows roughly
like the cube of the speed.
ship <- read.csv("ship_speed_fuel.csv")
Questions.
fuel against speed, fit a straight
line, and plot its residuals against speed with a lowess
curve. Is the straight line adequate?x = speed - mean(speed). Are the second- and third-order
terms needed, taken together? Compare the correlation between
speed and speed^2 with the correlation between
x and x^2.log(fuel) ~ log(speed).
Interpret the slope: what happens to the fuel consumption when the speed
increases by 1%? By 10%? Give a 95% confidence interval for the slope of
this model, and test at the 5% level whether it equals 3.exp). The speeds in the data run from
16.1 to 19.8 knots. Which predictions do you trust?fuel and the power law \(R^2 =
0.977\) for log(fuel). Can \(R^2\), adjusted \(R^2\) or AIC tell you which of the two fits
better? Propose a fair comparison, and carry it out.