I've been working on a machine learning project for a while and I've come up with an algorithm that does what I want it to do (predict some values). I wondered if it is possible to calculate R-squared for my results.

My results are like this (test):

Predictions  Observations:
4.25         4.30
4.15         4.25
3.70         3.75
3.25         3.15

How can I calculate R-squared for this?


To calculate an R-squared with the above results, you can perform simple linear regression with Predictions as your predictor, x, variable and Observations as your outcome, y, variable using any standard statistical program.

The below R code will allow you to do so:

slr <- lm(Observations ~ Predictions, data=yourdata)


  • 1
    $\begingroup$ Also, R-squared here is just the square of the Pearson correlation, to be calculated using the appropriate function or command in your favourite software. $\endgroup$ – Nick Cox Sep 3 '13 at 15:22
  • $\begingroup$ @NickCox, you are very correct as well (thanks for adding that to my answer), Sinead, whichever approach you choose will net you the same R-squared, cheers! $\endgroup$ – Matt Reichenbach Sep 3 '13 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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