1
$\begingroup$

The given data is:

x = {0.04, 0.06, 0.04, 0.08, 0.08, 0.05, 0.05, 0.07, 0.07, 0.06};
y = {0.25, 0.4, 0.22, 0.54, 0.51, 0.34, 0.36, 0.46, 0.42, 0.4};

Is there a quicker method to calculate the coefficient of determination for this dataset? The approach I used involved step-by-step computation using the formula for the coefficient of determination.

enter image description here

x = {0.04, 0.06, 0.04, 0.08, 0.08, 0.05, 0.05, 0.07, 0.07, 0.06};
y = {0.25, 0.4, 0.22, 0.54, 0.51, 0.34, 0.36, 0.46, 0.42, 0.4};
data = Thread@{x, y};
meanY = Mean[y];
SST = Total[(y - meanY)^2];
lm = LinearModelFit[data, t, t]
residuals = lm["FitResiduals"]
SSE = Total[residuals^2]
rSquared = 1 - SSE/SST
$\endgroup$
6
  • 5
    $\begingroup$ lm["RSquared"] ? $\endgroup$
    – ydd
    Commented Jul 8 at 2:22
  • $\begingroup$ For a simple linear model (just one predictor) try Correlation[x, y]^2. For linear models with more than one predictor use @ydd 's suggestion. $\endgroup$
    – JimB
    Commented Jul 8 at 2:32
  • 1
    $\begingroup$ What is a "coefficient of determination"? $\endgroup$ Commented Jul 8 at 5:19
  • 1
    $\begingroup$ @DavidG.Stork Please see the content introduction in this web address: en.wikipedia.org/wiki/Coefficient_of_determination $\endgroup$
    – csn899
    Commented Jul 8 at 8:20
  • $\begingroup$ Thanks. I have always referred to it as $R^2$. $\endgroup$ Commented Jul 8 at 18:34

1 Answer 1

5
$\begingroup$

If you want to be "old school" (using linear algebra):

a = Thread[{1, x}];
sol = Inverse[Transpose[a] . a] . Transpose[a] . y
Plot[Inverse[Transpose[a] . a] . Transpose[a] . y . {1, t}, {t, 0.03, 
  0.08}, Epilog -> Point[Thread[{x, y}]], PlotRange -> All]

enter image description here

Using LinearModelFit and comparing with linear algebra:

lm = LinearModelFit[Thread[{x, y}], {1, s}, s];
lm["RSquared"]
d = y - Mean[y];
vec = a . sol - y;
1 - vec . vec/d . d

->

0.947046

0.947046

$\endgroup$
2
  • 2
    $\begingroup$ Does using PseudoInverse count as old school? Or intermediate-oldish school? $\endgroup$
    – ydd
    Commented Jul 8 at 15:24
  • $\begingroup$ Yes you are right. This simple example… I was just sadly amusing myself $\endgroup$
    – ubpdqn
    Commented Jul 8 at 20:13

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