-
Notifications
You must be signed in to change notification settings - Fork 7
Open
Description
Issue description
Predict does not work on data with a single row.
The cause is in the way distances for categorical variables are computed in predict.kproto() .
Whenever the input data x has only 1 row, the result of sapply simplifies to a logical vector instead of a matrix and the subsequent call to rowSums fails.
d2 <- sapply(which(catvars), function(j) return(x[,j] != rep(protos[i,j], nrows)) )
d2[is.na(d2)] <- FALSE
if(length(lambda) == 1) d2 <- lambda * rowSums(d2) # <---- rowSums failsReproducible example
library(clustMixType)
set.seed(123)
model <- kproto(x = iris, k = 4)
predict(model, iris[1, ])
# which yields
# Error in rowSums(d2) : 'x' must be an array of at least two dimensionsSuggested fix
One quick fix would be to convert d2 to matrix whenever x is a data.frame with one row only:
d2 <- sapply(which(catvars), function(j) return(x[,j] != rep(protos[i,j], nrows)) )
if (NROW(x) == 1) d2 <- matrix(data = d2, nrow = 1, byrow = TRUE, dimnames = list(NULL, names(x)[catvars])) # <- FIX
d2[is.na(d2)] <- FALSE
if(length(lambda) == 1) d2 <- lambda * rowSums(d2) # <---- rowSums now succeedsMetadata
Metadata
Assignees
Labels
No labels