G02EAF (PDF version)
G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G02EAF

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.

 Contents

    1  Purpose
    7  Accuracy

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 k possible independent variables there are 2k linear regression models with from zero to k independent variables in each model. For example if 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 2k 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 2kth 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.
MEAN='M'
A mean term, intercept, will be included in the model.
MEAN='Z'
The model will pass through the origin, zero-point.
Constraint: MEAN='M' or 'Z'.
2:     WEIGHT – CHARACTER(1)Input
On entry: indicates if weights are to be used.
WEIGHT='U'
Least squares estimation is used.
WEIGHT='W'
Weighted least squares is used and weights must be supplied in array WT.
Constraint: WEIGHT='U' or 'W'.
3:     N – INTEGERInput
On entry: n, the number of observations.
Constraints:
  • N2;
  • Nm, 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: M2.
5:     XLDXM – REAL (KIND=nag_wp) arrayInput
On entry: Xij must contain the ith observation for the jth independent variable, for i=1,2,,N and j=1,2,,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: LDXN.
7:     VNAMEM – CHARACTER(*) arrayInput
On entry: VNAMEj must contain the name of the variable in column j of X, for j=1,2,,M.
8:     ISXM – INTEGER arrayInput
On entry: indicates which independent variables are to be considered in the model.
ISXj2
The variable contained in the jth column of X is included in all regression models, i.e., is a forced variable.
ISXj=1
The variable contained in the jth column of X is included in the set from which the regression models are chosen, i.e., is a free variable.
ISXj=0
The variable contained in the jth column of X is not included in the models.
Constraints:
  • ISXj0, for j=1,2,,M;
  • at least one value of ISX=1.
9:     YN – REAL (KIND=nag_wp) arrayInput
On entry: Yi must contain the ith observation on the dependent variable, yi, for i=1,2,,n.
10:   WT* – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array WT must be at least N if WEIGHT='W'.
On entry: if WEIGHT='W', WT must contain the weights to be used in the weighted regression.
If WTi=0.0, the ith observation is not included in the model, in which case the effective number of observations is the number of observations with nonzero weights.
If WEIGHT='U', WT is not referenced and the effective number of observations is N.
Constraint: if WEIGHT='W', WTi0.0, for i=1,2,,n.
11:   NMOD – INTEGEROutput
On exit: the total number of models for which residual sums of squares have been calculated.
12:   MODLLDMODLM – CHARACTER(*) arrayOutput
On exit: the first NTERMSi elements of the ith row of MODL contain the names of the independent variables, as given in VNAME, that are included in the ith 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:
  • LDMODLM;
  • LDMODL2k, k is the number of free variables in the model as specified in ISX, and hence 2k is the total number of models to be generated.
14:   RSSLDMODL – REAL (KIND=nag_wp) arrayOutput
On exit: RSSi contains the residual sum of squares for the ith model, for i=1,2,,NMOD.
15:   NTERMSLDMODL – INTEGER arrayOutput
On exit: NTERMSi contains the number of independent variables in the ith model, not including the mean if one is fitted, for i=1,2,,NMOD.
16:   MRANKLDMODL – INTEGER arrayOutput
On exit: MRANKi contains the rank of the residual sum of squares for the ith model.
17:   WKN×M+1 – REAL (KIND=nag_wp) arrayWorkspace
18:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ 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​ 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 -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry 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:
IFAIL=1
On entry,N<2,
orM<2,
orLDX<N,
orLDMODL<M,
orMEAN'M' or 'Z',
or WEIGHT'U' or 'W'.
IFAIL=2
On entry, WEIGHT='W' and a value of WT<0.0.
IFAIL=3
On entry,a value of ISX<0,
orthere are no free variables, i.e., no element of ISX=1.
IFAIL=4
On entry, LDMODL< the number of possible models =2k, where k is the number of free independent variables from ISX.
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.
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.
IFAIL=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.8 in the Essential Introduction for further information.
IFAIL=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
IFAIL=-999
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

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  Parallelism and Performance

G02EAF is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
G02EAF makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9  Further Comments

G02ECF may be used to compute R2 and Cp-values from the results of G02EAF.
If a mean has been included in the model and no variables are forced in then RSS1 contains the total sum of squares and in many situations a reasonable estimate of the variance of the errors is given by RSSNMOD/N-1-NTERMSNMOD.

10  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.

10.1  Program Text

Program Text (g02eafe.f90)

10.2  Program Data

Program Data (g02eafe.d)

10.3  Program Results

Program Results (g02eafe.r)


G02EAF (PDF version)
G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2015