1teamsah
1teamsah

Reputation: 1933

Multiple Regression in Math.Net Numerics

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.

Upvotes: 2

Views: 9766

Answers (2)

wip
wip

Reputation: 2442

@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.

Upvotes: 1

Christoph Rüegg
Christoph Rüegg

Reputation: 4736

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.

Upvotes: 1

Related Questions