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

MultipleLinearRegression 
Initializes a new instance of the MultipleLinearRegression class.
 
MultipleLinearRegression(Int32)  Obsolete.
Creates a new Multiple Linear Regression.
 
MultipleLinearRegression(Int32, Boolean)  Obsolete.
Creates a new Multiple Linear Regression.
 
MultipleLinearRegression(Int32, Double)  Obsolete.
Creates a new Multiple Linear Regression.

Name  Description  

Coefficients  Obsolete.
Gets the coefficients used by the regression model. If the model
contains an intercept term, it will be in the end of the vector.
 
HasIntercept  Obsolete.
Gets whether this model has an additional intercept term.
 
Inputs  Obsolete.
Gets the number of inputs for the regression model.
 
Intercept 
Gets or sets the intercept value for the regression.
 
NumberOfInputs 
Gets the number of inputs accepted by the model.
(Overrides TransformBaseTInput, TOutputNumberOfInputs.)  
NumberOfOutputs 
Gets the number of outputs generated by the model.
(Inherited from TransformBaseTInput, TOutput.)  
NumberOfParameters 
Gets the number of parameters in this model (equals the NumberOfInputs + 1).
 
Weights 
Gets or sets the linear weights of the regression model. The
intercept term is not stored in this vector, but is instead
available through the Intercept property.

Name  Description  

Clone 
Creates a new object that is a copy of the current instance.
 
CoefficientOfDetermination 
Gets the coefficient of determination, as known as R² (rsquared).
 
Compute(Double)  Obsolete.
Computes the Multiple Linear Regression for an input vector.
 
Compute(Double)  Obsolete.
Computes the Multiple Linear Regression for input vectors.
 
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.)  
FromCoefficients  Obsolete.
Creates a new linear regression from the regression coefficients.
 
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 vector.
 
GetStandardError(Double, Double) 
Gets the overall regression standard error.
 
GetStandardError(Double, Double, Double) 
Gets the standard error of the fit for a particular input vector.
 
GetStandardErrors 
Gets the standard error for each coefficient.
 
GetType  Gets the Type of the current instance. (Inherited from Object.)  
MemberwiseClone  Creates a shallow copy of the current Object. (Inherited from Object.)  
Regress(Double, Double)  Obsolete.
Performs the regression using the input vectors and output
data, returning the sum of squared errors of the fit.
 
Regress(Double, Double, Boolean)  Obsolete.
Performs the regression using the input vectors and output
data, returning the sum of squared errors of the fit.
 
Regress(Double, Double, Double, Boolean)  Obsolete.
Performs the regression using the input vectors and output
data, returning the sum of squared errors of the fit.
 
