Skip to main content

Section 9.6 Train and Test Split

Right now, we have one complete data set. What is amazing about our data is that it is complete. We have 10,000 data points each with the crucial information of if the person did or did not default. Our goal is still to create a model that takes the information we have about the people (if they’re a student, their balance, and their income) and use it to predict if a person will or will not default.
Ideally, we want this model to be able to predict it for people in the future? When we build it, how will we know how accurate it is?
In scenarios like this, we can actually split our data into two sets: one for training (creating our model) and one for testing (see how well our model actually works). This is important because we get to test our model using real data. It is kind of like practicing a basketball move before you use it in a real game.
Our goal: use a decent portion of our real data to test our model, and compare it to data not used in the model. To help with this, we will be using the sample.split() function from the caTools package ([D.1.4]).
# set.seed allows us to get the same split of the data each time we run this code
set.seed(42)

library(caTools) # Package that helps us split our data into Train and Test sets.

# We are going to be doing a 70/30 split. It is good practice to have most of your data in the training
sample <- sample.split(cc_data$default, SplitRatio = 0.7)

# This creates our test data, what we will use to train our logistic regression model
training_data <- subset(cc_data, sample == TRUE)

# This creates our test data, what we will measure our model against

test_data <- subset(cc_data, sample == FALSE)
We have successfully split our data into a training and a testing set! Again, most of our data is going to go into the training data set. We want our model to be as successful as possible while also providing enough of a chance to see if it works.
We have our training set, so let’s get predicting!