I did the knn imputation following this post: KNN imputation R packages

I met the error: Not sufficient complete cases for computing neighbors. Even when k = 1, this error occurs.

data.imputed <- knnImputation(data, k=3)
Error in knnImputation(data) : 
Not sufficient complete cases for computing neighbors.

However the data should have sufficient complete cases. What's the problem?

[1]  779 1558
[1] 600

I feel like this question would better belong on StackOverflow but I'll give it a go anyway:

paraphrasing the code for knnImputation (https://gist.github.com/tengpeng/fb6809717361319d8bde), you get that error message under the following conditions:

xcomplete <- data[setdiff(1:nrow(data),which(!complete.cases(data))),]
if (nrow(xcomplete) < k)
    stop("Not sufficient complete cases for computing neighbors.")

I suggest you check the value of nrow(xcomplete) is equal to 600.

EDIT: As described in PR#16648, complete.cases can lead to spurious errors when some columns have classes with length or is.na methods, for example "POSIXlt".


Not the answer you're looking for? Browse other questions tagged or ask your own question.