g02de adds a new independent variable to a general linear regression model.

# Syntax

C# |
---|

public static void g02de( string weight, int n, int ip, double[,] q, double[] p, double[] wt, double[] x, out double rss, double tol, out int ifail ) |

Visual Basic |
---|

Public Shared Sub g02de ( _ weight As String, _ n As Integer, _ ip As Integer, _ q As Double(,), _ p As Double(), _ wt As Double(), _ x As Double(), _ <OutAttribute> ByRef rss As Double, _ tol As Double, _ <OutAttribute> ByRef ifail As Integer _ ) |

Visual C++ |
---|

public: static void g02de( String^ weight, int n, int ip, array<double,2>^ q, array<double>^ p, array<double>^ wt, array<double>^ x, [OutAttribute] double% rss, double tol, [OutAttribute] int% ifail ) |

F# |
---|

static member g02de : weight : string * n : int * ip : int * q : float[,] * p : float[] * wt : float[] * x : float[] * rss : float byref * tol : float * ifail : int byref -> unit |

#### Parameters

- weight
- Type: System..::..String
*On entry*: indicates if weights are to be used.- ${\mathbf{weight}}=\text{"U"}$
- Least squares estimation is used.
- ${\mathbf{weight}}=\text{"W"}$
- Weighted least squares is used and weights must be supplied in array wt.

*Constraint*: ${\mathbf{weight}}=\text{"U"}$ or $\text{"W"}$.

- n
- Type: System..::..Int32
*On entry*: $n$, the number of observations.*Constraint*: ${\mathbf{n}}\ge 1$.

- ip
- Type: System..::..Int32
*On entry*: $p$, the number of independent variables already in the model.*Constraint*: ${\mathbf{ip}}\ge 0$ and ${\mathbf{ip}}<{\mathbf{n}}$.

- q
- Type: array<System..::..Double,2>[,](,)[,][,]An array of size [dim1, ${\mathbf{ip}}+2$]
**Note:**dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{n}}$*On entry*: if ${\mathbf{ip}}\ne 0$, q must contain the results of the $QR$ decomposition for the model with $p$ parameters as returned by g02da or a previous call to g02de.If ${\mathbf{ip}}=0$, the first column of q should contain the $n$ values of the dependent variable, $y$.*On exit*: the results of the $QR$ decomposition for the model with $p+1$ parameters:- the first column of q contains the updated value of $c$;
- the columns $2$ to ${\mathbf{ip}}+1$ are unchanged;
- the first ${\mathbf{ip}}+1$ elements of column ${\mathbf{ip}}+2$ contain the new column of $R$, while the remaining ${\mathbf{n}}-{\mathbf{ip}}-1$ elements contain details of the matrix ${Q}_{p+1}$.

- p
- Type: array<System..::..Double>[]()[][]An array of size [${\mathbf{ip}}+1$]

- wt
- Type: array<System..::..Double>[]()[][]An array of size [dim1]
**Note:**the dimension of the array wt must be at least ${\mathbf{n}}$ if ${\mathbf{weight}}=\text{"W"}$, and at least $1$ otherwise.*On entry*: if ${\mathbf{weight}}=\text{"W"}$ , wt must contain the weights to be used.If ${\mathbf{wt}}\left[i-1\right]=0.0$, the $i$th observation is not included in the model, in which case the effective number of observations is the number of observations with nonzero weights.If ${\mathbf{weight}}=\text{"U"}$, wt is not referenced and the effective number of observations is $n$.*Constraint*: if ${\mathbf{weight}}=\text{"W"}$, ${\mathbf{wt}}\left[\mathit{i}-1\right]\ge 0.0$, for $\mathit{i}=1,2,\dots ,n$.

- x
- Type: array<System..::..Double>[]()[][]An array of size [n]
*On entry*: $x$, the new independent variable.

- rss
- Type: System..::..Double%
*On exit*: the residual sum of squares for the new fitted model.**Note:**this will only be valid if the model is of full rank, see [Further Comments].

- tol
- Type: System..::..Double
*On entry*: the value of tol is used to decide if the new independent variable is linearly related to independent variables already included in the model. If the new variable is linearly related then $c$ is not updated. The smaller the value of tol the stricter the criterion for deciding if there is a linear relationship.*Suggested value*: ${\mathbf{tol}}=0.000001$.*Constraint*: ${\mathbf{tol}}>0.0$.

- ifail
- Type: System..::..Int32%
*On exit*: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# Description

A linear regression model may be built up by adding new independent variables to an existing model. g02de updates the $QR$ decomposition used in the computation of the linear regression model. The $QR$ decomposition may come from g02da or a previous call to g02de. The general linear regression model is defined by

$$y=X\beta +\epsilon \text{,}$$ |

where | $y$ is a vector of $n$ observations on the dependent variable, |

$X$ is an $n$ by $p$ matrix of the independent variables of column rank $k$, | |

