NAG Library Routine Document
G02EFF
1 Purpose
G02EFF calculates a full stepwise selection from $p$ variables by using Clarke's sweep algorithm on the correlation matrix of a design and data matrix, $Z$. The (weighted) variancecovariance, (weighted) means and sum of weights of $Z$ must be supplied.
2 Specification
SUBROUTINE G02EFF ( 
M, N, WMEAN, C, SW, ISX, FIN, FOUT, TAU, B, SE, RSQ, RMS, DF, MONLEV, MONFUN, IUSER, RUSER, IFAIL) 
INTEGER 
M, N, ISX(M), DF, MONLEV, IUSER(*), IFAIL 
REAL (KIND=nag_wp) 
WMEAN(M+1), C((M+1)*(M+2)/2), SW, FIN, FOUT, TAU, B(M+1), SE(M+1), RSQ, RMS, RUSER(*) 
EXTERNAL 
MONFUN 

3 Description
The general multiple linear regression model is defined by
where
 $y$ is a vector of $n$ observations on the dependent variable,
 ${\beta}_{0}$ is an intercept coefficient,
 $X$ is an $n$ by $p$ matrix of $p$ explanatory variables,
 $\beta $ is a vector of $p$ unknown coefficients, and
 $\epsilon $ is a vector of length $n$ of unknown, Normally distributed, random errors.
G02EFF employs a full stepwise regression to select a subset of explanatory variables from the $p$ available variables (the intercept is included in the model) and computes regression coefficients and their standard errors, and various other statistical quantities, by minimizing the sum of squares of residuals. The method applies repeatedly a forward selection step followed by a backward elimination step and halts when neither step updates the current model.
The criterion used to update a current model is the variance ratio of residual sum of squares. Let
${s}_{1}$ and
${s}_{2}$ be the residual sum of squares of the current model and this model after undergoing a single update, with degrees of freedom
${q}_{1}$ and
${q}_{2}$, respectively. Then the condition:
must be satisfied if a variable
$k$ will be considered for entry to the current model, and the condition:
must be satisfied if a variable
$k$ will be considered for removal from the current model, where
${f}_{1}$ and
${f}_{2}$ are usersupplied values and
${f}_{2}\le {f}_{1}$.
In the entry step the entry statistic is computed for each variable not in the current model. If no variable is associated with a test value that exceeds ${f}_{1}$ then this step is terminated; otherwise the variable associated with the largest value for the entry statistic is entered into the model.
In the removal step the removal statistic is computed for each variable in the current model. If no variable is associated with a test value less than ${f}_{2}$ then this step is terminated; otherwise the variable associated with the smallest value for the removal statistic is removed from the model.
The data values $X$ and $y$ are not provided as input to the routine. Instead, summary statistics of the design and data matrix $Z=\left(X\mid y\right)$ are required.
Explanatory variables are entered into and removed from the current model by using sweep operations on the correlation matrix
$R$ of
$Z$, given by:
where
${r}_{\mathit{i}\mathit{j}}$ is the correlation between the explanatory variables
$\mathit{i}$ and
$\mathit{j}$, for
$\mathit{i}=1,2,\dots ,p$ and
$\mathit{j}=1,2,\dots ,p$, and
${r}_{yi}$ (and
${r}_{iy}$) is the correlation between the response variable
$y$ and the
$\mathit{i}$th explanatory variable, for
$\mathit{i}=1,2,\dots ,p$.
A sweep operation on the
$k$th row and column (
$k\le p$) of
$R$ replaces:
The
$k$th explanatory variable is eligible for entry into the current model if it satisfies the collinearity tests:
${r}_{kk}>\tau $ and
for a usersupplied value (
$>0$) of
$\tau $ and where the index
$i$ runs over explanatory variables in the current model. The sweep operation is its own inverse, therefore pivoting on an explanatory variable
$k$ in the current model has the effect of removing it from the model.
Once the stepwise model selection procedure is finished, the routine calculates:
(a) 
the least squares estimate for the $i$th explanatory variable included in the fitted model; 
(b) 
standard error estimates for each coefficient in the final model; 
(c) 
the square root of the mean square of residuals and its degrees of freedom; 
(d) 
the multiple correlation coefficient. 
The routine makes use of the symmetry of the sweep operations and correlation matrix which reduces by almost one half the storage and computation required by the sweep algorithm, see
Clarke (1981) for details.
4 References
Clarke M R B (1981) Algorithm AS 178: the Gauss–Jordan sweep operator with detection of collinearity Appl. Statist. 31 166–169
Dempster A P (1969) Elements of Continuous Multivariate Analysis Addison–Wesley
Draper N R and Smith H (1985) Applied Regression Analysis (2nd Edition) Wiley
5 Arguments
 1: $\mathrm{M}$ – INTEGERInput

