-4

Sorry about the vagueness of my original question. Here is a revised and more detailed format of the same question.

The example I have below references this page: https://numerics.mathdotnet.com/regression.html

The page shows me how to get the slope and intercept of a simple regression that has two one-dimensional array inputs, namely xdata and ydata:

double[] xdata = new double[] { 10, 20, 30 };
double[] ydata = new double[] { 15, 20, 25 };

Tuple<double, double> p = Fit.Line(xdata, ydata);
double a = p.Item1; // == 10; intercept
double b = p.Item2; // == 0.5; slope

Based on the xdata and ydata inputs, I think I can get the coefficient by doing this:

var coefficient = GoodnessOfFit.R(xdata, ydata);

Now what I'm trying to figure out is, how do I get the intercept, slope and coefficient values if the xdata input is a two-dimensional array. Here are the xdata and ydata input values:

double[][] xdata = new[] { new[] { 1.0, 4.0 }, new[] { 2.0, 5.0 }, new[] { 3.0, 2.0 } };
double[] ydata = new[] { 15.0, 20, 10 };

There is a section on the math.net page above that talks about multiple regression but I'm not sure how I can extract the slope, intercept and coefficient values from it:

double[] p = Fit.MultiDim(xdata, ydata, intercept: true);

Please advise. Thanks!

Jon
  • 11
  • 2
  • 2
    What have you tried Jon? Your questions will be down-voted and eventually removed if you do not share with the community what you have attempted. Code examples are king. – JakeofSpades Feb 05 '18 at 21:44
  • `I can't seem to find the code I'm looking for` Drastic situations call for drastic actions: maybe you could *write* some? Please read [ask] and take the [tour] if you are going to stick around – Ňɏssa Pøngjǣrdenlarp Feb 05 '18 at 22:45
  • I have revised my original question. That was my very first post so sorry about that. Thanks! – Jon Feb 07 '18 at 03:28

1 Answers1

0

Fit.MultiDim is a function that returns a double[]. I've not used MultiDim, but I'm pretty sure that the coefficient values are returned in the array as described in the documentation for other regression methods.

To use to GoodnessOfFit, use the coefficients to create an array of modeled values. These and the original Y values go into R, StandardError, etc. Note that where it calls for degreesOfFreedom, it's actually asking for the number of DF reduced by the regression = # coefficients = 3 in your case.

phv3773
  • 487
  • 4
  • 10
  • Thanks for the reply. **Do you have the C# code that shows how slope, intercept and coefficient values can be pulled?** I'm a bit confused when you said the coefficient values are returned from the double[] array returned by MultiDim. Based on the example which involves one-dimensional xdata and ydata variables, only intercept and slope can be extracted from the return, not coefficient. Also what is DF? – Jon Feb 07 '18 at 15:43
  • As an example, MultiDim is used estimate the coefficients p0,p1,p2 in the formula Y = p0 + p1*x1 +p2*x2 where x1 and x2 are independent variable and Y is the dependent variable. The array returned by MultiDim would be {p1,p2,p2). – phv3773 Feb 07 '18 at 17:45
  • Intercept and slope are not defined for functions of more than one independent variable in the same way they are for the simple case. – phv3773 Feb 07 '18 at 17:52