简体   繁体   中英

GoodnessOfFit.StandardError wrong answer

Why am I getting the wrong answer (err2) from GoodnessOfFit.StandardError? In the code below, I do the computation myself and get the right answer (err3). I get the right answer from GoodnessOfFit.RSquared. Note: esttime and phrf are double[]. Length is 63.

    Tuple<double, double> p = Fit.Line(esttime, phrf);
    double ss = 0.0;
    for (int j = 0; j < esttime.Length; j++)
    {
        est[j] = p.Item1 + p.Item2 * esttime[j];
        ss += Math.Pow(est[j] - phrf[j], 2);
    }
    double err2 = GoodnessOfFit.StandardError(est, phrf, phrf.Length - 2);
    Console.WriteLine(err2.ToString()); //writes 70.91 which is wrong
    double err3 = Math.Sqrt(ss / est.Length - 2);
    Console.WriteLine(err3.ToString()); // writes 12.56 which is correct

Answer: The third argument, Degrees of Freedom, is actually the number of degrees of freedom lost in the regression. So in the example it should be 2 and not phrf.Length - 2. Even so, it does not match my calculation, and Excel's, exactly.

I get the exact same result with excel and Math.NET for a polynomial fit when I set the degrees of freedom to be the order of the polynomial fit + 1. So, in the following exemple:

double[] paramsPoly = MathNet.Numerics.Fit.Polynomial(X.ToArray(),Y.ToArray(),6);

The StandardError function must receive 6 + 1 as it's last parameter:

double sey = MathNet.Numerics.GoodnessOfFit.StandardError(Yest, Y, 7);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM