NAG FL Interface
g02eaf (linregm_​rssq)

Settings help

FL Name Style:


FL Specification Language:


1 Purpose

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

2 Specification

Fortran Interface
Subroutine g02eaf ( mean, weight, n, m, x, ldx, vname, isx, y, wt, nmod, modl, ldmodl, rss, nterms, mrank, wk, ifail)
Integer, Intent (In) :: n, m, ldx, isx(m), ldmodl
Integer, Intent (Inout) :: ifail
Integer, Intent (Out) :: nmod, nterms(ldmodl), mrank(ldmodl)
Real (Kind=nag_wp), Intent (In) :: x(ldx,m), y(n), wt(*)
Real (Kind=nag_wp), Intent (Out) :: rss(ldmodl), wk(n*(m+1))
Character (*), Intent (In) :: vname(m)
Character (*), Intent (Inout) :: modl(ldmodl,m)
Character (1), Intent (In) :: mean, weight
C Header Interface
#include <nag.h>
void  g02eaf_ (const char *mean, const char *weight, const Integer *n, const Integer *m, const double x[], const Integer *ldx, const char vname[], const Integer isx[], const double y[], const double wt[], Integer *nmod, char modl[], const Integer *ldmodl, double rss[], Integer nterms[], Integer mrank[], double wk[], Integer *ifail, const Charlen length_mean, const Charlen length_weight, const Charlen length_vname, const Charlen length_modl)
The routine may be called by the names g02eaf or nagf_correg_linregm_rssq.

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:
  1. (i)null model
  2. (ii)A
  3. (iii)B
  4. (iv)C
  5. (v)A and B
  6. (vi)A and C
  7. (vii)B and C
  8. (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 Arguments

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 Integer Input
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 Integer Input
On entry: the number of variables contained in x.
Constraint: m2.
5: x(ldx,m) Real (Kind=nag_wp) array Input
On entry: x(i,j) must contain the ith observation for the jth independent variable, for i=1,2,,n and j=1,2,,m.
6: ldx Integer Input
On entry: the first dimension of the array x as declared in the (sub)program from which g02eaf is called.
Constraint: ldxn.
7: vname(m) Character(*) array Input
On entry: vname(j) must contain the name of the variable in column j of x, for j=1,2,,m.
8: isx(m) Integer array Input
On entry: indicates which independent variables are to be considered in the model.
isx(j)2
The variable contained in the jth column of x is included in all regression models, i.e., is a forced variable.
isx(j)=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.
isx(j)=0
The variable contained in the jth column of x is not included in the models.
Constraints:
  • isx(j)0, for j=1,2,,m;
  • at least one value of isx=1.
9: y(n) Real (Kind=nag_wp) array Input
On entry: y(i) must contain the ith observation on the dependent variable, yi, for i=1,2,,n.
10: wt(*) Real (Kind=nag_wp) array Input
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 with the model.
If wt(i)=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', wt(i)0.0, for i=1,2,,n.
11: nmod Integer Output
On exit: the total number of models for which residual sums of squares have been calculated.
12: modl(ldmodl,m) Character(*) array Output
On exit: the first nterms(i) 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 Integer Input
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.
Constraint: ldmodlmax(2k,m), 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: rss(ldmodl) Real (Kind=nag_wp) array Output
On exit: rss(i) contains the residual sum of squares for the ith model, for i=1,2,,nmod.
15: nterms(ldmodl) Integer array Output
On exit: nterms(i) contains the number of independent variables in the ith model, not including the mean if one is fitted, for i=1,2,,nmod.
16: mrank(ldmodl) Integer array Output
On exit: mrank(i) contains the rank of the residual sum of squares for the ith model.
17: wk(n×(m+1)) Real (Kind=nag_wp) array Workspace
18: ifail Integer Input/Output
On entry: ifail must be set to 0, −1 or 1 to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of 0 causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of −1 means that an error message is printed while a value of 1 means that it is not.
If halting is not appropriate, the value −1 or 1 is recommended. If message printing is undesirable, then the value 1 is recommended. Otherwise, the value 0 is recommended. 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, ldmodl=value and m=value.
Constraint: ldmodlm.
On entry, ldx=value and n=value.
Constraint: ldxn.
On entry, m=value.
Constraint: m2.
On entry, mean=value.
Constraint: mean='M' or 'Z'.
On entry, n=value.
Constraint: n2.
On entry, weight=value.
Constraint: weight='W' or 'U'.
ifail=2
On entry, wt(value)<0.0.
Constraint: wt(i)0.0, for i=1,2,,n.
ifail=3
On entry, isx(value)<0.
Constraint: isx(i)0, for i=1,2,,m.
There are no free variables, i.e., no element of isx=1.
ifail=4
On entry, ldmodl=value and number of possible models is value.
Constraint: ldmodl the number of possible models.
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 7 in the Introduction to the NAG Library FL Interface for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface 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

Background information to multithreading can be found in the Multithreading documentation.
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 rss(1) contains the total sum of squares and in many situations a reasonable estimate of the variance of the errors is given by rss(nmod)/(n-1-nterms(nmod)).

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)