4. Classification Models: Logistic Regression

4.1 Create a new data set named Auto2 that is identical to the Auto{ISLR} data set (i.e., `Auto2=data.frame(Auto)`

). Then attach the Auto2 data set – i.e., `attach(Auto2)`

4.2 Compute the median value of the miles per gallon values and store the results in an object named med.mpg. Display this median.

4.3 Create a new dummy variable in the data set named mpg01 to classify “high mileage” vehicles. This dummy variable will be = 1 if the mpg value is greater than the median and 0 otherwise.

4.4 Set the seed to 1 (or any value you choose) to get replicable results. Then draw a random sample of index numbers into a vector called train containing 80% of the row numbers in Auto2. This vector will contain a random sample of 80% of the nrow(Auto2) row numbers, which you will use to index the training sub-sample. *Note: your resulst will probably deviate slightly from mine because of the random sampling.*

4.5 Fit a Logistic model on the training sub-sample, to predict the likelihood that a vehicle has high gas mileage (per the mpg01 variable) using cylinders, displacement, horsepower, weight, year and origin as predictors. Store the results in an object named fit.logit. Display the summary results.

4.6 Briefly interpret the results, focusing on significant coefficients, deviance and AIC.

4.7 As you know, the coefficients of this logit model represent the variable effects on the log-odds of having a high mileage car. But this is hard to interpret. To make this easier to interpret, create 3 vectors: log.odds containing the coefficients from fit.logit; odds containing the odds of these coefficients; and prob containing the probabilities that the coefficients represent. Then use the `cbind()`

function to diaplay all 3 vectors together.

4.8 Now let’s do some cross-validation. Use the `predict()`

function with the attribute `type="response"`

(which gives probabilities) to make predictions on the test data (tip: `[-train,]`

) and save the results in an object named pred.probs. Then create a vector named pred.hi.mpg containing a 1 if the probability of the vehicle being high mileage is greater than 50% and 0 otherwise (tip: use this function `ifelse(pred.probs>0.5, 1, 0)`

)

4.9 Display a confusion matrix with these test predictions using the table() function (tip: use `table(pred.hi.mpg, Auto2$mpg01[-train])`

)

4.10 Review the confusion matrix and then calculate (by hand, not with R code) theError Rate,Sensitivity andSpecificity (Note: I will provide the R code to do this with the solution).

</pstyle=”margin-bottom:></pstyle=”margin-bottom:></pstyle=”margin-bottom:>