G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG02EAF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G02EAF calculates the residual sums of squares for all possible linear regressions for a given set of independent variables.

## 2  Specification

 SUBROUTINE G02EAF ( MEAN, WEIGHT, N, M, X, LDX, VNAME, ISX, Y, WT, NMOD, MODL, LDMODL, RSS, NTERMS, MRANK, WK, IFAIL)
 INTEGER N, M, LDX, ISX(M), NMOD, LDMODL, NTERMS(LDMODL), MRANK(LDMODL), IFAIL REAL (KIND=nag_wp) X(LDX,M), Y(N), WT(*), RSS(LDMODL), WK(N*(M+1)) CHARACTER(*) VNAME(M), MODL(LDMODL,M) CHARACTER(1) MEAN, WEIGHT

## 3  Description

For a set of $\mathit{k}$ possible independent variables there are ${2}^{\mathit{k}}$ linear regression models with from zero to $\mathit{k}$ independent variables in each model. For example if $\mathit{k}=3$ and the variables are $A$, $B$ and $C$ then the possible models are:
 (i) null model (ii) $A$ (iii) $B$ (iv) $C$ (v) $A$ and $B$ (vi) $A$ and $C$ (vii) $B$ and $C$ (viii) $A$, $B$ and $C$.
G02EAF calculates the residual sums of squares from each of the ${2}^{\mathit{k}}$ possible models. The method used involves a $QR$ decomposition of the matrix of possible independent variables. Independent variables are then moved into and out of the model by a series of Givens rotations and the residual sums of squares computed for each model; see Clark (1981) and Smith and Bremner (1989).
The computed residual sums of squares are then ordered first by increasing number of terms in the model, then by decreasing size of residual sums of squares. So the first model will always have the largest residual sum of squares and the ${2}^{\mathit{k}}$th will always have the smallest. This aids you in selecting the best possible model from the given set of independent variables.
G02EAF allows you to specify some independent variables that must be in the model, the forced variables. The other independent variables from which the possible models are to be formed are the free variables.

## 4  References

Clark M R B (1981) A Givens algorithm for moving from one linear model to another without going back to the data Appl. Statist. 30 198–203
Smith D M and Bremner J M (1989) All possible subset regressions using the $QR$ decomposition Comput. Statist. Data Anal. 7 217–236
Weisberg S (1985) Applied Linear Regression Wiley

## 5  Parameters

