GoodnessOfFit.RSquared returning NaNs


(Anton Vrdoljak) #1

Evening all,

I am new here, and new with C#. Earlier in this year I started to work on
one scientific project, and because I needed a lot of numeric I installed
package id MathNet.Numerics, version 3.15.0, targetFramework net452

and later I made an update, of course, so current package is:
package id MathNet.Numerics, version 3.17.0, targetFramework net452

Now will try to describe what problem I found regarding Curve Fitting: Linear Regression.
I used your example for: How well do the parameters fit the data?
In other words I used:
R2Matrix[i, j] = GoodnessOfFit.RSquared(xdata.Select(x => a + b * x), ydata);

inside nested FOR loop (i, j = 0, 1, … 506), where I am creating xdata and ydata,
a 2 double vectors (x0, x1, …, x19), (y0, y1, …, y19)

At the end, I always finished with 33 pairs that will return NaN. ALL these pairs having
b = 0 for slope, and in ALL other cases I am having correct coefficient of determination.
When I changed an instruction in:
R2Matrix[i, j] = GoodnessOfFit.RSquared(xdata, ydata);

ALL was correct, as well as when I was checking all 33 cases with MS Excel!
Next, I made my function for calculating the coefficient of determination, and never had NaNs.

At the end, I want that someone check all this above ^^, I can write one example:
xdata ={ 0.93, 0.98, 0.98, 1.01, 0.88, 1.02, 1.02, 0.89, 0.79, 0.85, 1.05, 0.84, 0.78, 1.02, 0.99, 0.83, 0.93, 0.81, 1. 1.01}
ydata ={0, 1, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}

Thanx for reading!


(Peter Vanderwaart) #2

When I ran your example (below), it returned R_SQ = 1.219E-32, i.e, just about zero.

double[] xdata ={ 0.93, 0.98, 0.98, 1.01, 0.88, 1.02, 1.02, 0.89, 0.79, 0.85,1.05,0.84,0.78,1.02,0.99,0.83,0.93,0.81,1.0,1.01};
            double[] ydata ={ 0, 1, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            double R_SQ = GoodnessOfFit.RSquared(xdata, ydata);

Edit: A common reason to get a NaN is division by zero. There is a division in the formula for r-squared for which the denominator will be zero if the all the y values are equal.