# knnImputation for missing data

I have a dataframe containing missing values. I want to impute them with knnImputation as shown below.

Now my question is: are data imputed row wise or column wise and is there a way to give this option in knnImputation?

A <-c(1,2,3,4,5, NA, NA)
B <-c(2,3,NA, 2,4, 7, 6)
C <-c(2,3,4, NA, 5, 1,7)
D<-21:27
data<-data.frame(A,B, C, D)
new_data<-knnImputation(data, k = 3, scale = T, meth = "weighAvg", distData = NULL)

• According to the code it seems like by row scaling by column but I am not sure. But you can change the orientation of your matrix by transposing it with t . Another option is to test it with a symmetric (or quasi-symmetric) matrix
– llrs
Jan 20, 2018 at 10:36

The data are imputed using values from the same column

Here's a simplified example to show what's going on:

A <-c(1,2,3,4,5)
B <-c(2,3,NA, 2,4)
C <- c(2,3,4, 6, 1)
data<-data.frame(A,B,C)
data

#>  A  B C
#>1 1  2 2
#>2 2  3 3
#>3 3 NA 4
#>4 4  2 6
#>5 5  4 1

# I'm using scale = F to make things simpler
knnImputation(data, k = 3, scale = F, meth = "weighAvg", distData = NULL)

#>  A        B C
#>1 1 2.000000 2
#>2 2 3.000000 3
#>3 3 2.594272 4
#>4 4 2.000000 6
#>5 5 4.000000 1

# calculate euclidean distances, dropping column of interest
dist(data[,-2])

#>1        2        3        4
#>2 1.414214
#>3 2.828427 1.414214
#>4 5.000000 3.605551 2.236068
#>5 4.123106 3.605551 3.605551 5.099020

# use k rows with smallest distance to row of interest to calculate weighted average
# using exp(-dist) as weights
weight_sum <- weight_sum = exp(-2.828427) + exp(-1.414214) + exp(-2.236068)
2*exp(-2.828427)/weight_sum + 3*exp(-1.414214)/weight_sum + 2*exp(-2.236068)/weight_sum

#>2.594271


If you want to impute using values from the same row, all you have to do is transpose the matrix:

t(knnImputation(t(data), k = 2, scale = F, meth = "weighAvg", distData = NULL))

#>     A        B C
#>[1,] 1 2.000000 2
#>[2,] 2 3.000000 3
#>[3,] 3 3.086729 4
#>[4,] 4 2.000000 6
#>[5,] 5 4.000000 1


(I had to lower k because there are only 2 extra columns)