Get startedGet started for free

Model specification

The stepAIC() function gives back a reduced model, as you just saw in the previous video. Now you want to apply this method to the exercise dataset defaultData.

The prepared dataset is available in your environment. Additionally, the MASS package is loaded and the previously built logit model logitModelFull is defined for you. Also note that we've reduced the size of the dataset as performing stepwise model selection can take a long time with larger datasets and more complex models.

This exercise is part of the course

Machine Learning for Marketing Analytics in R

View Course

Exercise instructions

  • Make use of the stepAIC() function. Set trace = 0, as you do not want to get an output for the whole model selection process. Save the result to the object logitModelNew.
  • Then, use the summary() function to take a look at logitModelNew. You can ignore the warning message in this case. Go ahead and see what changed. Understand the results.
  • The formula is saved in an object so that you don't have to type the whole equation again when you want to use it later.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

library(MASS)
# The old (full) model
logitModelFull <- glm(PaymentDefault ~ limitBal + sex + education + marriage +
                   age + pay1 + pay2 + pay3 + pay4 + pay5 + pay6 + billAmt1 + 
                   billAmt2 + billAmt3 + billAmt4 + billAmt5 + billAmt6 + payAmt1 + 
                   payAmt2 + payAmt3 + payAmt4 + payAmt5 + payAmt6, 
                 family = binomial, defaultData)

#Build the new model
logitModelNew <- stepAIC(___,___) 

#Look at the model
summary(logitModelNew) 

# Save the formula of the new model (it will be needed for the out-of-sample part) 
formulaLogit <- as.formula(summary(logitModelNew)$call)
formulaLogit
Edit and Run Code