I have a KNN model that I used to predict the close price on houses.

KNN <- kknn(formula = scale(close_price) ~.,
     k = 4,
     distance = 2,
     kernel = "optimal"

mdae(test.data$close_price, KNN$fitted.values)

When I center and scale my response variable (as I believe I'm supposed to since KNN relies on Euclidean distance), I get a median relative absolute error of 185272.

When I don't center and scale, I get a median relative absolute error of 32590.

I assume these are in the same units since the formula applies to the training data and the predictions should use the regular close prices without scaling them. But, the difference seems to big to be reasonable.

Are they in the same units and should I be centering and scaling my response variable for KNN?


1 Answer 1


One of the advantages of standardization is making the values unitless, so that we can compare/sum different quantities with each other. Your scaled y is unit-free, because scaling divides your target (- mean) by its deviation, which is still of unit u. When we divide a quantity of unit u, with another quantity of unit u, the resulting quantity become unit-free. Subtraction preserves the units, which is why $y-\mu_y$ is of unit u. So, if you don't scale your data, the squared error will have unit $u^2$; and if you scale, it'll have no unit. They're not comparable.

In KNN (with euclidean) you actually need to scale your features. Scaling the target is not necessary, and is up to how you interpret. That's because the distance is calculated over the features, not the targets. Luckily for you, it's being handled via scale parameter, whose default value is TRUE.

  • $\begingroup$ Ah, so it should be formula = close_price ~ scale(latitude) + scale(longitude)? $\endgroup$
    – ivan
    Commented May 21, 2019 at 13:51
  • $\begingroup$ It appears that w/o explicitly calling scale as you do, the knnn method is doing that by default, because scale parameter is set to TRUE. $\endgroup$
    – gunes
    Commented May 21, 2019 at 13:55
  • $\begingroup$ Would you mind taking a look at this? stats.stackexchange.com/questions/409311/… $\endgroup$
    – ivan
    Commented May 21, 2019 at 14:19
  • $\begingroup$ I'm a bit tripped up by the scale. This generates median relative absolute error at 26572.95 KNN <- kknn(formula = close_price ~ ., train.data, test.data, k = 4, distance = 2, kernel = "optimal", scale = TRUE ). But this, which is supposed to tuned and has the best k (k = 203) generates median relative absolute error at 2469768 model <- caret::train(close_price~ ., data = train.data, method = "knn", trControl = trainControl("cv", number = 10), preProcess = c("center", "scale"), tuneLength = 200) predictions <- model %>% predict(test.data) mdae(test.data$close_price, predictions) $\endgroup$
    – ivan
    Commented May 21, 2019 at 15:43

Your Answer

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

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