g02 Chapter Contents
g02 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_regsn_mult_linear_add_var (g02dec)

## 1  Purpose

nag_regsn_mult_linear_add_var (g02dec) adds a new independent variable to a general linear regression model.

## 2  Specification

 #include #include
 void nag_regsn_mult_linear_add_var (Integer n, Integer ip, double q[], Integer tdq, double p[], const double wt[], const double x[], double *rss, double tol, NagError *fail)

## 3  Description

A linear regression model may be built up by adding new independent variables to an existing model. nag_regsn_mult_linear_add_var (g02dec) updates the $QR$ decomposition used in the computation of the linear regression model. The $QR$ decomposition may come from nag_regsn_mult_linear (g02dac) or a previous call to nag_regsn_mult_linear_add_var (g02dec). The general linear regression model is defined by:
 $y = X β + ε$
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 arguments, and $\epsilon$ is a vector of length $n$ of unknown random errors such that var $\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, $\stackrel{^}{\beta }$ of the arguments $\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.,
 $X = QR * or ​ W 1 2 X = QR *$
where ${R}^{*}=\left(\begin{array}{c}R\\ 0\end{array}\right)$ and $R$ is a $p$ by $p$ upper triangular matrix and $Q$ is an $n$ by $n$ orthogonal matrix. If $R$ is of full rank, then $\stackrel{^}{\beta }$ is the solution to:
 $R β ^ = c 1$
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$.
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)}_{i}$, for $i=p+2,p+3,\dots ,n$ is nonzero.
The new parameter estimates, $\stackrel{^}{\beta }$, can then be obtained by a call to nag_regsn_mult_linear_upd_model (g02ddc).
The function can be used with $p=0$, in which case $R$ and $c$ are initialized.

## 4  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

## 5  Arguments

1:     nIntegerInput
On entry: the number of observations, $n$.
Constraint: ${\mathbf{n}}\ge 1$.
2:     ipIntegerInput
On entry: the number of independent variables already in the model, $p$.
Constraint: ${\mathbf{ip}}\ge 0$ and ${\mathbf{ip}}<{\mathbf{n}}$.
3:     q[${\mathbf{n}}×{\mathbf{tdq}}$]doubleInput/Output
Note: the $\left(i,j\right)$th element of the matrix $Q$ is stored in ${\mathbf{q}}\left[\left(i-1\right)×{\mathbf{tdq}}+j-1\right]$.
On entry: if ${\mathbf{ip}}\ne 0$, then q must contain the results of the $QR$ decomposition for the model with $p$ arguments as returned by nag_regsn_mult_linear (g02dac) or a previous call to nag_regsn_mult_linear_add_var (g02dec).
If ${\mathbf{ip}}=0$, then 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$ arguments: 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}$.
4:     tdqIntegerInput
On entry: the stride separating matrix column elements in the array q.
Constraint: ${\mathbf{tdq}}\ge {\mathbf{ip}}+2$.
5:     p[${\mathbf{ip}}+1$]doubleInput/Output
On entry: p contains further details of the $QR$ decomposition used. The first ip elements of p must contain details of the Householder vector from the $QR$ decomposition. The first ip elements of array p are provided by nag_regsn_mult_linear (g02dac) or by previous calls to nag_regsn_mult_linear_add_var (g02dec).
On exit: the first ip elements of p are unchanged and the (${\mathbf{ip}}+1$)th element contains details of the Householder vector related to the new independent variable.
6:     wt[n]const doubleInput
On entry: if weighted estimates are required, then wt must contain the weights to be used. Otherwise wt need not be defined and may be set to the null pointer NULL, i.e., (double *)0.
If ${\mathbf{wt}}\left[i-1\right]=0.0$, then 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 wt is NULL, then the effective number of observations is $n$.
Constraint:  or ${\mathbf{wt}}\left[\mathit{i}-1\right]\ge 0.0$, for $\mathit{i}=1,2,\dots ,n$.
7:     x[n]const doubleInput
On entry: the new independent variable, $x$.
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 Section 8.
9:     toldoubleInput
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$.
10:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_2_INT_ARG_GE
On entry, ${\mathbf{ip}}=〈\mathit{\text{value}}〉$ while ${\mathbf{n}}=〈\mathit{\text{value}}〉$. These arguments must satisfy ${\mathbf{ip}}<{\mathbf{n}}$.
NE_2_INT_ARG_LT
On entry, ${\mathbf{tdq}}=〈\mathit{\text{value}}〉$ while ${\mathbf{ip}}+2=〈\mathit{\text{value}}〉$. These arguments must satisfy ${\mathbf{tdq}}\ge {\mathbf{ip}}+2$.
NE_INT_ARG_LT
On entry, ${\mathbf{ip}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ip}}\ge 0$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 1$.
NE_NVAR_NOT_IND
The new independent variable is a linear combination of existing variables. The $\left({\mathbf{ip}}+1\right)$th column of q is, therefore, NULL.
NE_REAL_ARG_LE
On entry, tol must not be less than or equal to 0.0: ${\mathbf{tol}}=〈\mathit{\text{value}}〉$.
NE_REAL_ARG_LT
On entry, ${\mathbf{wt}}\left[〈\mathit{\text{value}}〉\right]$ must not be less than 0.0: ${\mathbf{wt}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$.

## 7  Accuracy

The accuracy is closely related to the accuracy of the QR decomposition.

It should be noted that the residual sum of squares produced by nag_regsn_mult_linear_add_var (g02dec) 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_regsn_mult_linear_upd_model (g02ddc) should be used to calculate the residual sum of squares.

## 9  Example

A dataset consisting of 12 observations is read in. The four independent variables are stored in the array x while the dependent variable is read into the first column of q. If the character variable meanc indicates that a mean should be included in the model, a variable taking the value 1.0 for all observations is set up and fitted. Subsequently, one variable at a time is selected to enter the model as indicated by the input value of indx. After the variable has been added the parameter estimates are calculated by nag_regsn_mult_linear_upd_model (g02ddc) and the results printed. This is repeated until the input value of indx is 0.

### 9.1  Program Text

Program Text (g02dece.c)

### 9.2  Program Data

Program Data (g02dece.d)

### 9.3  Program Results

Program Results (g02dece.r)