**6.1****
****Creating Linear Regressions** (.NET, C#, CSharp, VB, Visual Basic, F#)

A **LinearRegression**
instance is constructed from a predictor matrix and observation vector,
like so:

Code Example – C# linear regression

DoubleMatrix predictors = new DoubleMatrix( " 8x4 [ 1 1450 .50 70 1 1600 .50 70 1 1450 .70 70 1 1600 .70 70 1 1450 .50 120 1 1600 .50 120 1 1450 .70 120 1 1600 .70 120 ]" ); DoubleVector obs = new DoubleVector( "[ 67 79 61 75 59 90 52 87 ]" ); var lr = new LinearRegression( predictors, obs );

A **MismatchedSizeException**
is raised if the number of rows in the matrix A
is not equal to the length of the vector obs.

You can also construct a **LinearRegression**
instance from data in a **DataFrame**,
by indicating which column contains the observations. Non-numeric columns
are ignored. For instance, if column 8 contains
the dependent variable, this code constructs a regression from the data:

Code Example – C# linear regression

var lr = new LinearRegression( df, 8 );

**Parameter Calculation by Least
Squares Minimization**

By default, class **LinearRegression**
computes the model parameter values by the *method
of **least squares* using
a QR factorization, but you may elect to use a complete orthogonal factorization
or singular value decomposition instead.

**IRegressionCalculation**
is the interface for classes used by **LinearRegression**
to calculate regression parameters. **NMath
Stats** includes three regression calculator classes:

● Class **QRRegressionCalculation**
(the default) solves the regression problem using a QR decomposition.

● Class **SVDRegressionCalculation**
solves the regression problem using a singular value decomposition.

● Class **CORegressionCalculation**
solves least squares problems using a complete orthogonal decomposition.

You can specify a non-default regression calculation object in the constructor. For example:

Code Example – C# linear regression

var calcObj = new CORegressionCalculation(); calcObj.Tolerance = 1e-8; var lr = new LinearRegression( predictors, obs, calcObj );

The Tolerance property is used for computing numerical rank. Values with less than the specified tolerance are considered zero when computing the effective rank.

After construction, the regression calculator used
by a **LinearRegression** instance
can be changed using the RegressionCalculator
property.

If the linear model Ax
= y contains a non-zero intercept parameter, then the first column
of matrix A must be all ones. Some of the
**LinearRegression** constructors allow
you to specify whether a column of ones should be prepended to the data
in the input regression matrix, or whether the regression matrix should
be used as it is given. Thus, this code prepends a column of ones:

Code Example – C# linear regression

LinearRegression lr = new LinearRegression( predictors, obs, true );

This code does not:

Code Example – C# linear regression

LinearRegression lr = new LinearRegression( predictors, obs, false );