2

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
1teamsah
  • 1,863
  • 3
  • 23
  • 43

2 Answers2

1

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.

Christoph Rüegg
  • 4,626
  • 1
  • 20
  • 34
  • 1
    Christoph, could you help with a few lines of C# code? I think I understand the concept here is xj is not a scalar but a vector itself (tuple as you wrote), but I can not figure out how the actual preparing the input data will be. Thx – g.pickardou Mar 12 '13 at 14:50
  • Sure. Can you pick a target function I should use in the sample code? Maybe something like `y: (u,v) -> a+b*sin(u)+c*cos(v)`? – Christoph Rüegg Mar 13 '13 at 08:12
  • Christoph, thx for the feedback. Meanwhile I've managed to solve the multiple case, I have to say Math.NET is cool!. – g.pickardou Mar 14 '13 at 08:46
1

@christoph-ruegg Thank you for your post on Linear Regression, that helped me to get started with Math.NET.
@team16sah @g-pickardou If you have access to the Math.NET library, I suggest you use the Fit.Polynomial() method. I found it more reliable and flexible than just using the Linear Regression.
In your case above, the code would look like:

        var xdata = new double[] { 10, 20, 30, 40, 50 };
        var ydata = new double[] { 15, 20, 25, 55, 95 };

        double[] p = Fit.Polynomial(xdata, ydata, 1);
        var a = p[0];
        var b = p[1];

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

Then you can change the polynomial order (third parameter of the Polynomial function) to get more precision.

wip
  • 2,313
  • 5
  • 31
  • 47