0

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
user1221399
  • 41
  • 2
  • 11

1 Answers1

0

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
Christoph Rüegg
  • 4,626
  • 1
  • 20
  • 34