We will now examine the Carseats data, which is part of the ISLR2 library. (Don’t forget to load and if necessary install the package first in RStudio.) We will attempt to predict Sales (child car seat sales) in 400 locations based on a number of predictors.

> head(Carseats)
> names(Carseats)
 [1] "Sales"       "CompPrice"   "Income"      "Advertising" "Population" 
 [6] "Price"       "ShelveLoc"   "Age"         "Education"   "Urban"      
[11] "US"         

The Carseats data includes qualitative predictors such as ShelveLoc, an indicator of the quality of the shelving location—that is, the space within a store in which the car seat is displayed—at each location. The predictor ShelveLoc takes on three possible values, Bad, Medium, and Good. Given a qualitative variable such as ShelveLoc, R generates dummy variables automatically.

Try to use the levels() function to check the three possible values of ShelveLoc yourself:


Assume that: