# NMath User's Guide

42.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

```var 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 ]" );
```
```var obs =
```
```  new DoubleVector( "[ 67 79 61 75 59 90 52 87 ]" );
```
```var lr = new LinearRegression( predictors, obs );
```

Code Example – VB linear regression

```Dim Predictors As 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 ]")
```
```Dim Obs As New DoubleVector("[ 67 79 61 75 59 90 52 87 ]")
```
```Dim LR As 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 );
```

Code Example – VB linear regression

```Dim LR As 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.

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

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

Class solves the regression problem using a singular value decomposition.

Class 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 );
```

Code Example – VB linear regression

```Dim CalcObj As New CORegressionCalculation()
```
```CalcObj.Tolerance = 0.00000001
```
```Dim LR As 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.

Intercept Parameters

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

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

Code Example – VB linear regression

```Dim LR As New LinearRegression(Predictors, Obs, True)
```

This code does not:

Code Example – C# linear regression

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

Code Example – VB linear regression

```Dim LR As New LinearRegression(Predictors, Obs, False)
```

Top

Top