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)
t
. Another option is to test it with a symmetric (or quasi-symmetric) matrix $\endgroup$