Huggsboson
Huggsboson

Reputation: 11

Accord.net SimpleLinearRegression regress method obsolete?

I just started learning accord.net, and while going through some examples I noticed that the Regress method on the SimpleLinearRegression is obsolete.

Apparently I should use the OrdinaryLeastSquares class, but I cannot find anything that will return the residual sum of squares, similar to the Regress method.

Do I need to create this method by myself?

Upvotes: 0

Views: 562

Answers (1)

Cesar
Cesar

Reputation: 2118

Here is a full example on how to learn a SimpleLinearRegression and still be able to compute the residual sum of squares as you have been doing using the previous version of the framework:

// This is the same data from the example available at
// http://mathbits.com/MathBits/TISection/Statistics2/logarithmic.htm

// Declare your inputs and output data
double[] inputs = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
double[] outputs = { 6, 9.5, 13, 15, 16.5, 17.5, 18.5, 19, 19.5, 19.7, 19.8 };

// Transform inputs to logarithms
double[] logx = Matrix.Log(inputs);

// Use Ordinary Least Squares to learn the regression
OrdinaryLeastSquares ols = new OrdinaryLeastSquares();

// Use OLS to learn the simple linear regression
SimpleLinearRegression lr = ols.Learn(logx, outputs);

// Compute predicted values for inputs
double[] predicted = lr.Transform(logx);

// Get an expression representing the learned regression model
// We just have to remember that 'x' will actually mean 'log(x)'
string result = lr.ToString("N4", CultureInfo.InvariantCulture);

// Result will be "y(x) = 6.1082x + 6.0993"

// The mean squared error between the expected and the predicted is
double error = new SquareLoss(outputs).Loss(predicted); // 0.261454

The last line in this example is the one that should be the most interesting to you. As you can see, the residual sum of squares that beforehand was being returned by the .Regress method can now be computed using the SquareLoss class. The advantages of this approach is that now you should be able to compute the most appropriate metric that matters the most to you, such as the ZeroOneLoss or the Euclidean loss or the Hamming loss.

In any case, I just wanted to reiterate that any methods marked as Obsolete in the framework are not going to stop working anytime soon. They are marked as obsolete meaning that new features will not be supported when using those methods, but your application will not stop working in case you have used any of those methods from within it.

Upvotes: 1

Related Questions