Is there a simple way to computed the $R^2$ (adjusted and not adjusted), from the LinearModelFit?

It has so many options, but none for the $R^2$, that I suspect there's a simple way to compute it it the info given by some of the options.

Any help would be appreciated.

  • 2
    $\begingroup$ It's "AdjustedRSquared" and "RSquared". Check the Details and Options section of the documentation for details. $\endgroup$ – Karsten 7. Feb 9 '16 at 20:36
  • $\begingroup$ @Karsten7. you're right. I checked the 'Details and Options' section, but I should have scrolled further down... $\endgroup$ – An old man in the sea. Feb 9 '16 at 21:18
lm = LinearModelFit[xydata, x, x];
r2 = lm["RSquared"]
ar2 = lm["AdjustedRSquared"]

Alternatively calculating in steps.

{x, y} = Transpose[xydata];

MapIndexed[(X@#2[[1]] = #1) &, x];
MapIndexed[(Y@#2[[1]] = #1) &, y];

n = Length[xydata];
ΣX = Sum[X[i], {i, n}];
ΣY = Sum[Y[i], {i, n}];
ΣXY = Sum[X[i] Y[i], {i, n}];
ΣX2 = Sum[X[i]^2, {i, n}];

Clear[a, b];
{{a, b}} = {a, b} /. Solve[{
     (*Normal equations for straight line*)
     ΣY == n a + b ΣX,
     ΣXY == a ΣX + b ΣX2}, {a, b}];

(*Least-squares regression of Y on X*)
Array[(Yhat[#] = a + b X[#]) &, n];

Array[(e[#] = Y[#] - Yhat[#]) &, n];
(*Residual or unexplained sum of squares*)
RSS = Sum[e[i]^2, {i, n}];

Ymean = ΣY/n;
Array[(Yd[#] = Y[#] - Ymean) &, n];
(*Total sum of squares in the dependent variable,
measured about its mean*)
TSS = Sum[Yd[i]^2, {i, n}];

(*Coefficient of determination, R-squared*)
R2 = 1 - RSS/TSS

(*Number of regression parameters, k*)
k = 2;
(*Adjusted R-squared*)
AdjR2 = 1 - (RSS/(n - k))/(TSS/(n - 1))
  • $\begingroup$ I have a bunch of observed data and I have my model. I don't want to know what the best fit is, I want to know how my model compares to the observed data. Same question: how do you calculate the $R^2$? $\endgroup$ – Quarkly Dec 8 '16 at 1:00
  • $\begingroup$ @MikeDoonsebury Sounds like you want the chi-squared goodness of fit test. See ref 1 and ref 2. $\endgroup$ – Chris Degnen Dec 8 '16 at 9:39
  • $\begingroup$ Thank you but I want to know how to do this in Mathematica. The $\chi^2$ and $R^2$ functions work on fitted models. I need either test done for a predictive model. That is, I want to know how well the observed data matches the model that I provide. $\endgroup$ – Quarkly Dec 8 '16 at 11:37
  • $\begingroup$ Then perhaps Single Prediction Intervals. E.g. mathematica.stackexchange.com/a/39956/363 $\endgroup$ – Chris Degnen Dec 8 '16 at 13:58

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