1

In sklearn there is a function sklearn.metrics.r2_score(y_true, y_pred) where I can give it two arrays and it calculates r^2. Is there something similar in R? I've found some functions but they are only for GLMs. I have a test set and test predictions from KNN regression that I want to calculate r^2 for. Am I going to have to hand-code this?

wordsforthewise
  • 11,271
  • 5
  • 74
  • 108

1 Answers1

0

It is not something obvious, but the caret package has a function postResample() that will calculate "A vector of performance estimates" according to the documentation (really helpful documentation). The "performance estimates" are

  • RMSE
  • Rsquared
  • mean absolute error (MAE)

and have to be accessed from the vector like this

library(caret)
vect1 <- c(1, 2, 3)
vect2 <- c(3, 2, 2)
res <- caret::postResample(vect1, vect2)
rsq <- res[2]

However, this is using the correlation squared approximation for r-squared. Why they didn't just use the conventional 1-SSE/SST is beyond me.

The way to implement the normal coefficient of determination equation is:

preds <- c(1, 2, 3)
actual <- c(2, 2, 4)
rss <- sum((preds - actual) ^ 2)
tss <- sum((actual - mean(actual)) ^ 2)
rsq <- 1 - rss/tss
wordsforthewise
  • 11,271
  • 5
  • 74
  • 108