On entry: the number of explanatory variables available in the design matrix, $Z$.
Constraint:
${\mathbf{M}}>1$.
 2: $\mathrm{N}$ – INTEGERInput

On entry: the number of observations used in the calculations.
Constraint:
${\mathbf{N}}>1$.
 3: $\mathrm{WMEAN}\left({\mathbf{M}}+1\right)$ – REAL (KIND=nag_wp) arrayInput

On entry: the mean of the design matrix, $Z$.
 4: $\mathrm{C}\left(\left({\mathbf{M}}+1\right)\times \left({\mathbf{M}}+2\right)/2\right)$ – REAL (KIND=nag_wp) arrayInput

On entry: the uppertriangular variancecovariance matrix packed by column for the design matrix,
$Z$. Because the routine computes the correlation matrix
$R$ from
C, the variancecovariance matrix need only be supplied up to a scaling factor.
 5: $\mathrm{SW}$ – REAL (KIND=nag_wp)Input

On entry: if weights were used to calculate
C then
SW is the sum of positive weight values; otherwise
SW is the number of observations used to calculate
C.
Constraint:
${\mathbf{SW}}>1.0$.
 6: $\mathrm{ISX}\left({\mathbf{M}}\right)$ – INTEGER arrayInput/Output

On entry: the value of
${\mathbf{ISX}}\left(\mathit{j}\right)$ determines the set of variables used to perform full stepwise model selection, for
$\mathit{j}=1,2,\dots ,{\mathbf{M}}$.
 ${\mathbf{ISX}}\left(\mathit{j}\right)=1$
 To exclude the variable corresponding to the $j$th column of $X$ from the final model.
 ${\mathbf{ISX}}\left(\mathit{j}\right)=1$
 To consider the variable corresponding to the $j$th column of $X$ for selection in the final model.
 ${\mathbf{ISX}}\left(\mathit{j}\right)=2$
 To force the inclusion of the variable corresponding to the $j$th column of $X$ in the final model.
Constraint:
${\mathbf{ISX}}\left(\mathit{j}\right)=1,1\text{ or}2$, for $\mathit{j}=1,2,\dots ,{\mathbf{M}}$.
On exit: the value of
${\mathbf{ISX}}\left(\mathit{j}\right)$ indicates the status of the
$j$th explanatory variable in the model.
 ${\mathbf{ISX}}\left(\mathit{j}\right)=1$
 Forced exclusion.
 ${\mathbf{ISX}}\left(\mathit{j}\right)=0$
 Excluded.
 ${\mathbf{ISX}}\left(\mathit{j}\right)=1$
 Selected.
 ${\mathbf{ISX}}\left(\mathit{j}\right)=2$
 Forced selection.
 7: $\mathrm{FIN}$ – REAL (KIND=nag_wp)Input

On entry: the value of the variance ratio which an explanatory variable must exceed to be included in a model.
Suggested value:
${\mathbf{FIN}}=4.0$.
Constraint:
${\mathbf{FIN}}>0.0$.
 8: $\mathrm{FOUT}$ – REAL (KIND=nag_wp)Input

On entry: the explanatory variable in a model with the lowest variance ratio value is removed from the model if its value is less than
FOUT.
FOUT is usually set equal to the value of
FIN; a value less than
FIN is occasionally preferred.
Suggested value:
${\mathbf{FOUT}}={\mathbf{FIN}}$.
Constraint:
$0.0\le {\mathbf{FOUT}}\le {\mathbf{FIN}}$.
 9: $\mathrm{TAU}$ – REAL (KIND=nag_wp)Input

