sambofra / bnstruct

R package for Bayesian Network Structure Learning
GNU General Public License v3.0
17 stars 11 forks source link

Error in knn.impute #13

Closed msmith01 closed 4 years ago

msmith01 commented 4 years ago
library(bnstruct)
data(iris)
data <- iris[, 1:4]
data <- prodNA(iris, noNA = 0.2) # generate random missing data
knn.impute(data, k = 10, cat.var = 1:ncol(data),
           to.impute = 1:nrow(data), using = 1:nrow(data))

Returns:

Error in storage.mode(use.data) <- "double" : 
  (list) object cannot be coerced to type 'double'

Also;

mtcars <- subset(mtcars, select = c(mpg, cyl, wt))

mtcars[1:10,]$mpg <- NA
knn.impute(mtcars, k = 4)

Gives the same error.

Source:

albertofranzin commented 4 years ago

knn.impute works on numerical matrices, not on dataframes. The documentation is wrong, sorry, I will fix it as soon as possible. The workaround is

data <- iris[, 1:4]
data <- prodNA(data, noNA = 0.2)
knn.impute(as.matrix(data), k = 10, cat.var = 1:ncol(data),
           to.impute = 1:nrow(data), using = 1:nrow(data))

Thanks for pointing this out.