Click or drag to resize
Accord.NET (logo)

MultipleLinearRegression Class

Multiple Linear Regression.
Inheritance Hierarchy
SystemObject
  Accord.MachineLearningTransformBaseDouble, Double
    Accord.Statistics.Models.Regression.LinearMultipleLinearRegression

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

The MultipleLinearRegression type exposes the following members.

Constructors
  NameDescription
Public methodMultipleLinearRegression
Initializes a new instance of the MultipleLinearRegression class.
Public methodMultipleLinearRegression(Int32) Obsolete.
Creates a new Multiple Linear Regression.
Public methodMultipleLinearRegression(Int32, Boolean) Obsolete.
Creates a new Multiple Linear Regression.
Public methodMultipleLinearRegression(Int32, Double) Obsolete.
Creates a new Multiple Linear Regression.
Top
Properties
  NameDescription
Public propertyCoefficients 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.
Public propertyHasIntercept Obsolete.
Gets whether this model has an additional intercept term.
Public propertyInputs Obsolete.
Gets the number of inputs for the regression model.
Public propertyIntercept
Gets or sets the intercept value for the regression.
Public propertyNumberOfInputs
Gets the number of inputs accepted by the model.
(Overrides TransformBaseTInput, TOutputNumberOfInputs.)
Public propertyNumberOfOutputs
Gets the number of outputs generated by the model.
(Inherited from TransformBaseTInput, TOutput.)
Public propertyNumberOfParameters
Gets the number of parameters in this model (equals the NumberOfInputs + 1).
Public propertyWeights
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.
Top
Methods
  NameDescription
Public methodClone
Creates a new object that is a copy of the current instance.
Public methodCoefficientOfDetermination
Gets the coefficient of determination, as known as R² (r-squared).
Public methodCompute(Double) Obsolete.
Computes the Multiple Linear Regression for an input vector.
Public methodCompute(Double) Obsolete.
Computes the Multiple Linear Regression for input vectors.
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 memberFromCoefficients Obsolete.
Creates a new linear regression from the regression coefficients.
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 vector.
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 vector.
Public methodGetStandardErrors
Gets the standard error for each coefficient.
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(Double, Double) Obsolete.
Performs the regression using the input vectors and output data, returning the sum of squared errors of the fit.
Public methodRegress(Double, Double, Boolean) Obsolete.
Performs the regression using the input vectors and output data, returning the sum of squared errors of the fit.
Public methodRegress(Double, Double, Double, Boolean) Obsolete.
Performs the regression using the input vectors 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(String, IFormatProvider)
Returns a String that represents this instance.
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 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.

Examples

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 user-friendly 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 one-hot 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 one-hot 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 one-hot 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 user-friendly 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
See Also