I am using a k-modes model (mymodel) which is created by a data frame mydf1. I am looking to assign the nearest cluster of mymodel for each row of a new data frame mydf2.
Similar to this question - just with k-modes instead of k-means. The predict function of the flexclust package only works with numeric data, not categorial.
A short example:
require(klaR)
set.seed(100)
mydf1 <- data.frame(var1 = as.character(sample(1:20, 50, replace = T)),
var2 = as.character(sample(1:20, 50, replace = T)),
var3 = as.character(sample(1:20, 50, replace = T)))
mydf2 <- data.frame(var1 = as.character(sample(1:20, 50, replace = T)),
var2 = as.character(sample(1:20, 50, replace = T)),
var3 = as.character(sample(1:20, 50, replace = T)))
mymodel <- klaR::kmodes(mydf1, modes = 5)
# Get mode centers
mycenters <- mymodel$modes
# Now I would want to predict which of the 5 clusters each row
# of mydf2 would be closest to, e.g.:
# cluster2 <- predict(mycenters, mydf2)
Is there already a function which can predict with a k-modes model or what would be the simplest way to do that? Thanks!