# R-squared for user-defined prediction algorithms

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?

## 1 Answer

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)

summary(slr)

• 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. – Nick Cox Sep 3 '13 at 15:22
• @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! – Matt Reichenbach Sep 3 '13 at 15:32