Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

## Purpose

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

## Syntax

[q, p, rss, ifail] = g02de(ip, q, p, x, 'n', n, 'wt', wt, 'tol', tol)
[q, p, rss, ifail] = nag_correg_linregm_var_add(ip, q, p, x, 'n', n, 'wt', wt, 'tol', tol)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 23: weight dropped from interface, wt now optional
.

## Description

A linear regression model may be built up by adding new independent variables to an existing model. nag_correg_linregm_var_add (g02de) updates the QR$QR$ decomposition used in the computation of the linear regression model. The QR$QR$ decomposition may come from nag_correg_linregm_fit (g02da) or a previous call to nag_correg_linregm_var_add (g02de). The general linear regression model is defined by
 y = Xβ + ε, $y=Xβ+ε,$
 where y$y$ is a vector of n$n$ observations on the dependent variable, X$X$ is an n$n$ by p$p$ matrix of the independent variables of column rank k$k$, β$\beta$ is a vector of length p$p$ of unknown parameters, and ε$\epsilon$ is a vector of length n$n$ of unknown random errors such that varε = Vσ2$\mathrm{var}\epsilon =V{\sigma }^{2}$, where V$V$ is a known diagonal matrix.
If V = I$V=I$, the identity matrix, then least squares estimation is used. If VI$V\ne I$, then for a given weight matrix WV1$W\propto {V}^{-1}$, weighted least squares estimation is used.
The least squares estimates, β̂$\stackrel{^}{\beta }$ of the parameters β$\beta$ minimize (yXβ)T (yXβ) ${\left(y-X\beta \right)}^{\mathrm{T}}\left(y-X\beta \right)$ while the weighted least squares estimates, minimize (yXβ)T W (yXβ) ${\left(y-X\beta \right)}^{\mathrm{T}}W\left(y-X\beta \right)$.
The parameter estimates may be found by computing a QR$QR$ decomposition of X$X$ (or W(1/2)X${W}^{\frac{1}{2}}X$ in the weighted case), i.e.,
 X = QR* (or  W(1/2)X = QR*), $X=QR* (or W12X=QR*),$
where R* =
 ( R ) 0
${R}^{*}=\left(\begin{array}{l}R\\ 0\end{array}\right)$ and R$R$ is a p$p$ by p$p$ upper triangular matrix and Q$Q$ is an n$n$ by n$n$ orthogonal matrix.
If R$R$ is of full rank, then β̂$\stackrel{^}{\beta }$ is the solution to
 Rβ̂ = c1, $Rβ^=c1,$
