The introductory book says R^2 is between 0 and 1, but I have two randomly generated sequences and the R^2 is negative. So I read further and now understand the negative value is because R^2 essentially measures how well your model beats a horizontal line. OK, that makes sense. So then I made a random sequence that is always either 0 or 1. Obviously the best fitting line is a horizontal line at y=.5 and that prediction will have an error of .5 on every single point. The R^2 of that prediction is 0, so that makes sense. But then I made two random sequences that are always either 0 or 1, and the R^2 is still negative. But since each value is either 0 or 1, that means half the points will be identical (error=0) and half the points will be different (error=1), so there should be an average error of .5 exactly like the horizontal line. So why doesn't this random sequence have exactly the same explanatory power as the horizontal line? Why doesn't it also have the same R^2=0 as the horizontal line?

x = np.arange(size).reshape(-1, 1)
random_1 = np.random.randint(0,2,size=size)
random_2 = np.random.randint(0,2,size=size)

r2_vs_random = sklearn.metrics.r2_score(random_1, random_2)
print(f"R^2 vs random={r2_vs_random}")
plt.scatter(x, random_2);
  • $\begingroup$ Have you read the scikit learn documentation? scikit-learn.org/stable/modules/generated/… $\endgroup$ – amdopt Nov 1 '19 at 21:25
  • 2
    $\begingroup$ There is an excellent answer to this question at stats.stackexchange.com/questions/12900/… $\endgroup$ – Dave Harris Nov 2 '19 at 0:58
  • $\begingroup$ Hi: you should figure out what r2_score is literally doing in terms of a formula. My guess is that it's not doing what you think it's doing. The correlation that 2 random variables have is pretty different than the R squared that comes out of a regression of one versus the other. The latter needs an intercept to be included or it makes no sense. $\endgroup$ – mark leeds Nov 2 '19 at 4:31
  • $\begingroup$ I'm voting to close this question as off-topic because it’s a stats question to which Dave Harris link provides an excellent answer. $\endgroup$ – Bob Jansen Nov 2 '19 at 10:56
  • $\begingroup$ @Bob Jansen: To me. it's not a relevant answer because the "artificially constrained case" is not applicable to what the person above described. They called a function in python which does "who knows what ?".. But you're right in that it's more of a "what does sklearn.metric.r2_score" do under the hood question which is not terribly relevant for this list. $\endgroup$ – mark leeds Nov 2 '19 at 13:24