17
$\begingroup$

I am looking for a KNN imputation package. I have been looking at imputation package (http://cran.r-project.org/web/packages/imputation/imputation.pdf) but for some reason the KNN impute function (even when following the example from the description) only seems to impute zero values (as per below). I have been looking around but cannot find something yet, and hence was wondering if anyone has other suggestions for good KNN imputation packages?

W

In the code per below - the NA values are replaced by zero's - not by the Knn mean value

require(imputation)
x = matrix(rnorm(100),10,10)
x.missing = x > 1
x[x.missing] = NA
kNNImpute(x, 3)
x
$\endgroup$
6
  • 1
    $\begingroup$ According to the source code github.com/jeffwong/imputation/blob/master/R/kNN.R, any entries which cannot be imputed are just set to zero. The reason why you are seeing so many zeroes is because the algorithm which the package author has chosen cannot impute values for these entries. It might be better to relax the algorithm somehow to get sensible estimates for these values. $\endgroup$
    – Flounderer
    Jun 7, 2013 at 2:46
  • $\begingroup$ (see lines 91-93 of the code in the above link) $\endgroup$
    – Flounderer
    Jun 7, 2013 at 2:51
  • $\begingroup$ I had this same question awhile ago, posted on stackoverflow $\endgroup$ Aug 28, 2015 at 14:58
  • $\begingroup$ Just worth noting: there is no hope that any imputation model would have an unbiased estimation of your missing data that you've generated (based on how you've dropped it). Of course, I think you're more interested in getting kNNImpute to work at all (rather than to work well), so you probably don't care about the bias. $\endgroup$
    – Cliff AB
    Sep 19, 2015 at 19:09
  • $\begingroup$ Is there any specific reason you want to use KNN? Predictive mean matching is quite similar, and has a lot of optimal properties. $\endgroup$
    – RayVelcoro
    Sep 19, 2015 at 21:12

5 Answers 5

10
$\begingroup$

You could also try the following package: DMwR.

It failed on the case of 3 NN, giving 'Error in knnImputation(x, k = 3) : Not sufficient complete cases for computing neighbors.'

However, trying 2 gives.

> knnImputation(x,k=2)
             [,1]       [,2]       [,3]       [,4]       [,5]        [,6]
 [1,] -0.59091360 -1.2698175  0.5556009 -0.1327224 -0.8325065  0.71664000
 [2,] -1.27255074 -0.7853602  0.7261897  0.2969900  0.2969556 -0.44612831
 [3,]  0.55473981  0.4748735  0.5158498 -0.9493917 -1.5187722 -0.99377854
 [4,] -0.47797654  0.1647818  0.6167311 -0.5149731  0.5240514 -0.46027809
 [5,] -1.08767831 -0.3785608  0.6659499 -0.7223724 -0.9512409 -1.60547053
 [6,] -0.06153279  0.9486815 -0.5464601  0.1544475  0.2835521 -0.82250221
 [7,] -0.82536029 -0.2906253 -3.0284281 -0.8473210  0.7985286 -0.09751927
 [8,] -1.15366189  0.5341000 -1.0109258 -1.5900281  0.2742328  0.29039928
 [9,] -1.49504465 -0.5419533  0.5766574 -1.2412777 -1.4089572 -0.71069839
[10,] -0.35935440 -0.2622265  0.4048126 -2.0869817  0.2682486  0.16904559
             [,7]       [,8]        [,9]      [,10]
 [1,]  0.58027159 -1.0669137  0.48670802  0.5824858
 [2,] -0.48314440 -1.0532693 -0.34030385 -1.1041681
 [3,] -2.81996446  0.3191438 -0.48117020 -0.0352633
 [4,] -0.55080515 -1.0620243 -0.51383557  0.3161907
 [5,] -0.56808769 -0.3696951  0.35549191  0.3202675
 [6,] -0.25043479 -1.0389393  0.07810902  0.5251606
 [7,] -0.41667318  0.8809541 -0.04613332 -1.1586756
 [8,] -0.06898363 -1.0736161  0.62698065 -1.0373835
 [9,]  0.30051583 -0.2936140  0.31417921 -1.4155193
[10,] -0.68180034 -1.0789745  0.58290920 -1.0197956

You can test for sufficient observations using complete.cases(x), where that value must be at least k.

One way to overcome this problem is to relax your requirements (i.e. less incomplete rows), by 1) increasing the NA threshold, or alternatively, 2) increasing your number of observations.

Here is the first:

> x = matrix(rnorm(100),10,10)
> x.missing = x > 2
> x[x.missing] = NA
> complete.cases(x)
 [1]  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