$\beta $ is a vector of length $p$ of unknown parameters, | |

and | $\epsilon $ is a vector of length $n$ of unknown random errors such that $\mathrm{var}\u200a\epsilon =V{\sigma}^{2}$, where $V$ is a known diagonal matrix. |

If $V=I$, the identity matrix, then least squares estimation is used. If $V\ne I$, then for a given weight matrix $W\propto {V}^{-1}$, weighted least squares estimation is used.

The least squares estimates, $\hat{\beta}$ of the parameters $\beta $ minimize ${\left(y-X\beta \right)}^{\mathrm{T}}\left(y-X\beta \right)$ while the weighted least squares estimates, minimize ${\left(y-X\beta \right)}^{\mathrm{T}}W\left(y-X\beta \right)$.

The parameter estimates may be found by computing a $QR$ decomposition of $X$ (or ${W}^{\frac{1}{2}}X$ in the weighted case), i.e.,

where ${R}^{*}=\left(\begin{array}{l}R\\ 0\end{array}\right)$ and $R$ is a $p$ by $p$ upper triangular matrix and $Q$ is an $n$ by $n$ orthogonal matrix.

$$X=Q{R}^{*}\text{\hspace{1em}}\left(\text{or \hspace{1em}}{W}^{\frac{1}{2}}X=Q{R}^{*}\right)\text{,}$$ |

If $R$ is of full rank, then $\hat{\beta}$ is the solution to

where $c={Q}^{\mathrm{T}}y$ (or ${Q}^{\mathrm{T}}{W}^{\frac{1}{2}}y$) and ${c}_{1}$ is the first $p$ elements of $c$.

$$R\hat{\beta}={c}_{1}\text{,}$$ |

If $R$ is not of full rank a solution is obtained by means of a singular value decomposition (SVD) of $R$.

To add a new independent variable, ${x}_{p+1}$, $R$ and $c$ have to be updated. The matrix ${Q}_{p+1}$ is found such that ${Q}_{p+1}^{\mathrm{T}}\left[R:{Q}^{\mathrm{T}}{x}_{p+1}\right]$ (or ${Q}_{p+1}^{\mathrm{T}}\left[R:{Q}^{\mathrm{T}}{W}^{\frac{1}{2}}{x}_{p+1}\right]$) is upper triangular. The vector $c$ is then updated by multiplying by ${Q}_{p+1}^{\mathrm{T}}$.

The new independent variable is tested to see if it is linearly related to the existing independent variables by checking that at least one of the values ${\left({Q}^{\mathrm{T}}{x}_{p+1}\right)}_{\mathit{i}}$, for $\mathit{i}=p+2,\dots ,n$, is nonzero.

The new parameter estimates, $\hat{\beta}$, can then be obtained by a call to g02dd.

The method can be used with $p=0$, in which case $R$ and $c$ are initialized.

# References

Draper N R and Smith H (1985)

*Applied Regression Analysis*(2nd Edition) WileyGolub G H and Van Loan C F (1996)

*Matrix Computations*(3rd Edition) Johns Hopkins University Press, BaltimoreHammarling S (1985) The singular value decomposition in multivariate statistics

*SIGNUM Newsl.***20(3)**2–25McCullagh P and Nelder J A (1983)

*Generalized Linear Models*Chapman and HallSearle S R (1971)

*Linear Models*Wiley# Error Indicators and Warnings

**Note:**g02de may return useful information for one or more of the following detected errors or warnings.

Errors or warnings detected by the method:

Some error messages may refer to parameters that are dropped from this interface
(LDQ) In these
cases, an error in another parameter has usually caused an incorrect value to be inferred.

- ${\mathbf{ifail}}=1$
On entry, ${\mathbf{n}}<1$, or ${\mathbf{ip}}<0$, or ${\mathbf{ip}}\ge {\mathbf{n}}$, or ${\mathbf{tol}}\le 0.0$, or ${\mathbf{weight}}\ne \text{"U"}$ or $\text{"W"}$.

- ${\mathbf{ifail}}=3$
- The new independent variable is a linear combination of existing variables. The $\left({\mathbf{ip}}+2\right)$th column of q will therefore be null.

- ${\mathbf{ifail}}=-9000$
- An error occured, see message report.
- ${\mathbf{ifail}}=-6000$
- Invalid Parameters $\u2329\mathit{\text{value}}\u232a$
- ${\mathbf{ifail}}=-4000$
- Invalid dimension for array $\u2329\mathit{\text{value}}\u232a$
- ${\mathbf{ifail}}=-8000$
- Negative dimension for array $\u2329\mathit{\text{value}}\u232a$
- ${\mathbf{ifail}}=-6000$
- Invalid Parameters $\u2329\mathit{\text{value}}\u232a$

# Accuracy

The accuracy is closely related to the accuracy of f08ag which should be consulted for further details.

# Parallelism and Performance

None.