On entry: the tolerance, $\tau $, for detecting collinearities between variables when adding or removing an explanatory variable from a model. Explanatory variables deemed to be collinear are excluded from the final model.
Suggested value:
${\mathbf{TAU}}=1.0\times {10}^{6}$.
Constraint:
${\mathbf{TAU}}>0.0$.
 10: $\mathrm{B}\left({\mathbf{M}}+1\right)$ – REAL (KIND=nag_wp) arrayOutput

On exit: ${\mathbf{B}}\left(1\right)$ contains the estimate for the intercept term in the fitted model. If ${\mathbf{ISX}}\left(j\right)\ne 0$ then ${\mathbf{B}}\left(j+1\right)$ contains the estimate for the $j$th explanatory variable in the fitted model; otherwise ${\mathbf{B}}\left(j+1\right)=0$.
 11: $\mathrm{SE}\left({\mathbf{M}}+1\right)$ – REAL (KIND=nag_wp) arrayOutput

On exit: ${\mathbf{SE}}\left(\mathit{j}\right)$ contains the standard error for the estimate of ${\mathbf{B}}\left(\mathit{j}\right)$, for $\mathit{j}=1,2,\dots ,{\mathbf{M}}+1$.
 12: $\mathrm{RSQ}$ – REAL (KIND=nag_wp)Output

On exit: the ${R}^{2}$statistic for the fitted regression model.
 13: $\mathrm{RMS}$ – REAL (KIND=nag_wp)Output

On exit: the mean square of residuals for the fitted regression model.
 14: $\mathrm{DF}$ – INTEGEROutput

On exit: the number of degrees of freedom for the sum of squares of residuals.
 15: $\mathrm{MONLEV}$ – INTEGERInput

On entry: if a subroutine is provided by you to monitor the model selection process, set
MONLEV to
$1$; otherwise set
MONLEV to
$0$.
Constraint:
${\mathbf{MONLEV}}=0$ or $1$.
 16: $\mathrm{MONFUN}$ – SUBROUTINE, supplied by the NAG Library or the user.External Procedure

You may define your own function or specify the NAG defined default function G02EFH.
If
${\mathbf{MONLEV}}=0$,
MONFUN is not referenced; otherwise its specification is:
The specification of
MONFUN is:
INTEGER 
VAR, IUSER(*) 
REAL (KIND=nag_wp) 
VAL, RUSER(*) 
CHARACTER(1) 
FLAG 

 1: $\mathrm{FLAG}$ – CHARACTER(1)Input

On entry: the value of
FLAG indicates the stage of the stepwise selection of explanatory variables.
 ${\mathbf{FLAG}}=\text{'A'}$
 Variable VAR was added to the current model.
 ${\mathbf{FLAG}}=\text{'B'}$
 Beginning the backward elimination step.
 ${\mathbf{FLAG}}=\text{'C'}$
 Variable VAR failed the collinearity test and is excluded from the model.
 ${\mathbf{FLAG}}=\text{'D'}$
 Variable VAR was dropped from the current model.
 ${\mathbf{FLAG}}=\text{'F'}$
 Beginning the forward selection step
 ${\mathbf{FLAG}}=\text{'K'}$
 Backward elimination did not remove any variables from the current model.
 ${\mathbf{FLAG}}=\text{'S'}$
 Starting stepwise selection procedure.
 ${\mathbf{FLAG}}=\text{'V'}$
 The variance ratio for variable VAR takes the value VAL.
 ${\mathbf{FLAG}}=\text{'X'}$
 Finished stepwise selection procedure.
 2: $\mathrm{VAR}$ – INTEGERInput

On entry: the index of the explanatory variable in the design matrix
$Z$ to which
FLAG pertains.
 3: $\mathrm{VAL}$ – REAL (KIND=nag_wp)Input

