# NMath Stats User's Guide

6.4 Accessing and Modifying the Model (.NET, C#, CSharp, VB, Visual Basic, F#)

Class LinearRegression provides a variety of properties and member functions for accessing and modifying the predictors in the model, the observations, and the intercept option.

Accessing and Modifying Predictors

Class LinearRegression provides the following properties for accessing the predictors in the model:

RegressionMatrix gets the regression matrix.

PredictorMatrix gets the predictor matrix. If the model contains an intercept parameter, then the predictor matrix is obtained from the regression matrix by removing the leading column of ones. If the model does not have an intercept parameter then the predictor matrix is the same as the regression matrix.

NumberOfParameters gets the number of parameters in the model.

NumberOfPredictors gets the number of predictors in the model. If the model contains an intercept parameter then the number of predictors is equal to the number of parameters minus one. If the model does not contain an intercept parameter, then the number of predictors is equal to the number of parameters.

If you modify the data in the regression or predictor matrix using the reference returned by RegressionMatrix or PredictorMatrix, respectively, invoke method RecalculateParameters() to recalculate the regression parameters. For instance:

Code Example – C# linear regression

```lr.PredictorMatrix[2,13] = 15.4;
lr.RecalculateParameters();
```

Member functions are also provided for adding and removing one or more predictors. The AddPredictor() method appends a given column of predictor values to the predictor matrix, and recalculates the parameters:

Code Example – C# linear regression

```DoubleVector predictors =
new DoubleVector( "[ 1.43 5.5 0.43 14.2 9.0 ]" );

lr.AddPredictor( predictors );
```

A MismatchedSizeException is thrown if the number of predictor values is not equal to the number of rows in the regression matrix (also equal to the length of the observation vector).

Similarly, AddPredictors() adds a matrix of predictors. Each column of the input matrix is a set of observed predictor values. This, this code adds three predictors:

Code Example – C# linear regression

```DoubleMatrix predictors =
new DoubleMatrix( " 8x3 [ 1450 .50 70
1600 .50 70
1450 .70 70
1600 .70 70
1450 .50 120
1600 .50 120
1450 .70 120
1600 .70 120 ]" );
lr.AddPredictor( predictors );
```

The RemovePredictor() method removes the ith predictor from the model and recalculates the parameters. This code removes the predictor at (zero-based) index 4:

Code Example – C# linear regression

```lr.RemovePredictor( 4 );
```

If the model has an intercept parameter, removing the 0th predictor will not remove the intercept parameter. Use the RemoveInterceptParameter() method to remove the intercept parameter (see below).

RemovePredictors() removes the specified number of columns from the predictor matrix beginning with the specified column. Thus, this code removes the second, third, and fourth predictors:

Code Example – C# linear regression

```lr.RemovePredictors( 1, 3 );
```

Accessing and Modifying Observations

The Observations property gets the vector of observations. If you use the returned reference to modify the observation vector, invoke method RecalculateParameters() to recalculate the regression parameters. For instance:

Code Example – C# linear regression

```lr.Observations = 0.965;
lr.RecalculateParameters();
```

The NumberOfObservations property gets the number of observations, which is simply the length of the observation vector, and also the number of rows in the regression matrix.

Member functions are also provided for adding and removing one or more observations. The AddObservation() method appends a given row of predictor values to the predictor matrix and a given observation to the observation vector, and recalculates the parameters:

Code Example – C# linear regression

```DoubleVector predictors =
new DoubleVector( "[ 1.43 5.5 0.43 14.2 9.0 ]" );
double obs = 2.5;

lr.AddObservation( predictors, obs );
```

NOTE—If the model has an intercept parameter, do not include the leading one in the predictors vector. It will be accounted for in the model.

A MismatchedSizeException is thrown if the length of the predictors vector is not equal to the number of predictors in the model.

Similarly, AddObservations() adds a collection of observations:

Code Example – C# linear regression

```var predictors =
new DoubleMatrix( "3x4 [ 150.0 33.5 0.66 80.0
160.0 24.5 0.88 70.0
170.0 22.6 0.56 60.0 ]" );
var obs = new DoubleVector( "14.2, 15.5, 10.3" );

lr.AddObservation( predictors, obs );
```

RemoveObservation() removes the row at the indicated index from the predictor matrix and the corresponding element from the observation vector. This code removes the observation at (zero-based) index 3:

Code Example – C# linear regression

```lr.RemoveObservation( 3 );
```

RemoveObservations() removes the specified number of rows from the predictor matrix beginning with the specified row. Thus, this code removes the third, fourth, fifth, and sixth observations:

Code Example – C# linear regression

```lr.RemoveObservations( 2, 4 );
```

Accessing and Modifying the Intercept Option

The HasInterceptParameter property gets a boolean value indicating whether or not the model already has an intercept parameter.

The AddInterceptParameter() method adds an intercept parameter to the model and recalculates the parameters. Thus, this code prepends a column of one to the regression matrix:

Code Example – C# linear regression

```lr.AddInterceptParameter()
```

NOTE—If the model already has an intercept parameter AddInterceptParameter() has no effect.

The RemoveInterceptParameter() method removes the intercept parameter.

Updating the Entire Model

Method SetRegressionData() updates the entire model by setting the regression matrix, the observation vector, and the intercept option to the specified values, and recalculating the model parameters. For instance:

Code Example – C# linear regression

```var A = 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 ]" );

lr.SetRegressionData( A, obs, true );
```

Top

Top