Multiple Regression with math.net

自作多情 提交于 2019-12-12 01:29:29

问题


Hello I am trying to get multiple regression with math.net and I am a little confused.

var xdata = new DenseMatrix(
 new double[,]{{1, 36, 66, 45, 32},
             {1, 37, 68, 12, 2},
             {1, 47, 64, 78, 34},
             {1, 32, 53, 56, 32},
             {1, 1, 101, 24, 90}});

        var ydata = new double[] { 15, 20, 25, 55, 95 };

        var X = DenseMatrix.CreateFromColumns(new[] { new DenseVector(xdata.Length, 1), new DenseVector(xdata) });
        var y = new DenseVector(ydata);

        var p = X.QR().Solve(y);
        var a = p[0];
        var b = p[1];

I guess I don't understand Math.Net, any help with this would be great. Basically I have multiple x and a single y and need to get the coefficient data from them.


回答1:


From the way you've prepared your matrix (i.e. first column always 1), it seems to me you actually have 4 independent variables and you're looking for a simple regression with just a linear combination of all the independent variables as target function:

y : (x1, ..., x4) -> p0 + p1*x1 + ... + p4*x4

If this is the case, just remove the line var X = ... and instead rename xdata to X, then all 5 parameters will be available in the p vector as expected.

Given the data above, you'll end up with approximately:

y : (x1, ..., x4) -> 123.2 - 8.9*x1 + 2.8*x2 + 3.7*x3 - 4.4*x4


来源:https://stackoverflow.com/questions/15394328/multiple-regression-with-math-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!