user113156
user113156

Reputation: 7107

Anybody else get this error from knn.impute?

I am not sure why but I am getting the following error when applying knn.impute:

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

I don`t see anything wrong with the data.

Data:

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))

str(data)

'data.frame':   150 obs. of  4 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...

Upvotes: 3

Views: 1067

Answers (1)

Alberto Franzin
Alberto Franzin

Reputation: 231

As pointed out in the comments, bnstruct::knn.impute requires a numerical matrix, and it will not work with a dataframe. It will work with

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))

The documentation is wrong, sorry for that, I'm fixing it as soon as possible.

EDIT: the correct documentation has been updated in the github repo.

Upvotes: 1

Related Questions