SimpleLinearRegression Class 
Namespace: Accord.Statistics.Models.Regression.Linear
[SerializableAttribute] public class SimpleLinearRegression : TransformBase<double, double>, ILinearRegression
The SimpleLinearRegression type exposes the following members.
Name  Description  

SimpleLinearRegression 
Creates a new Simple Linear Regression of the form y = Ax + B.

Name  Description  

Intercept 
Linear coefficient (Intercept).
 
NumberOfInputs 
Gets the number of inputs accepted by the model.
(Inherited from TransformBaseTInput, TOutput.)  
NumberOfOutputs 
Gets the number of outputs generated by the model.
(Inherited from TransformBaseTInput, TOutput.)  
NumberOfParameters 
Gets the number of parameters in the model (returns 2).
 
Slope 
Angular coefficient (Slope).

Name  Description  

CoefficientOfDetermination(Double, Double) 
Gets the coefficient of determination, or R² (rsquared).
 
CoefficientOfDetermination(Double, Double, Boolean) 
Gets the coefficient of determination, as known as R² (rsquared).
 
Compute(Double)  Obsolete.
Computes the regression for a single input.
 
Compute(Double)  Obsolete.
Computes the regression output for a given input.
 
Equals  Determines whether the specified object is equal to the current object. (Inherited from Object.)  
Finalize  Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)  
FromData 
Creates a new linear regression directly from data points.
 
GetConfidenceInterval 
Gets the confidence interval for an input point.
 
GetDegreesOfFreedom 
Gets the degrees of freedom when fitting the regression.
 
GetHashCode  Serves as the default hash function. (Inherited from Object.)  
GetPredictionInterval 
Gets the prediction interval for an input point.
 
GetPredictionStandardError 
Gets the standard error of the prediction for a particular input point.
 
GetStandardError(Double, Double) 
Gets the overall regression standard error.
 
GetStandardError(Double, Double, Double) 
Gets the standard error of the fit for a particular input point.
 
GetType  Gets the Type of the current instance. (Inherited from Object.)  
MemberwiseClone  Creates a shallow copy of the current Object. (Inherited from Object.)  
Regress  Obsolete.
Performs the regression using the input and output
data, returning the sum of squared errors of the fit.
 
ToString 
Returns a System.String representing the regression.
(Overrides ObjectToString.)  
ToString(IFormatProvider) 
Returns a System.String representing the regression.
 
ToString(String) 
Returns a System.String representing the regression.
 
ToString(String, IFormatProvider) 
Returns a System.String representing the regression.
 
Transform(TInput) 
Applies the transformation to a set of input vectors,
producing an associated set of output vectors.
(Inherited from TransformBaseTInput, TOutput.)  
Transform(Double) 
Applies the transformation to an input, producing an associated output.
(Overrides TransformBaseTInput, TOutputTransform(TInput).)  
Transform(TInput, TOutput) 
Applies the transformation to an input, producing an associated output.
(Inherited from TransformBaseTInput, TOutput.) 
Name  Description  

HasMethod 
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)  
IsEqual  Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices. (Defined by Matrix.)  
ToT  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.)  
ToT  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 Matrix.) 
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 minimumsquarederror 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 minimumsquarederror 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