Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using knnImpute pre-processing option for dataset containing factor #404

Closed
saviola777 opened this issue Apr 8, 2016 · 1 comment

Comments

@saviola777
Copy link

Hello,

here is a reproducible example:

library(caret)
library(mlbench)
data("PimaIndiansDiabetes2")
pp <- preProcess(PimaIndiansDiabetes2, method = "knnImpute")
PimaIndiansDiabetes2pp <- predict(pp, newdata = PimaIndiansDiabetes2)

This does not produce errors when using "medianImpute". When I remove the only factor from the data, it works as expected:

PimaIndiansDiabetes2$diabetes <- NULL
pp <- preProcess(PimaIndiansDiabetes2, method = "knnImpute")
PimaIndiansDiabetes2pp <- predict(pp, newdata = PimaIndiansDiabetes2)

my session info is attached below. I already dug through the code for a bit and it seems that the old data used by the nnimp function (stored in pp$data) does not contain the factor columns and the function then tries to select all non-NA columns of the new data from the old data (old[, non_missing_cols, drop = FALSE]), which fails because the factors are not NA but not in the old data.

Is this a bug, or am I missing something when it comes to using the "knnImpute" option?

R version 3.2.4 (2016-03-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Gentoo/Linux

locale:
 [1] LC_CTYPE=en_US.UTF-8      LC_NUMERIC=C              LC_TIME=en_US.UTF8       
 [4] LC_COLLATE=en_US.UTF8     LC_MONETARY=en_US.UTF8    LC_MESSAGES=en_US.UTF8   
 [7] LC_PAPER=en_US.UTF8       LC_NAME=C                 LC_ADDRESS=C             
[10] LC_TELEPHONE=C            LC_MEASUREMENT=en_US.UTF8 LC_IDENTIFICATION=C      

attached base packages:
[1] stats     graphics  grDevices utils     datasets  compiler  methods   base     

other attached packages:
[1] caret_6.0-64    ggplot2_2.0.0   lattice_0.20-33 mlbench_2.1-1
topepo added a commit that referenced this issue Apr 13, 2016
@topepo
Copy link
Owner

topepo commented Apr 13, 2016

You aren't missing anything; it is a bug and you are correct about the issue. I checked in code to fix the issue.

Thanks,

Max

@topepo topepo closed this as completed Apr 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants