My Problem
I can't get expected results from a multiple linear regression calculation in accord framework and math.net
I'm trying to write a little program to calculate regression constants to predict the lifetime of a product by providing test results with variants of the product.
Im not very good at the math behind this but what i'm trying to archive is to get the same results then i get in Microsoft Excel when using "Regression".
How i test:
public class RegressionResult
{
public double weight1 { get; set; }
public double weight2 { get; set; }
public double interception { get; set; }
public override string ToString()
{
return weight1 + " - " + weight2 + " - " + interception;
}
}
public RegressionResult CalcVals(double[][] x, double[] y)
{
var ols = new OrdinaryLeastSquares()
{
UseIntercept = true
};
MultipleLinearRegression regressiontest = ols.Learn(x, y);
double a = regressiontest.Weights[0]; // a = 0
double b = regressiontest.Weights[1]; // b = 0
double c = regressiontest.Intercept; // c = 1
RegressionResult res = new RegressionResult();
res.weight1 = a;
res.weight2 = b;
res.interception = c;
return res;
}
Even when i try the example code under "Multiple Linear Regression":
https://github.com/accord-net/framework/wiki/Regression
My results are:
a = 6.63599155010096E-17
b = 1.8129866073473581E-16
c = 0.99999999999999978
After the comment from James i realized this is correct because it is 0,0,1.
Why i checked the example
When i try to reproduce the excel calculations i get the following results:
(Input is always the X Range in Excel, Output the Y)
Working
double[][] input = {
new double[] { 1, 4 },
new double[] { 2, 5 },
new double[] { 3, 2 },
};
double[] output = new double[] { 15, 20, 10 };
Gives me c# : Weight1: 1,2499, Weight2: 3,7499, Intercept: -1,2499 Same results in Excel
double[][] input = {
new double[] { 10, 11 },
new double[] { 20, 21 },
new double[] { 30, 31 },
};
double[] output = new double[] { 15.0002, 20.0002, 25.0002 };
Gives me weight1: -0,00001 weight2: 0,5 intercept: 9,5 Same results in Excel
Getting strange
double[][] input = {
new double[] { 10.0002, 11.0002 },
new double[] { 20.0002, 21.0002 },
new double[] { 30.0002, 31.0002 },
};
double[] output = new double[] { 15.0002, 20.0002, 25.0002 };
weight1: 0,5 weight2: -3,8064 intercept: 10,0001
Excel gives me: weight1: -0,00001 weight2: 0,5 intercept: 9,5
double[][] input = {
new double[] { 10d, 11d },
new double[] { 20d, 21d },
new double[] { 30d, 31d },
};
double[] output = new double[] { 15d, 20d, 25d };
weight1: -0,4999 weight2: 0,9999, intercept: 9 Excel still: weight1: -0,00001 weight2: 0,5 intercept: 9,5
Completly lost
Again to this Calculation:
double[][] input = {
new double[] { 10, 11 },
new double[] { 20, 21 },
new double[] { 30, 31 },
};
double[] output = new double[] { 15.0002, 20.0002, 25.0002 };
Perfect, same Results in Excel and C# (weight1: -0,00001 weight2: 0,5 intercept: 9,5)
But now:
double[][] input = {
new double[] { 10, 11 },
new double[] { 20, 21 },
new double[] { 30, 31 },
};
double[] output = new double[] { 15.0003, 20.0002, 25.0002 };
I just changed 15.0002 to 15.0003 and now i get:
weight1: 28147497670,9996 weight2: -28147497670,4996 intercept: 28147497680,5}
Help!
First i thought it's some problem with localization but after trying for hours now, i don't have any idea what i'm doing wrong. I don't even know if Excel is wrong or the librarys.
I also tried:
Math.Net
Fit.MultiDim
MultipleRegression.NormalEquations
MultipleRegression.QR
MultipleRegression.SVD
Short example of real values im trying to calculate!
var xin = new[] {
new[] { 2.031d, 1.301d, 2.642d },
new[] { 2.253d, 1.301d, 2.931d },
new[] { 2.031d, 1.301d, 2.642d },
new[] { 2.156d, 1.301d, 2.805d }
};
var yin = new[] { 5.509d, 5.119d, 5.590d, 5.406d };