Search code examples
rr-caret

A warning in R when using predict function in caret


I want to imputate missing values using preProcess. The code is simple. However, a warning occurred and it seems I cannot use knnImpute. I failed to find a correct explanation online.

library(mlbench)
data(Soybean)

library(caret)
imputationObj <- preProcess(Soybean, method = c('knnImpute'), na.remove = FALSE)
imputationObj
imputedSoybean <- predict(imputationObj, Soybean)
summary(imputedSoybean)

>     Warning in pre_process_options(method, column_types) :
>       The following pre-processing methods were eliminated: 'knnImpute', 'center', 'scale'

Solution

  • The problem is that the data frame Soybean contains factors. From ?preProcess, the first argument should be

    x a matrix or data frame. Non-numeric predictors are allowed but will be ignored.

    but

    R> class(Soybean[,2])
    [1] "factor"
    

    To put it another way, how would you centre a factor containing the value red,blueandgreen`.