On entry: if
${\mathbf{FLAG}}=\text{'V'}$,
VAL is the variance ratio value for the coefficient associated with explanatory variable index
VAR.
 4: $\mathrm{IUSER}\left(*\right)$ – INTEGER arrayUser Workspace
 5: $\mathrm{RUSER}\left(*\right)$ – REAL (KIND=nag_wp) arrayUser Workspace

MONFUN is called with the arguments
IUSER and
RUSER as supplied to G02EFF. You should use the arrays
IUSER and
RUSER to supply information to
MONFUN.
MONFUN must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which G02EFF is called. Arguments denoted as
Input must
not be changed by this procedure.
 17: $\mathrm{IUSER}\left(*\right)$ – INTEGER arrayUser Workspace
 18: $\mathrm{RUSER}\left(*\right)$ – REAL (KIND=nag_wp) arrayUser Workspace

IUSER and
RUSER are not used by G02EFF, but are passed directly to
MONFUN and should be used to pass information to this routine.
 19: $\mathrm{IFAIL}$ – INTEGERInput/Output

On entry:
IFAIL must be set to
$0$,
$1\text{ or}1$. If you are unfamiliar with this argument you should refer to
Section 3.4 in How to Use the NAG Library and its Documentation 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 argument, 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}}=0$ or
$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{FIN}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{FIN}}>0.0$.
On entry, ${\mathbf{FOUT}}=\u2329\mathit{\text{value}}\u232a$; ${\mathbf{FIN}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: $0.0\le {\mathbf{FOUT}}\le {\mathbf{FIN}}$.
On entry, ${\mathbf{M}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{M}}>1$.
On entry, ${\mathbf{MONLEV}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{MONLEV}}=0$ or $1$.
On entry, ${\mathbf{N}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{N}}>1$.
On entry, ${\mathbf{SW}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{SW}}>1.0$.
On entry, ${\mathbf{TAU}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{TAU}}>0.0$.
 ${\mathbf{IFAIL}}=2$

No free variables from which to select.
At least one element of
ISX should be set to
$1$.
On entry, invalid value for ${\mathbf{ISX}}\left(\u2329\mathit{\text{value}}\u232a\right)=\u2329\mathit{\text{value}}\u232a$.
On entry at least one diagonal element of ${\mathbf{C}}\le 0.0$.
 ${\mathbf{IFAIL}}=3$

The design and data matrix $Z$ is not positive definite, results may be inaccurate. All output is returned as documented.
 ${\mathbf{IFAIL}}=4$

All variables are collinear, no model to select.
 ${\mathbf{IFAIL}}=99$
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 3.9 in How to Use the NAG Library and its Documentation for further information.
 ${\mathbf{IFAIL}}=399$
Your licence key may have expired or may not have been installed correctly.
See
Section 3.8 in How to Use the NAG Library and its Documentation for further information.
 ${\mathbf{IFAIL}}=999$
Dynamic memory allocation failed.
See
Section 3.7 in How to Use the NAG Library and its Documentation for further information.
7 Accuracy
G02EFF returns a warning if the design and data matrix is not positive definite.
8 Parallelism and Performance
G02EFF is not threaded in any implementation.
Although the condition for removing or adding a variable to the current model is based on a ratio of variances, these values should not be interpreted as
$F$statistics with the usual interpretation of significance unless the probability levels are adjusted to account for correlations between variables under consideration and the number of possible updates (see, e.g.,
Draper and Smith (1985)).
G02EFF allocates internally $\mathcal{O}\left(4\times {\mathbf{M}}+\left({\mathbf{M}}+1\right)\times \left({\mathbf{M}}+2\right)/2+2\right)$ of real storage.
10 Example
This example calculates a full stepwise model selection for the Hald data described in
Dempster (1969). Means, the uppertriangular variancecovariance matrix and the sum of weights are calculated by
G02BUF. The NAG defined default monitor function G02EFH is used to print information at each step of the model selection process.
10.1 Program Text
Program Text (g02effe.f90)
10.2 Program Data
Program Data (g02effe.d)
10.3 Program Results
Program Results (g02effe.r)