Click or drag to resize
Accord.NET (logo)

SimpleLinearRegression Class

Simple Linear Regression of the form y = Ax + B.
Inheritance Hierarchy
SystemObject
  Accord.MachineLearningTransformBaseDouble, Double
    Accord.Statistics.Models.Regression.LinearSimpleLinearRegression

Namespace:  Accord.Statistics.Models.Regression.Linear
Assembly:  Accord.Statistics (in Accord.Statistics.dll) Version: 3.8.0
Syntax
[SerializableAttribute]
public class SimpleLinearRegression : TransformBase<double, double>, 
	ILinearRegression
Request Example View Source

The SimpleLinearRegression type exposes the following members.

Constructors
  NameDescription
Public methodSimpleLinearRegression
Creates a new Simple Linear Regression of the form y = Ax + B.
Top
Properties
Methods
  NameDescription
Public methodCoefficientOfDetermination(Double, Double, Double)
Gets the coefficient of determination, or R² (r-squared).
Public methodCoefficientOfDetermination(Double, Double, Boolean, Double)
Gets the coefficient of determination, as known as R² (r-squared).
Public methodCompute(Double) Obsolete.
Computes the regression for a single input.
Public methodCompute(Double) Obsolete.
Computes the regression output for a given input.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodStatic memberFromData
Creates a new linear regression directly from data points.
Public methodGetConfidenceInterval
Gets the confidence interval for an input point.
Public methodGetDegreesOfFreedom
Gets the degrees of freedom when fitting the regression.
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetPredictionInterval
Gets the prediction interval for an input point.
Public methodGetPredictionStandardError
Gets the standard error of the prediction for a particular input point.
Public methodGetStandardError(Double, Double)
Gets the overall regression standard error.
Public methodGetStandardError(Double, Double, Double)
Gets the standard error of the fit for a particular input point.
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodRegress Obsolete.
Performs the regression using the input and output data, returning the sum of squared errors of the fit.
Public methodToString
Returns a System.String representing the regression.
(Overrides ObjectToString.)
Public methodToString(IFormatProvider)
Returns a System.String representing the regression.
Public methodToString(String)
Returns a System.String representing the regression.
Public methodToString(String, IFormatProvider)
Returns a System.String representing the regression.
Public methodTransform(TInput)
Applies the transformation to a set of input vectors, producing an associated set of output vectors.
(Inherited from TransformBaseTInput, TOutput.)
Public methodTransform(Double)
Applies the transformation to an input, producing an associated output.
(Overrides TransformBaseTInput, TOutputTransform(TInput).)
Public methodTransform(TInput, TOutput)
Applies the transformation to an input, producing an associated output.
(Inherited from TransformBaseTInput, TOutput.)
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Top
Remarks
In linear regression, the model specification is that the dependent variable, y is a linear combination of the parameters (but need not be linear in the independent variables). As the linear regression has a closed form solution, the regression coefficients can be efficiently computed using the Regress method of this class.
Examples

Let's say we have some univariate, continuous sets of input data, and a corresponding univariate, continuous set of output data, such as a set of points in R². A simple linear regression is able to fit a line relating the input variables to the output variables in which the minimum-squared-error of the line and the actual output points is minimum.

// Let's say we have some univariate, continuous sets of input data,
// and a corresponding univariate, continuous set of output data, such
// as a set of points in R². A simple linear regression is able to fit
// a line relating the input variables to the output variables in which
// the minimum-squared-error of the line and the actual output points
// is minimum.

// Declare some sample test data.
double[] inputs = { 80, 60, 10, 20, 30 };
double[] outputs = { 20, 40, 30, 50, 60 };

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

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

// Compute the output for a given input:
double y = regression.Transform(85); // The answer will be 28.088

// We can also extract the slope and the intercept term
// for the line. Those will be -0.26 and 50.5, respectively.
double s = regression.Slope;     // -0.264706
double c = regression.Intercept; // 50.588235

Now, let's say we would like to perform a regression using an intermediary transformation, such as for example logarithmic regression. In this case, all we have to do is to first transform the input variables into the desired domain, then apply the regression as normal:

// 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
See Also