> knnImputation(x,k=3)
             [,1]       [,2]       [,3]       [,4]        [,5]       [,6]       [,7]        [,8]        [,9]       [,10]
 [1,]  0.86882569 -0.2409922  0.3859031  0.5818927 -1.50310330  0.8752261 -0.5173105 -2.18244988 -0.28817656 -0.63941237
 [2,]  1.54114079  0.7227511  0.7856277  0.8512048 -1.32442954 -2.1668744  0.7017532 -0.40086348 -0.41251883  0.42924986
 [3,]  0.60062917 -0.5955623  0.6192783 -0.3836310  0.06871570  1.7804657  0.5965411 -1.62625036  1.27706937  0.72860273
 [4,] -0.07328279 -0.1738157  1.4965579 -1.1686115 -0.06954318 -1.0171604 -0.3283916  0.63493884  0.72039689 -0.20889111
 [5,]  0.78747874 -0.8607320  0.4828322  0.6558960 -0.22064430  0.2001473  0.7725701  0.06155196  0.09011719 -1.01902968
 [6,]  0.17988720 -0.8520000 -0.5911523  1.8100573 -0.56108621  0.0151522 -0.2484345 -0.80695513 -0.18532984 -1.75115335
 [7,]  1.03943492  0.4880532 -2.7588922 -0.1336166 -1.28424057  1.2871333  0.7595750 -0.55615677 -1.67765572 -0.05440992
 [8,]  1.12394474  1.4890366 -1.6034648 -1.4315445 -0.23052386 -0.3536677 -0.8694188 -0.53689507 -1.11510406 -1.39108817
 [9,] -0.30393916  0.6216156  0.1559639  1.2297105 -0.29439390  1.8224512 -0.4457441 -0.32814665  0.55487894 -0.22602598
[10,]  1.18424722 -0.1816049 -2.2975095 -0.7537477  0.86647524 -0.8710603  0.3351710 -0.79632184 -0.56254688 -0.77449398
> x
             [,1]       [,2]       [,3]       [,4]       [,5]       [,6]       [,7]        [,8]        [,9]       [,10]
 [1,]  0.86882569 -0.2409922  0.3859031  0.5818927 -1.5031033  0.8752261 -0.5173105 -2.18244988 -0.28817656 -0.63941237
 [2,]  1.54114079  0.7227511  0.7856277  0.8512048 -1.3244295 -2.1668744  0.7017532 -0.40086348 -0.41251883  0.42924986
 [3,]  0.60062917 -0.5955623  0.6192783 -0.3836310  0.0687157  1.7804657  0.5965411 -1.62625036  1.27706937  0.72860273
 [4,] -0.07328279 -0.1738157  1.4965579 -1.1686115         NA -1.0171604 -0.3283916  0.63493884  0.72039689 -0.20889111
 [5,]  0.78747874 -0.8607320  0.4828322         NA -0.2206443  0.2001473  0.7725701  0.06155196  0.09011719 -1.01902968
 [6,]  0.17988720 -0.8520000 -0.5911523  1.8100573 -0.5610862  0.0151522 -0.2484345 -0.80695513 -0.18532984 -1.75115335
 [7,]  1.03943492  0.4880532 -2.7588922 -0.1336166 -1.2842406  1.2871333  0.7595750 -0.55615677 -1.67765572 -0.05440992
 [8,]  1.12394474  1.4890366 -1.6034648 -1.4315445 -0.2305239 -0.3536677 -0.8694188 -0.53689507 -1.11510406 -1.39108817
 [9,] -0.30393916  0.6216156  0.1559639  1.2297105 -0.2943939  1.8224512 -0.4457441 -0.32814665  0.55487894 -0.22602598
[10,]  1.18424722 -0.1816049 -2.2975095 -0.7537477  0.8664752 -0.8710603  0.3351710 -0.79632184 -0.56254688 -0.77449398

here is an example of the 2nd...

x = matrix(rnorm(1000),100,10)
x.missing = x > 1
x[x.missing] = NA