where c = QTy$c={Q}^{\mathrm{T}}y$ (or QTW(1/2)y${Q}^{\mathrm{T}}{W}^{\frac{1}{2}}y$) and c1${c}_{1}$ is the first p$p$ elements of c$c$.
If R$R$ is not of full rank a solution is obtained by means of a singular value decomposition (SVD) of R$R$.
To add a new independent variable, xp + 1${x}_{p+1}$, R$R$ and c$c$ have to be updated. The matrix Qp + 1${Q}_{p+1}$ is found such that Qp + 1T [R : QTxp + 1]${Q}_{p+1}^{\mathrm{T}}\left[R:{Q}^{\mathrm{T}}{x}_{p+1}\right]$ (or Qp + 1T [R : QTW(1/2)xp + 1]${Q}_{p+1}^{\mathrm{T}}\left[R:{Q}^{\mathrm{T}}{W}^{\frac{1}{2}}{x}_{p+1}\right]$) is upper triangular. The vector c$c$ is then updated by multiplying by Qp + 1T ${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 (QTxp + 1)i${\left({Q}^{\mathrm{T}}{x}_{p+1}\right)}_{\mathit{i}}$, for i = p + 2,,n$\mathit{i}=p+2,\dots ,n$, is nonzero.
The new parameter estimates, β̂$\stackrel{^}{\beta }$, can then be obtained by a call to nag_correg_linregm_update (g02dd).
The function can be used with p = 0$p=0$, in which case R$R$ and c$c$ are initialized.

## References

Draper N R and Smith H (1985) Applied Regression Analysis (2nd Edition) Wiley
Golub G H and Van Loan C F (1996) Matrix Computations (3rd Edition) Johns Hopkins University Press, Baltimore
Hammarling S (1985) The singular value decomposition in multivariate statistics SIGNUM Newsl. 20(3) 2–25
McCullagh P and Nelder J A (1983) Generalized Linear Models Chapman and Hall
Searle S R (1971) Linear Models Wiley

## Parameters

### Compulsory Input Parameters

1:     ip – int64int32nag_int scalar
p$p$, the number of independent variables already in the model.
Constraint: ip0${\mathbf{ip}}\ge 0$ and ip < n${\mathbf{ip}}<{\mathbf{n}}$.
2:     q(ldq,ip + 2${\mathbf{ip}}+2$) – double array
ldq, the first dimension of the array, must satisfy the constraint ldqn$\mathit{ldq}\ge {\mathbf{n}}$.
If ip0${\mathbf{ip}}\ne 0$, q must contain the results of the QR$QR$ decomposition for the model with p$p$ parameters as returned by nag_correg_linregm_fit (g02da) or a previous call to nag_correg_linregm_var_add (g02de).
If ip = 0${\mathbf{ip}}=0$, the first column of q should contain the n$n$ values of the dependent variable, y$y$.
3:     p(ip + 1${\mathbf{ip}}+1$) – double array
Contains further details of the QR$QR$ decomposition used. The first ip elements of p must contain the zeta values for the QR$QR$ decomposition (see nag_lapack_dgeqrf (f08ae) for details).
The first ip elements of array p are provided by nag_correg_linregm_fit (g02da) or by previous calls to nag_correg_linregm_var_add (g02de).
4:     x(n) – double array
n, the dimension of the array, must satisfy the constraint n1${\mathbf{n}}\ge 1$.
x$x$, the new independent variable.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the array x and the first dimension of the array q. (An error is raised if these dimensions are not equal.)
n$n$, the number of observations.
Constraint: n1${\mathbf{n}}\ge 1$.
2:     wt( : $:$) – double array
Note: the dimension of the array wt must be at least n${\mathbf{n}}$ if weight = 'W'$\mathit{weight}=\text{'W'}$, and at least 1$1$ otherwise.
If provided, wt must contain the weights to be used.
If wt(i) = 0.0${\mathbf{wt}}\left(i\right)=0.0$, the i$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 wt is not provided the effective number of observations is n$n$.
Constraint: if weight = 'W'$\mathit{weight}=\text{'W'}$, wt(i)0.0${\mathbf{wt}}\left(\mathit{i}\right)\ge 0.0$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
3:     tol – double scalar
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$c$ is not updated. The smaller the value of tol the stricter the criterion for deciding if there is a linear relationship.
Default: 0.000001$0.000001$
Constraint: tol > 0.0${\mathbf{tol}}>0.0$.

weight ldq

### Output Parameters

1:     q(ldq,ip + 2${\mathbf{ip}}+2$) – double array
ldqn$\mathit{ldq}\ge {\mathbf{n}}$.
The results of the QR$QR$ decomposition for the model with p + 1$p+1$ parameters:
• the first column of q contains the updated value of c$c$;
• the columns 2$2$ to ip + 1${\mathbf{ip}}+1$ are unchanged;
• the first ip + 1${\mathbf{ip}}+1$ elements of column ip + 2${\mathbf{ip}}+2$ contain the new column of R$R$, while the remaining nip1${\mathbf{n}}-{\mathbf{ip}}-1$ elements contain details of the matrix Qp + 1${Q}_{p+1}$.
2:     p(ip + 1${\mathbf{ip}}+1$) – double array
The first ip elements of p are unchanged and the (ip + 1)$\left({\mathbf{ip}}+1\right)$th element contains the zeta value for Qp + 1${Q}_{p+1}$.
The residual sum of squares for the new fitted model.
Note:  this will only be valid if the model is of full rank, see Section [Further Comments].
4:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Note: nag_correg_linregm_var_add (g02de) may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

ifail = 1${\mathbf{ifail}}=1$
 On entry, n < 1${\mathbf{n}}<1$, or ip < 0${\mathbf{ip}}<0$, or ip ≥ n${\mathbf{ip}}\ge {\mathbf{n}}$, or ldq < n$\mathit{ldq}<{\mathbf{n}}$, or tol ≤ 0.0${\mathbf{tol}}\le 0.0$, or weight ≠ 'U'$\mathit{weight}\ne \text{'U'}$ or 'W'$\text{'W'}$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, weight = 'W'$\mathit{weight}=\text{'W'}$ and a value of wt < 0.0${\mathbf{wt}}<0.0$.
W ifail = 3${\mathbf{ifail}}=3$
The new independent variable is a linear combination of existing variables. The (ip + 2)$\left({\mathbf{ip}}+2\right)$th column of q will therefore be null.

## Accuracy

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

It should be noted that the residual sum of squares produced by nag_correg_linregm_var_add (g02de) may not be correct if the model to which the new independent variable is added is not of full rank. In such a case nag_correg_linregm_update (g02dd) should be used to calculate the residual sum of squares.

## Example

```function nag_correg_linregm_var_add_example
ip = int64(0);
q = [4.32, 0;
5.21, 0;
6.49, 0;
7.1,  0;
7.94, 0;
8.53, 0;
8.84, 0;
9.02, 0;
9.27, 0;
9.43, 0;
9.68, 0;
9.83, 0];
p = [0];
x = [1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1];
```
```

qOut =

-27.6147   -3.4641
-1.9437    0.2543
-0.6637    0.2543
-0.0537    0.2543
0.7863    0.2543
1.3763    0.2543
1.6863    0.2543
1.8663    0.2543
2.1163    0.2543
2.2763    0.2543
2.5263    0.2543
2.6763    0.2543

pOut =

1.1352

36.2666

ifail =

0

```
```function g02de_example
ip = int64(0);
q = [4.32, 0;
5.21, 0;
6.49, 0;
7.1,  0;
7.94, 0;
8.53, 0;
8.84, 0;
9.02, 0;
9.27, 0;
9.43, 0;
9.68, 0;
9.83, 0];
p = [0];
x = [1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1];
[qOut, pOut, rss, ifail] = g02de(ip, q, p, x)
```
```

qOut =

-27.6147   -3.4641
-1.9437    0.2543
-0.6637    0.2543
-0.0537    0.2543
0.7863    0.2543
1.3763    0.2543
1.6863    0.2543
1.8663    0.2543
2.1163    0.2543
2.2763    0.2543
2.5263    0.2543
2.6763    0.2543

pOut =

1.1352

36.2666

ifail =

0

```