Out-of-sample testing
Your goal is to make purchase predictions for new stores based on your estimated model. You check if this goal is viable by using the function predict()
. The predict()
function retrieves the parameter estimates of train.model
to make predictions about the response variable in test.data
. To obtain predicted values on the scale of the response variable (the predicted purchase probabilities) you have to set the additional type
argument to "response"
.
Finally, the hold-out predictions are classified into purchases and no-purchases by using the function ifelse()
and compared to the observed purchases by using the function table()
. Finally, you use the function prop.table()
to convert the numbers in the table into relative numbers.
This exercise is part of the course
Building Response Models in R
Exercise instructions
- Predict the responses for
test.data
by using the functionpredict()
ontrain.model
. Set thetype
argument to"response"
and name the resultprobability
. - Classify the model predictions into
1
if theprobability
exceeds0.5
and0
otherwise. Assign the result to an objectpredicted
. - Obtain the observed purchases for
HOPPINESS
fromtest.data
. Assign them to an objectobserved
. - Cross-tabulate the
observed
andpredicted
data vectors by using the functionstable()
andprop.table()
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Predict the purchase probabilities for test.data
probability <- ___(___, ___, type = "response")
# Classify the predictions
predicted <- ___(probability >= ___, ___, ___)
# Obtain the observed purchases from test.data
observed <- test.data$HOPPINESS
# Cross-tabulate observed vs. predicted purchases
___(___(predicted, observed))