How to use this sheet

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.


Exercise 1. Two lines, or one?

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.

  1. In Model 1, what do the two coefficients estimate? What is the mean fuel economy of the manual cars?
  2. In Model 2, write the fitted line of each type of transmission, and interpret the coefficient of am. The difference of 7.24 mpg of Model 1 has disappeared: explain why, using the weights given above.
  3. In Model 3, write the fitted line of each type of transmission. Test at the 5% level whether the two slopes are equal. Which of Models 2 and 3 do you keep?
  4. A reader of the output of Model 3 concludes: “manual cars do 14.9 mpg better than automatic cars, and the difference is highly significant (\(p = 0.0016\))”. What is wrong with this reading? At which weight do the two fitted lines cross?
  5. Estimate the difference in mean fuel economy between a manual and an automatic car of the same weight, at 2.5 and at 3.5 thousand pounds, with a 95% confidence interval each time. Why were these two weights chosen rather than, say, 2 and 5?
  6. Test at the 5% level whether the transmission matters at all once the weight is in the model, that is, whether one line is enough for all the cars. In Model 2 the transmission seemed useless (\(p = 0.99\)): how do you reconcile the two results?

Exercise 2. Do we need the squares?

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.

  1. Fit the second-order model on the raw predictors, 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.
  2. Create the centred predictors 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.
  3. Test \(H_0: \beta_{11} = \beta_{22} = \beta_{12} = 0\) at the 5% level, with 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?
  4. Fit the first-order model on the raw predictors. Interpret the two slopes in the units of the problem, and give a 95% confidence interval for the effect of 10 more degrees.

Exercise 3. A power law

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.

  1. Plot fuel against speed, fit a straight line, and plot its residuals against speed with a lowess curve. Is the straight line adequate?
  2. Fit a cubic polynomial in the centred speed, 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.
  3. Now fit the power law: 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.
  4. Predict the fuel consumption at 18 knots and at 22 knots with the straight line, the cubic and the power law (for the power law, come back to tons per day with exp). The speeds in the data run from 16.1 to 19.8 knots. Which predictions do you trust?
  5. The cubic has \(R^2 = 0.997\) for 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.