1:     MEAN – CHARACTER(1)Input
On entry: indicates if a mean term is to be included.
${\mathbf{MEAN}}=\text{'M'}$
A mean term, intercept, will be included in the model.
${\mathbf{MEAN}}=\text{'Z'}$
The model will pass through the origin, zero-point.
Constraint: ${\mathbf{MEAN}}=\text{'M'}$ or $\text{'Z'}$.
2:     WEIGHT – CHARACTER(1)Input
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'}$.
3:     N – INTEGERInput
On entry: $n$, the number of observations.
Constraints:
• ${\mathbf{N}}\ge 2$;
• ${\mathbf{N}}\ge m$, is the number of independent variables to be considered (forced plus free plus mean if included), as specified by MEAN and ISX.
4:     M – INTEGERInput
On entry: the number of variables contained in X.
Constraint: ${\mathbf{M}}\ge 2$.
5:     X(LDX,M) – REAL (KIND=nag_wp) arrayInput
On entry: ${\mathbf{X}}\left(\mathit{i},\mathit{j}\right)$ must contain the $\mathit{i}$th observation for the $\mathit{j}$th independent variable, for $\mathit{i}=1,2,\dots ,{\mathbf{N}}$ and $\mathit{j}=1,2,\dots ,{\mathbf{M}}$.
6:     LDX – INTEGERInput
On entry: the first dimension of the array X as declared in the (sub)program from which G02EAF is called.
Constraint: ${\mathbf{LDX}}\ge {\mathbf{N}}$.
7:     VNAME(M) – CHARACTER(*) arrayInput
On entry: ${\mathbf{VNAME}}\left(\mathit{j}\right)$ must contain the name of the variable in column $\mathit{j}$ of X, for $\mathit{j}=1,2,\dots ,{\mathbf{M}}$.
8:     ISX(M) – INTEGER arrayInput
On entry: indicates which independent variables are to be considered in the model.
${\mathbf{ISX}}\left(j\right)\ge 2$
The variable contained in the $j$th column of X is included in all regression models, i.e., is a forced variable.
${\mathbf{ISX}}\left(j\right)=1$
The variable contained in the $j$th column of X is included in the set from which the regression models are chosen, i.e., is a free variable.
${\mathbf{ISX}}\left(j\right)=0$
The variable contained in the $j$th column of X is not included in the models.
Constraints:
• ${\mathbf{ISX}}\left(\mathit{j}\right)\ge 0$, for $\mathit{j}=1,2,\dots ,{\mathbf{M}}$;
• at least one value of ${\mathbf{ISX}}=1$.
9:     Y(N) – REAL (KIND=nag_wp) arrayInput
On entry: ${\mathbf{Y}}\left(\mathit{i}\right)$ must contain the $\mathit{i}$th observation on the dependent variable, ${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
10:   WT($*$) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array WT must be at least ${\mathbf{N}}$ if ${\mathbf{WEIGHT}}=\text{'W'}$.
On entry: if ${\mathbf{WEIGHT}}=\text{'W'}$, WT must contain the weights to be used in the weighted regression.
If ${\mathbf{WT}}\left(i\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}\right)\ge 0.0$, for $\mathit{i}=1,2,\dots ,n$.
11:   NMOD – INTEGEROutput
On exit: the total number of models for which residual sums of squares have been calculated.
12:   MODL(LDMODL,M) – CHARACTER(*) arrayOutput
On exit: the first ${\mathbf{NTERMS}}\left(i\right)$ elements of the $i$th row of MODL contain the names of the independent variables, as given in VNAME, that are included in the $i$th model.
Constraint: the length of MODL should be greater or equal to the length of VNAME.
13:   LDMODL – INTEGERInput
On entry: the first dimension of the array MODL and the dimension of the arrays RSS, NTERMS and MRANK as declared in the (sub)program from which G02EAF is called.
Constraints:
• ${\mathbf{LDMODL}}\ge {\mathbf{M}}$;
• ${\mathbf{LDMODL}}\ge {2}^{\mathit{k}}$, $\mathit{k}$ is the number of free variables in the model as specified in ISX, and hence ${2}^{\mathit{k}}$ is the total number of models to be generated.
14:   RSS(LDMODL) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{RSS}}\left(\mathit{i}\right)$ contains the residual sum of squares for the $\mathit{i}$th model, for $\mathit{i}=1,2,\dots ,{\mathbf{NMOD}}$.
15:   NTERMS(LDMODL) – INTEGER arrayOutput
On exit: ${\mathbf{NTERMS}}\left(\mathit{i}\right)$ contains the number of independent variables in the $\mathit{i}$th model, not including the mean if one is fitted, for $\mathit{i}=1,2,\dots ,{\mathbf{NMOD}}$.
16:   MRANK(LDMODL) – INTEGER arrayOutput
On exit: ${\mathbf{MRANK}}\left(i\right)$ contains the rank of the residual sum of squares for the $i$th model.
17:   WK(${\mathbf{N}}×\left({\mathbf{M}}+1\right)$) – REAL (KIND=nag_wp) arrayWorkspace
18:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{N}}<2$, or ${\mathbf{M}}<2$, or ${\mathbf{LDX}}<{\mathbf{N}}$, or ${\mathbf{LDMODL}}<{\mathbf{M}}$, or ${\mathbf{MEAN}}\ne \text{'M'}$ or $\text{'Z'}$, or ${\mathbf{WEIGHT}}\ne \text{'U'}$ or $\text{'W'}$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{WEIGHT}}=\text{'W'}$ and a value of ${\mathbf{WT}}<0.0$.
${\mathbf{IFAIL}}=3$
 On entry, a value of ${\mathbf{ISX}}<0$, or there are no free variables, i.e., no element of ${\mathbf{ISX}}=1$.
${\mathbf{IFAIL}}=4$
On entry, ${\mathbf{LDMODL}}<\text{}$ the number of possible models $\text{}={2}^{\mathit{k}}$, where $\mathit{k}$ is the number of free independent variables from ISX.
${\mathbf{IFAIL}}=5$
On entry, the number of independent variables to be considered (forced plus free plus mean if included) is greater or equal to the effective number of observations.
${\mathbf{IFAIL}}=6$
The full model is not of full rank, i.e., some of the independent variables may be linear combinations of other independent variables. Variables must be excluded from the model in order to give full rank.

## 7  Accuracy

For a discussion of the improved accuracy obtained by using a method based on the $QR$ decomposition see Smith and Bremner (1989).

## 8  Further Comments

G02ECF may be used to compute ${R}^{2}$ and ${C}_{p}$-values from the results of G02EAF.
If a mean has been included in the model and no variables are forced in then ${\mathbf{RSS}}\left(1\right)$ contains the total sum of squares and in many situations a reasonable estimate of the variance of the errors is given by ${\mathbf{RSS}}\left({\mathbf{NMOD}}\right)/\left({\mathbf{N}}-1-{\mathbf{NTERMS}}\left({\mathbf{NMOD}}\right)\right)$.

## 9  Example

The data for this example is given in Weisberg (1985). The independent variables and the dependent variable are read, as are the names of the variables. These names are as given in Weisberg (1985). The residual sums of squares computed and printed with the names of the variables in the model.

### 9.1  Program Text

Program Text (g02eafe.f90)

### 9.2  Program Data

Program Data (g02eafe.d)

### 9.3  Program Results

Program Results (g02eafe.r)