Multiple Regression in Math.Net Numerics

1teamsah picture 1teamsah · Jan 7, 2013 · Viewed 8.2k times · Source

I achieved simple single regression using math.net regression method like this:

var xdata = new double[] { 10, 20, 30, 40, 50 };
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];

MessageBox.Show(a.ToString(), "Test");
MessageBox.Show(b.ToString(), "Test");

Question is: What can I apply multiple regression with this method? So, I have also zdata array and I want to use this for multiple regression.

Answer

Christoph Rüegg picture Christoph Rüegg · Jan 8, 2013

This form, as introduced in Linear Regression With Math.NET Numerics, is technically already a multiple linear regression.

Assuming you have data points ((uj,vj),yj) instead of (xj,yj), you can map x to the tuple u,v accordingly when building the X matrix. So the cell X[i,j] would then instead of fi(xj) be evaluated as fi(uj,vj).

For example, for a linear regression to a spatial line given by y=a+b*u+c*v, you would end up with:

  • p1 = a, f1 : u,v -> 1
  • p2 = b, f2 : u,v -> u
  • p3 = c, f3 : u,v -> v

Hence the full system:

|y1|   |1  u1  v1|   |a|
|y2| = |1  u2  v2| * |b|
|..|   |.. ..  ..|   |c|
|yM|   |1  uM  vM|

Please leave a comment if it is unclear how this would work in actual code, or not what you're looking for.