R code

Please find some R code used to fit and plot some models. You can find most data sets on Moodle. Please download data and try some of the R code as you read through these notes.

Search strategy

In the previous lecture, we considered two search strategies.

  1. All subset where we search all possible models.
  2. Stepwise selection where we begin with an initial model and systematically add or remove variables one at a time.

All subset regression was considered previously and so today we will conserder option (2)

Model selection for Motor Trend Car Road Tests data

The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models). A full description of the data can be found with the R command

?mtcars

These data contain 32 observations on 11 (numeric) variables.

  1. mpg Miles/(US) gallon
  2. cyl Number of cylinders
  3. disp Displacement (cu.in.)
  4. hp Gross horsepower
  5. drat Rear axle ratio
  6. wt Weight (1000 lbs)
  7. qsec 1/4 mile time
  8. vs Engine (0 = V-shaped, 1 = straight)
  9. am Transmission (0 = automatic, 1 = manual)
  10. gear Number of forward gears
  11. carb Number of carburetors

Recall for the last lecture where we wanted to model mpg using the remaining variables as explanatory variables.