complete.cases(x)

  [1]  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE
 [22] FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [43]  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [64] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
 [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE

At least k=3 complete rows are satisfied, thus it is able to impute for k=3.

> head(knnImputation(x,k=3))
            [,1]       [,2]       [,3]       [,4]       [,5]       [,6]       [,7]       [,8]        [,9]       [,10]
[1,]  0.01817557 -2.8141502  0.3929944  0.1495092 -1.7218396  0.4159133 -0.8438809  0.6599224 -0.02451113 -1.14541016
[2,]  0.51969964 -0.4976021 -0.1495392 -0.6448184 -0.6066386 -1.6210476 -0.3118440  0.2477855 -0.30986749  0.32424673
...
$\endgroup$
7
$\begingroup$

The imputation package isn't on CRAN any more.

One package other than DMwR that offers a kNN imputation function is VIM.

Also easy to use:

library("VIM")
kNN(x, k=3)
$\endgroup$
5
$\begingroup$
require(imputation)
x = matrix(rnorm(100),10,10)
x.missing = x > 1
x[x.missing] = NA
y <- kNNImpute(x, 3)

attributes(y)

$names
[1] "x"              "missing.matrix"

y$x

> x (original matrix)

             [,1]        [,2]       [,3]       [,4]        [,5]        [,6]        [,7]
 [1,]  0.38515909  0.52661156  0.6164138  0.3095225  0.55909716 -1.16543168 -0.70714440
 [2,] -0.39222402 -1.29703536  0.4429824 -1.3950116          NA -0.46841443 -0.57563472
 [3,] -2.04467869 -0.52022405         NA  0.7219057 -0.93573417 -1.51490638  0.62356689
 [4,] -1.08684345  0.63083074         NA  0.5603603  0.48583414          NA -0.69447183
 [5,]  0.30116921  0.25127476 -0.2132160         NA -1.63484823 -0.58266488  0.34432576
 [6,]  0.82152305 -0.12900915 -1.8498997  0.8012059          NA -0.14987133 -1.11232289
 [7,]  0.27912763 -0.68923032 -0.2355762 -0.2541675 -0.14181344 -0.08519797  0.13061823
 [8,]  0.06653984 -0.87521539 -0.0980306 -0.4350224  0.05021324 -1.66963624 -0.09204772
 [9,]  0.12687240 -0.62717646 -0.1258722         NA -0.86913445  0.68365036          NA
[10,]  0.56680502  0.03318012  0.1411861  0.6573134 -0.14747073          NA -1.37949278
             [,8]        [,9]       [,10]
 [1,] -2.67066748          NA -0.64370528
 [2,] -1.26864936 -1.95692064  0.28917897
 [3,] -0.27816124 -0.20332695 -1.29456054
 [4,] -1.10917662 -0.59598910 -0.32475962
 [5,] -0.15448822  0.71667444 -1.60827152
 [6,] -0.66691445  0.05396037  0.04074923
 [7,]  0.05644956  0.99416556 -0.77808427
 [8,] -0.32294266          NA -2.50933697
 [9,] -0.67226044          NA          NA
[10,] -0.84866945 -0.54318570          NA

> y$x (imputed matrix)

            [,1]        [,2]        [,3]        [,4]        [,5]        [,6]        [,7]
 [1,]  0.38515909  0.52661156  0.61641378  0.30952251  0.55909716 -1.16543168 -0.70714440
 [2,] -0.39222402 -1.29703536  0.44298237 -1.39501160 -0.22157531 -0.46841443 -0.57563472
 [3,] -2.04467869 -0.52022405  0.08298882  0.72190573 -0.93573417 -1.51490638  0.62356689
 [4,] -1.08684345  0.63083074 -0.66707695  0.56036034  0.48583414 -0.98956026 -0.69447183
 [5,]  0.30116921  0.25127476 -0.21321600 -0.02480909 -1.63484823 -0.58266488  0.34432576
 [6,]  0.82152305 -0.12900915 -1.84989965  0.80120592 -0.76323053 -0.14987133 -1.11232289
 [7,]  0.27912763 -0.68923032 -0.23557619 -0.25416751 -0.14181344 -0.08519797  0.13061823
 [8,]  0.06653984 -0.87521539 -0.09803060 -0.43502238  0.05021324 -1.66963624 -0.09204772
 [9,]  0.12687240 -0.62717646 -0.12587221  0.00000000 -0.86913445  0.68365036  0.00000000
[10,]  0.56680502  0.03318012  0.14118610  0.65731337 -0.14747073  0.00000000 -1.37949278
             [,8]        [,9]       [,10]
 [1,] -2.67066748  0.04286260 -0.64370528
 [2,] -1.26864936 -1.95692064  0.28917897
 [3,] -0.27816124 -0.20332695 -1.29456054
 [4,] -1.10917662 -0.59598910 -0.32475962
 [5,] -0.15448822  0.71667444 -1.60827152
 [6,] -0.66691445  0.05396037  0.04074923
 [7,]  0.05644956  0.99416556 -0.77808427
 [8,] -0.32294266  0.00000000 -2.50933697
 [9,] -0.67226044  0.00000000  0.00000000
[10,] -0.84866945 -0.54318570  0.00000000

It's imputed the values that it can. Those that can't be imputed are set to zero.

$\endgroup$
2
  • $\begingroup$ Seems imputation packages doesn't exist anymore (for R version 3.1.2) $\endgroup$ Feb 16, 2015 at 18:35
  • $\begingroup$ it's in github, google it. $\endgroup$
    – marbel
    Feb 15, 2017 at 21:33
1
$\begingroup$
install.packages("DMwR")*  # for use of knnImputation.

require(DMwR)
x  = matrix(rnorm(100), 10, 10)
x.missing= x >1
x[x.missing] = NA
complete.cases(x)
y <- knnImputation(x, 3)
$\endgroup$
0
$\begingroup$

The reason for R not being able to impute is because in many instances, more than one attribute in a row is missing and hence it cannot compute the nearest neighbor. What you can do alternatively is either impute interval variables with projected probabilities from a normal distribution ( or if its skewed use a Gamma distribution which have similar skew). and use a decision tree to predict missing values in case of a class variable.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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