Get startedGet started for free

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

View Course

Exercise instructions

  • Predict the responses for test.data by using the function predict() on train.model. Set the type argument to "response" and name the result probability.
  • Classify the model predictions into 1 if the probability exceeds 0.5 and 0 otherwise. Assign the result to an object predicted.
  • Obtain the observed purchases for HOPPINESS from test.data. Assign them to an object observed.
  • Cross-tabulate the observed and predicted data vectors by using the functions table() and prop.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))
Edit and Run Code