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

# NAG Toolbox: nag_correg_linregm_fit_stepwise (g02ef)

## Purpose

nag_correg_linregm_fit_stepwise (g02ef) 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) variance-covariance, (weighted) means and sum of weights of $Z$ must be supplied.

## Syntax

[isx, b, se, rsq, rms, df, user, ifail] = g02ef(n, wmean, c, sw, isx, 'm', m, 'fin', fin, 'fout', fout, 'tau', tau, 'monfun', monfun, 'user', user)
[isx, b, se, rsq, rms, df, user, ifail] = nag_correg_linregm_fit_stepwise(n, wmean, c, sw, isx, 'm', m, 'fin', fin, 'fout', fout, 'tau', tau, 'monfun', monfun, 'user', user)
Note: the interface to this routine has changed since earlier releases of the toolbox:
 At Mark 23: monfun was made optional; monlevel was removed from the interface

## Description

The general multiple linear regression model is defined by
 $y = β0 +Xβ+ε,$
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.
nag_correg_linregm_fit_stepwise (g02ef) 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:
 $s2 - s1 / q2 - q1 s1 / q1 > f1 ,$
must be satisfied if a variable $k$ will be considered for entry to the current model, and the condition:
 $s1 - s2 / q1 - q2 s1 / q1 < f2 ,$
must be satisfied if a variable $k$ will be considered for removal from the current model, where ${f}_{1}$ and ${f}_{2}$ are user-supplied 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 function. 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:
 $R = 1 … r1p r1y ⋮ ⋱ ⋮ ⋮ rp1 … 1 rpy ry1 … ryp 1 ,$
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:
 $rkk ​ by ​ -1 / rkk ; rik ​ by ​ rik / rkk , i=1,2,…,p+1 ​ ​ i≠k ; rkj ​ by ​ rkj / rkk , j=1,2,…,p+1 ​ ​ j≠k ; rij ​ by ​ rij - rik rkj / rkk , ​ i=1,2,…,p+1 ​ ​ i≠k ; ​ j=1,2,…,p+1 ​ ​ j≠k .$
The $k$th explanatory variable is eligible for entry into the current model if it satisfies the collinearity tests: ${r}_{kk}>\tau$ and
 $rii - rik rki rkk τ≤1 ,$
for a user-supplied 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 function 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 function 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.

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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
The number of observations used in the calculations.
Constraint: ${\mathbf{n}}>1$.
2:     $\mathrm{wmean}\left({\mathbf{m}}+1\right)$ – double array
The mean of the design matrix, $Z$.
3:     $\mathrm{c}\left(\left({\mathbf{m}}+1\right)×\left({\mathbf{m}}+2\right)/2\right)$ – double array
The upper-triangular variance-covariance matrix packed by column for the design matrix, $Z$. Because the function computes the correlation matrix $R$ from c, the variance-covariance matrix need only be supplied up to a scaling factor.
4:     $\mathrm{sw}$ – double scalar
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$.
5:     $\mathrm{isx}\left({\mathbf{m}}\right)$int64int32nag_int array
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}}$.

### Optional Input Parameters

1:     $\mathrm{m}$int64int32nag_int scalar
Default: the dimension of the array isx.
The number of explanatory variables available in the design matrix, $Z$.
Constraint: ${\mathbf{m}}>1$.
2:     $\mathrm{fin}$ – double scalar
Default: $4.0$
The value of the variance ratio which an explanatory variable must exceed to be included in a model.
Constraint: ${\mathbf{fin}}>0.0$.
3:     $\mathrm{fout}$ – double scalar
Default: ${\mathbf{fin}}$
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.
Constraint: $0.0\le {\mathbf{fout}}\le {\mathbf{fin}}$.
4:     $\mathrm{tau}$ – double scalar
Default: $0.000001$
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.
Constraint: ${\mathbf{tau}}>0.0$.
5:     $\mathrm{monfun}$ – function handle or string containing name of m-file
You may define your own function or specify the NAG defined default function nag_correg_linregm_fit_stepwise_sample_monfun (g02efh).
If $\mathit{monlev}=0$, monfun is not referenced; otherwise its specification is:
[user] = monfun(flag, var, val, user)

Input Parameters

1:     $\mathrm{flag}$ – string (length ≥ 1)
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}$int64int32nag_int scalar
The index of the explanatory variable in the design matrix $Z$ to which flag pertains.
3:     $\mathrm{val}$ – double scalar
If ${\mathbf{flag}}=\text{'V'}$, val is the variance ratio value for the coefficient associated with explanatory variable index var.
4:     $\mathrm{user}$ – Any MATLAB object
monfun is called from nag_correg_linregm_fit_stepwise (g02ef) with the object supplied to nag_correg_linregm_fit_stepwise (g02ef).

Output Parameters

1:     $\mathrm{user}$ – Any MATLAB object
6:     $\mathrm{user}$ – Any MATLAB object
user is not used by nag_correg_linregm_fit_stepwise (g02ef), but is passed to monfun. Note that for large objects it may be more efficient to use a global variable which is accessible from the m-files than to use user.

### Output Parameters