ToString 
Returns a System.String representing the regression.
(Overrides ObjectToString.)  
ToString(String, IFormatProvider) 
Returns a String that represents this instance.
 
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.)  
To(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.)  
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.) 
In multiple linear regression, the model specification is that the dependent variable, denoted y_i, is a linear combination of the parameters (but need not be linear in the independent x_i variables). As the linear regression has a closed form solution, the regression coefficients can be computed by calling the Regress(Double, Double) method only once.
The following example shows how to fit a multiple linear regression model to model a plane as an equation in the form ax + by + c = z.
// We will try to model a plane as an equation in the form // "ax + by + c = z". We have two input variables (x and y) // and we will be trying to find two parameters a and b and // an intercept term c. // We will use Ordinary Least Squares to create a // linear regression model with an intercept term var ols = new OrdinaryLeastSquares() { UseIntercept = true }; // Now suppose you have some points double[][] inputs = { new double[] { 1, 1 }, new double[] { 0, 1 }, new double[] { 1, 0 }, new double[] { 0, 0 }, }; // located in the same Z (z = 1) double[] outputs = { 1, 1, 1, 1 }; // Use Ordinary Least Squares to estimate a regression model MultipleLinearRegression regression = ols.Learn(inputs, outputs); // As result, we will be given the following: double a = regression.Weights[0]; // a = 0 double b = regression.Weights[1]; // b = 0 double c = regression.Intercept; // c = 1 // This is the plane described by the equation // ax + by + c = z => 0x + 0y + 1 = z => 1 = z. // We can compute the predicted points using double[] predicted = regression.Transform(inputs); // And the squared error loss using double error = new SquareLoss(outputs).Loss(predicted); // We can also compute other measures, such as the coefficient of determination r² double r2 = new RSquaredLoss(numberOfInputs: 2, expected: outputs).Loss(predicted); // should be 1 // We can also compute the adjusted or weighted versions of r² using var r2loss = new RSquaredLoss(numberOfInputs: 2, expected: outputs) { Adjust = true, // Weights = weights; // (if you have a weighted problem) }; double ar2 = r2loss.Loss(predicted); // should be 1 // Alternatively, we can also use the less generic, but maybe more userfriendly method directly: double ur2 = regression.CoefficientOfDetermination(inputs, outputs, adjust: true); // should be 1
The next example shows how to fit a multiple linear regression model in conjunction with a discrete codebook to learn from discrete variables using onehot encodings when applicable:
// Let's say we would like predict a continuous number from a set // of discrete and continuous input variables. For this, we will // be using the Servo dataset from UCI's Machine Learning repository // as an example: http://archive.ics.uci.edu/ml/datasets/Servo // Create a Servo dataset Servo servo = new Servo(); object[][] instances = servo.Instances; // 167 x 4 double[] outputs = servo.Output; // 167 x 1 // This dataset contains 4 columns, where the first two are // symbolic (having possible values A, B, C, D, E), and the // last two are continuous. // We will use a codification filter to transform the symbolic // variables into onehot vectors, while keeping the other two // continuous variables intact: var codebook = new Codification<object>() { { "motor", CodificationVariable.Categorical }, { "screw", CodificationVariable.Categorical }, { "pgain", CodificationVariable.Continuous }, { "vgain", CodificationVariable.Continuous }, }; // Learn the codebook codebook.Learn(instances); // We can gather some info about the problem: int numberOfInputs = codebook.NumberOfInputs; // should be 4 (since there are 4 variables) int numberOfOutputs = codebook.NumberOfOutputs; // should be 12 (due their onehot encodings) // Now we can use it to obtain double[] vectors: double[][] inputs = codebook.ToDouble().Transform(instances); // We will use Ordinary Least Squares to create a // linear regression model with an intercept term var ols = new OrdinaryLeastSquares() { UseIntercept = true }; // Use Ordinary Least Squares to estimate a regression model: MultipleLinearRegression regression = ols.Learn(inputs, outputs); // We can compute the predicted points using: double[] predicted = regression.Transform(inputs); // And the squared error using the SquareLoss class: double error = new SquareLoss(outputs).Loss(predicted); // We can also compute other measures, such as the coefficient of determination r² using: double r2 = new RSquaredLoss(numberOfOutputs, outputs).Loss(predicted); // should be 0.55086630162967354 // Or the adjusted or weighted versions of r² using: var r2loss = new RSquaredLoss(numberOfOutputs, outputs) { Adjust = true, // Weights = weights; // (uncomment if you have a weighted problem) }; double ar2 = r2loss.Loss(predicted); // should be 0.51586887058782993 // Alternatively, we can also use the less generic, but maybe more userfriendly method directly: double ur2 = regression.CoefficientOfDetermination(inputs, outputs, adjust: true); // should be 0.51586887058782993
The next example shows how to fit a multiple linear regression model with the additional constraint that none of its coefficients should be negative. For this we can use the NonNegativeLeastSquares learning algorithm instead of the OrdinaryLeastSquares used above.
// Declare training samples var inputs = new double[][] { new[] { 1.0, 1.0, 1.0 }, new[] { 2.0, 4.0, 8.0 }, new[] { 3.0, 9.0, 27.0 }, new[] { 4.0, 16.0, 64.0 }, }; var outputs = new double[] { 0.23, 1.24, 3.81, 8.72 }; // Create a NN LS learning algorithm var nnls = new NonNegativeLeastSquares() { MaxIterations = 100 }; // Use the algorithm to learn a multiple linear regression MultipleLinearRegression regression = nnls.Learn(inputs, outputs); // None of the regression coefficients should be negative: double[] coefficients = regression.Weights; // should be // Check the quality of the regression: double[] prediction = regression.Transform(inputs); double error = new SquareLoss(expected: outputs) .Loss(actual: prediction); // should be 0