nag_all_regsn (g02eac) (PDF version)
g02 Chapter Contents
g02 Chapter Introduction
NAG Library Manual

NAG Library Function Document

nag_all_regsn (g02eac)

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

nag_all_regsn (g02eac) calculates the residual sums of squares for all possible linear regressions for a given set of independent variables.

2  Specification

#include <nag.h>
#include <nagg02.h>
void  nag_all_regsn (Nag_OrderType order, Nag_IncludeMean mean, Integer n, Integer m, const double x[], Integer pdx, const char *var_names[], const Integer sx[], const double y[], const double wt[], Integer *nmod, const char *model[], double rss[], Integer nterms[], Integer mrank[], NagError *fail)

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.
nag_all_regsn (g02eac) 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.
nag_all_regsn (g02eac) 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:     orderNag_OrderTypeInput
On entry: the order argument specifies the two-dimensional storage scheme being used, i.e., row-major ordering or column-major ordering. C language defined storage is specified by order=Nag_RowMajor. See Section 3.2.1.3 in the Essential Introduction for a more detailed explanation of the use of this argument.
Constraint: order=Nag_RowMajor or Nag_ColMajor.
2:     meanNag_IncludeMeanInput
On entry: indicates if a mean term is to be included.
mean=Nag_MeanInclude
A mean term, intercept, will be included in the model.
mean=Nag_MeanZero
The model will pass through the origin, zero-point.
Constraint: mean=Nag_MeanInclude or Nag_MeanZero.
3:     nIntegerInput
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 sx.
4:     mIntegerInput
On entry: the number of variables contained in x.
Constraint: m2.
5:     x[dim]const doubleInput
Note: the dimension, dim, of the array x must be at least
  • max1,pdx×m when order=Nag_ColMajor;
  • max1,n×pdx when order=Nag_RowMajor.
Where Xi,j appears in this document, it refers to the array element
  • x[j-1×pdx+i-1] when order=Nag_ColMajor;
  • x[i-1×pdx+j-1] when order=Nag_RowMajor.
On entry: Xi,j must contain the ith observation for the jth independent variable, for i=1,2,,n and j=1,2,,m.
6:     pdxIntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array x.
Constraints:
  • if order=Nag_ColMajor, pdxn;
  • if order=Nag_RowMajor, pdxm.
7:     var_names[m]const char *Input
On entry: var_names[i-1] must contain the name of the independent variable in row i of x, for i=1,2,,m.
8:     sx[m]const IntegerInput
On entry: indicates which independent variables are to be considered in the model.
sx[j-1]2
The variable contained in the jth column of X is included in all regression models, i.e., is a forced variable.
sx[j-1]=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.
sx[j-1]=0
The variable contained in the jth column of X is not included in the models.
Constraints:
  • sx[j-1]0, for j=1,2,,m;
  • at least one value of sx=1.
9:     y[n]const doubleInput
On entry: y[i-1] must contain the ith observation on the dependent variable, yi, for i=1,2,,n.
10:   wt[n]const doubleInput
On entry: optionally, the weights to be used in the weighted regression.
If wt[i-1]=0.0, then 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 weights are not provided then wt must be set to NULL and the effective number of observations is n.
Constraint: if wtis notNULL, wt[i-1]=0.0, for i=1,2,,n.
11:   nmodInteger *Output
On exit: the total number of models for which residual sums of squares have been calculated.
12:   model[dim]const char *Output
Note: the dimension, dim, of the array model must be at least big enough to hold the names of all the free independent variables which appear in all the models. This will never exceed 2k×m, where k is the number of free variables in the model.
On exit: the names of the independent variables in each model, represented as pointers to the names provided by you in var_names. The model names are stored as follows:
  • if the first model has three names, i.e., nterms[0]=3; then model[0], model[1] and model[2] will contain these three names;
  • if the second model has two names, i.e., nterms[1]=2; then model[3], model[4] will contain these two names.
13:   rss[max2k,m]doubleOutput
On exit: rss[i-1] contains the residual sum of squares for the ith model, for i=1,2,,nmod.
14:   nterms[max2k,m]IntegerOutput
On exit: nterms[i-1] contains the number of independent variables in the ith model, not including the mean if one is fitted, for i=1,2,,nmod.
15:   mrank[max2k,m]IntegerOutput
On exit: mrank[i-1] contains the rank of the residual sum of squares for the ith model.
16:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_BAD_PARAM
On entry, argument value had an illegal value.
NE_FREE_VARS
There are no free x variables.
NE_FULL_RANK
Full model is not of full rank.
NE_INDEP_VARS_OBS
Number of requested x-variables  number of observations.
NE_INT
On entry, m=value.
Constraint: m2.
On entry, n=value.
Constraint: n2.
On entry, pdx=value.
Constraint: pdx>0.
NE_INT_2
On entry, pdx=value and m=value.
Constraint: pdxm.
On entry, pdx=value and n=value.
Constraint: pdxn.
NE_INT_ARRAY_ELEM_CONS
On entry, sx[value]<0.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
NE_REAL_ARRAY_ELEM_CONS
On entry, wt[value]<0.0.

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

nag_all_regsn (g02eac) is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
nag_all_regsn (g02eac) 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 Users' Note for your implementation for any additional implementation-specific information.

9  Further Comments

nag_cp_stat (g02ecc) may be used to compute R2 and Cp-values from the results of nag_all_regsn (g02eac).
If a mean has been included in the model and no variables are forced in then rss[0] contains the total sum of squares and in many situations a reasonable estimate of the variance of the errors is given by rss[nmod-1]/n-1-nterms[nmod-1].

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 (g02eace.c)

10.2  Program Data

Program Data (g02eace.d)

10.3  Program Results

Program Results (g02eace.r)


nag_all_regsn (g02eac) (PDF version)
g02 Chapter Contents
g02 Chapter Introduction
NAG Library Manual

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