Get startedGet started for free

Predict LDA

Like in the regression, the function predict() can be used to predict values based on a model. The function arguments are almost the same. You can see the help page of prediction function for LDA with ?predict.lda.

We split our data earlier so that we have the test set and the correct class labels. See how the LDA model performs when predicting on new (test) data.

This exercise is part of the course

Helsinki Open Data Science

View Course

Exercise instructions

  • Predict the crime classes with the test data. Like in regression, the predict() function takes the model object as a first argument.
  • Create a table of the correct classes and the predicted ones. You can get the predicted classes with lda.pred$class.
  • Look at the table. Did the classifier predict the crime rates correctly?

Hands-on interactive exercise

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

# lda.fit, correct_classes and test are available

# predict classes with test data
lda.pred <- predict(lda.fit, newdata = "change me!")

# cross tabulate the results
table(correct = "change me!", predicted = "change me!")
Edit and Run Code