I'm trying to hand calculate the distance and weight measures that are output from the R kknn package. I'm able to correctly calculate the euclidean distances and inverse weights, when the data are not scaled, as follows:
euclidean distances
sqrt((6-8)^2 + (4-5)^2) = 2.236068
sqrt((6-3)^2 + (4-7)^2) = 4.242641
sqrt((6-7)^2 + (4-3)^2) = 1.414214
inverse weights
1 / (2.236068 / 4.242641) = 1.897368
1 / (1.414214 / 4.242641) = 3.000000.
I'm not seeing how the rectangular weights are calculated because I get:
1/2 * 1 = 0.50
1/2 * 1 = 0.50
and the kknn package gives 1 and 1.
Lastly, I'm having no luck at all calculating distances and weights when the data are scaled. Any help is appreciated because I'm trying to understand how the kknn package works.
library(kknn)
training <- data.frame(class = c(1, 0, 1), height = c(8, 3, 7), weight = c(5, 7, 3))
training
holdouts <- data.frame(class = 1, height = 6, weight = 4)
holdouts
rectangular_no_scale <- kknn(class ~., training, holdouts, distance = 2, kernel = "rectangular", k = 2, scale = FALSE)
rectangular_no_scale[["D"]]
rectangular_no_scale[["W"]]
inversion_no_scale <- kknn(class ~., training, holdouts, distance = 2, kernel = "inv", k = 2, scale = FALSE)
inversion_no_scale[["D"]]
inversion_no_scale[["W"]]
rectangular_with_scale <- kknn(class ~., training, holdouts, distance = 2, kernel = "rectangular", k = 2, scale = TRUE)
rectangular_with_scale[["D"]]
rectangular_with_scale[["W"]]
inversion_with_scale <- kknn(class ~., training, holdouts, distance = 2, kernel = "inv", k = 2, scale = TRUE)
inversion_with_scale[["D"]]
inversion_with_scale[["W"]]