1:     $\mathrm{isx}\left({\mathbf{m}}\right)$int64int32nag_int array
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.
2:     $\mathrm{b}\left({\mathbf{m}}+1\right)$ – double array
${\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$.
3:     $\mathrm{se}\left({\mathbf{m}}+1\right)$ – double array
${\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$.
4:     $\mathrm{rsq}$ – double scalar
The ${R}^{2}$-statistic for the fitted regression model.
5:     $\mathrm{rms}$ – double scalar
The mean square of residuals for the fitted regression model.
6:     $\mathrm{df}$int64int32nag_int scalar
The number of degrees of freedom for the sum of squares of residuals.
7:     $\mathrm{user}$ – Any MATLAB object
8:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and 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.

${\mathbf{ifail}}=1$
Constraint: $0.0\le {\mathbf{fout}}\le {\mathbf{fin}}$.
Constraint: ${\mathbf{fin}}>0.0$.
Constraint: ${\mathbf{m}}>1$.
Constraint: $\mathit{monlev}=0$ or $1$.
Constraint: ${\mathbf{n}}>1$.
Constraint: ${\mathbf{sw}}>1.0$.
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 .
On entry at least one diagonal element of ${\mathbf{c}}\le 0.0$.
W  ${\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$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

nag_correg_linregm_fit_stepwise (g02ef) returns a warning if the design and data matrix is not positive definite.

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)).
nag_correg_linregm_fit_stepwise (g02ef) allocates internally $\mathcal{O}\left(4×{\mathbf{m}}+\left({\mathbf{m}}+1\right)×\left({\mathbf{m}}+2\right)/2+2\right)$ of double storage.

## Example

This example calculates a full stepwise model selection for the Hald data described in Dempster (1969). Means, the upper-triangular variance-covariance matrix and the sum of weights are calculated by nag_correg_ssqmat (g02bu). The NAG defined default monitor function nag_correg_linregm_fit_stepwise_sample_monfun (g02efh) is used to print information at each step of the model selection process.
```function g02ef_example

fprintf('g02ef example results\n\n');

z   = [  7  26   6  60   78.5;
1  29  15  52   74.3;
11  56   8  20  104.3;
11  31   8  47   87.6;
7  52   6  33   95.9;
11  55   9  22  109.2;
3  71  17   6  102.7;
1  31  22  44   72.5;
2  54  18  22   93.1;
21  47   4  26  115.9;
1  40  23  34   83.8;
11  66   9  12  113.3;
10  68   8  12  109.4];
[n,m1] = size(z);
m = m1 - 1;

isx = ones(m,1,'int64');

% Compute sums of squares and cross-products of deviations from mean for z
[sw, wmean, c, ifail] = g02bu(z);

% Perform stepwise selection of variables
fout = 2;
[isx, b, se, rsq, rms, df, user, ifail] = ...
g02ef( ...
int64(n), wmean, c, sw, isx, 'fout', fout, 'monfun', @monfun);

% Display results
fprintf('\nFitted Model Summary\n');
fprintf('Term              Estimate   Standard Error\n');
fprintf('Intercept:    %12.3e     %12.3e\n', b(1), se(1));
for j = 1:m
if isx(j)==1 || isx(j)==2
fprintf('Variable: %3d %12.3e     %12.3e\n', j, b(j+1), se(j+1));
end
end
fprintf('\nRMS: %12.3e\n', rms);

function [user] = monfun(flag, var, val, user)

switch flag
case 'C'
fprintf('\nVariable %d aliased\n', var);
case 'S'
fprintf('\nStarting Stepwise Selection\n');
case 'F'
fprintf('\nForward Selection\n');
case 'V'
fprintf('Variable %d  Variance ratio = %12.3f\n', var, val);
case 'A'
fprintf('\nAdding variable %d to model\n', var);
case 'B'
fprintf('\nBackward Selection\n');
case 'D'
fprintf('\nDropping variable %d from model\n', var);
case 'K'
fprintf('\nKeeping all current variables\n');
case 'X'
fprintf('\nFinished Stepwise Selection\n');
end;
```
```g02ef example results

Starting Stepwise Selection

Forward Selection
Variable 1  Variance ratio =       12.603
Variable 2  Variance ratio =       21.961
Variable 3  Variance ratio =        4.403
Variable 4  Variance ratio =       22.799

Backward Selection
Variable 4  Variance ratio =       22.799

Keeping all current variables

Forward Selection
Variable 1  Variance ratio =      108.224
Variable 2  Variance ratio =        0.172
Variable 3  Variance ratio =       40.295

Backward Selection
Variable 1  Variance ratio =      108.224
Variable 4  Variance ratio =      159.295

Keeping all current variables

Forward Selection
Variable 2  Variance ratio =        5.026
Variable 3  Variance ratio =        4.236

Backward Selection
Variable 1  Variance ratio =      154.008
Variable 2  Variance ratio =        5.026
Variable 4  Variance ratio =        1.863

Dropping variable 4 from model

Forward Selection
Variable 3  Variance ratio =        1.832
Variable 4  Variance ratio =        1.863

Finished Stepwise Selection

Fitted Model Summary
Term              Estimate   Standard Error
Intercept:       5.258e+01        2.294e+00
Variable:   1    1.468e+00        1.213e-01
Variable:   2    6.623e-01        4.585e-02

RMS:    5.790e+00
```