[FIXED] Plotting classification prediction (K-nearest neighbor)

Issue

Im trying to plot my predictions using the k-nearest neighbor method but am unable to do do, I get an error message as seen below. Im sure it's something to do with how ive set up my plot but unsure as to how i need to change it. Dataset is here; https://drive.google.com/file/d/1GYnlsXgT2GS9ubeXq8Pm7iNUWDRGogU_/view?usp=sharing

 set.seed(20220719)

#splitting training and testing data
ii = createDataPartition(classification[,3], p = .75, list = F) 
#split the data using the indices returned by 
createDataPartition
xTrain = classification[ii, 1:2] #predictors for training
yTrain = classification[ii, 3]   #class label for training
xTest = classification[-ii, 1:2] #predictors for testing
yTest = classification[-ii, 3]   #class label for testing
 #set training options
#repeat 10 fold cross-validation, 5 times
opts = trainControl(method = 'repeatedcv', number = 10, repeats = 5)

 #find optimal k (model)
kmeans_mod = train(x = xTrain, y = as.factor(yTrain),    
         method ='knn',                               
         trControl = opts,                            
         tuneGrid = data.frame(k = seq(3, 10)))       

#test model on testing data
yTestPred = predict(kmeans_mod, newdata = xTest)
confusionMatrix(as.factor(yTestPred), as.factor(yTest))

#plot
plot(kmeans_mod, xTrain)

Gives the error message

 Error in if (!(plotType %in% c("level", "scatter", "line"))) stop("plotType must be either level, scatter or line") : 
the condition has length > 1

Im looking for an output like this;

enter image description here


Solution

To get a plot similar to the one in the question, you can create a grid of prediction points to produce the background classification map, then plot the test data on top using ggplot.

# Create prediction data frame for test data
preds <- data.frame(X1 = xTest[,1], X2 = xTest[,2], Group = yTestPred)

# Create classification grid
gr <- expand.grid(X1 = seq(min(classification[,1]), max(classification[,1]),
                       length.out = 100),
                  X2 = seq(min(classification[,2]), max(classification[,2]),
                       length.out = 100))
gr$Group <- predict(kmeans_mod, newdata = gr)

# Plot the result
library(ggplot2)

ggplot(gr, aes(X1, X2, col = Group)) +
  geom_point(size = 0.6) +
  geom_point(data = preds, shape = 21, aes(fill = Group), 
             col = "black", size = 3) +
  theme_minimal(base_size = 16)

enter image description here

Though you may prefer a raster:

library(ggplot2)

ggplot(gr, aes(X1, X2, fill = Group)) +
  geom_raster(alpha = 0.3) +
  geom_point(data = preds, shape = 21, col = "black", size = 3) +
  theme_minimal(base_size = 16)

enter image description here

And you may wish to color the test data points with their actual level rather than their predicted level to get a visual impression of the model accuracy:

library(ggplot2)

ggplot(gr, aes(X1, X2, fill = Group)) +
  geom_raster(alpha = 0.3) +
  geom_point(data = within(preds, Group <- factor(yTest)),
             col = "black", size = 3, shape = 21) +
  theme_minimal(base_size = 16)

enter image description here



Answered By - Allan Cameron
Answer Checked By - Marilyn (Python.Easybugfix Volunteer)

Related Posts

Post a Comment