NAG CPP Interface
nagcpp::correg::lars (g02ma)
1
Purpose
lars performs Least Angle Regression (LARS), forward stagewise linear regression or Least Absolute Shrinkage and Selection Operator (LASSO).
2
Specification
#include "g02/nagcpp_g02ma.hpp"
template <typename D, typename ISX, typename Y, typename B, typename FITSUM>
void function lars(const types::f77_integer mtype, const D &d, const ISX &isx, const Y &y, types::f77_integer &ip, types::f77_integer &nstep, B &&b, FITSUM &&fitsum, OptionalG02MA opt)
template <typename D, typename ISX, typename Y, typename B, typename FITSUM>
void function lars(const types::f77_integer mtype, const D &d, const ISX &isx, const Y &y, types::f77_integer &ip, types::f77_integer &nstep, B &&b, FITSUM &&fitsum)
3
Description
lars implements the LARS algorithm of
Efron et al. (2004) as well as the modifications needed to perform forward stagewise linear regression and fit LASSO and positive LASSO models.
Given a vector of
observed values,
and an
design matrix
, where the
th column of
, denoted
, is a vector of length
representing the
th independent variable
, standardized such that
, and
and a set of model parameters
to be estimated from the observed values, the LARS algorithm can be summarised as:
-
1.Set and all coefficients to zero, that is .
-
2.Find the variable most correlated with , say . Add to the ‘most correlated’ set . If go to 8.
-
3.Take the largest possible step in the direction of (i.e., increase the magnitude of ) until some other variable, say , has the same correlation with the current residual, .
-
4.Increment and add to .
-
5.If go to 8.
-
6.Proceed in the ‘least angle direction’, that is, the direction which is equiangular between all variables in , altering the magnitude of the parameter estimates of those variables in , until the th variable, , has the same correlation with the current residual.
-
7.Go to 4.
-
8.Let .
As well as being a model selection process in its own right, with a small number of modifications the LARS algorithm can be used to fit the LASSO model of
Tibshirani (1996), a positive LASSO model, where the independent variables enter the model in their defined direction (i.e.,
), forward stagewise linear regression (
Hastie et al. (2001)) and forward selection (
Weisberg (1985)). Details of the required modifications in each of these cases are given in
Efron et al. (2004).
The LASSO model of
Tibshirani (1996) is given by
for all values of
, where
. The positive LASSO model is the same as the standard LASSO model, given above, with the added constraint that
Unlike the standard LARS algorithm, when fitting either of the LASSO models, variables can be dropped as well as added to the set . Therefore, the total number of steps is no longer bounded by .
Forward stagewise linear regression is an iterative procedure of the form:
-
1.Initialize and the vector of residuals .
-
2.For each calculate . The value is, therefore, proportional to the correlation between the th independent variable and the vector of previous residual values, .
-
3.Calculate , the value of with the largest absolute value of .
-
4.If then go to 7.
-
5.Update the residual values, with
where is a small constant and when and otherwise.
-
6.Increment and go to 2.
-
7.Set .
If the largest possible step were to be taken, that is
then forward stagewise linear regression reverts to the standard forward selection method as implemented in
g02eef (no CPP interface).
The LARS procedure results in
models, one for each step of the fitting process. In order to aid in choosing which is the most suitable
Efron et al. (2004) introduced a
-type statistic given by
where
is the approximate degrees of freedom for the
th step and
One way of choosing a model is, therefore, to take the one with the smallest value of .
4
References
Efron B, Hastie T, Johnstone I and Tibshirani R (2004) Least Angle Regression The Annals of Statistics (Volume 32) 2 407–499
Hastie T, Tibshirani R and Friedman J (2001) The Elements of Statistical Learning: Data Mining, Inference and Prediction Springer (New York)
Tibshirani R (1996) Regression Shrinkage and Selection via the Lasso Journal of the Royal Statistics Society, Series B (Methodological) (Volume 58) 1 267–288
Weisberg S (1985) Applied Linear Regression Wiley
5
Arguments
-
1:
– types::f77_integer
Input
-
On entry: indicates the type of model to fit.
- LARS is performed.
- Forward linear stagewise regression is performed.
- LASSO model is fit.
- A positive LASSO model is fit.
Constraint:
, , or .
-
2:
– double array
Input
-
On entry:
, the data, which along with
pred and
isx, defines the design matrix
. The
th observation for the
th variable must be supplied in
, for
and
.
-
3:
– types::f77_integer array
Input
-
On entry: indicates which independent variables from
d will be included in the design matrix,
.
If
isx is
nullptr, all variables are included in the design matrix.
Otherwise
must be set as follows, for
:
- To indicate that the th variable, as supplied in d, is included in the design matrix;
- To indicated that the th variable, as supplied in d, is not included in the design matrix;
and
.
Constraint:
if ,
or and at least one value of , for .
-
4:
– double array
Input
-
On entry: , the observations on the dependent variable.
-
5:
– types::f77_integer
Output
-
On exit:
, number of parameter estimates.
If
isx is
nullptr,
, i.e., the number of variables in
d.
Otherwise
is the number of nonzero values in
isx.
-
6:
– types::f77_integer
Output
-
On exit: , the actual number of steps carried out in the model fitting process.
-
7:
– double array
Output
-
On exit:
the parameter estimates, with
, the parameter estimate for the
th variable,
at the
th step of the model fitting process,
.
By default, when
or
the parameter estimates are rescaled prior to being returned. If the parameter estimates are required on the normalized scale, then this can be overridden via
ropt.
The values held in the remaining part of
b depend on the type of preprocessing performed.
- If ,
- If ,
- If ,
- If ,
for .
-
8:
– double array
Output
-
On exit: summaries of the model fitting process. When
,
- , the sum of the absolute values of the parameter estimates for the th step of the modelling fitting process. If or , the scaled parameter estimates are used in the summation.
- , the residual sums of squares for the th step, where .
- , approximate degrees of freedom for the th step.
- , a -type statistic for the th step, where .
- , correlation between the residual at step and the most correlated variable not yet in the active set , where the residual at step is .
- , the step size used at step .
In addition
- , with if and otherwise.
- , the residual sums of squares for the null model, where when and otherwise.
- , the degrees of freedom for the null model, where if and otherwise.
- , a -type statistic for the null model, where .
- , where and .
Although the statistics described above are returned when they may not be meaningful due to the estimate not being based on the saturated model.
-
9:
– OptionalG02MA
Input/Output
-
Optional parameter container, derived from
Optional.
Container for:
- pred – types::f77_integer
This optional parameter
may be set using the method
OptionalG02MA::pred
and accessed via
OptionalG02MA::get_pred.
Default:
On entry: indicates the type of data preprocessing to perform on the independent variables supplied in
d to comply with the standardized form of the design matrix.
- No preprocessing is performed.
- Each of the independent variables,
, for , are mean centred prior to fitting the model. The means of the independent variables, , are returned in b, with
, for .
- Each independent variable is normalized, with the th variable scaled by . The scaling factor used by variable is returned in .
- As and , all of the independent variables are mean centred prior to being normalized.
Suggested value:
.
Constraint:
, , or .
- prey – types::f77_integer
This optional parameter
may be set using the method
OptionalG02MA::prey
and accessed via
OptionalG02MA::get_prey.
Default:
On entry: indicates the type of data preprocessing to perform on the dependent variable supplied in
y.
- No preprocessing is performed, this is equivalent to setting .
- The dependent variable, , is mean centred prior to fitting the model, so . Which is equivalent to fitting a non-penalized intercept to the model and the degrees of freedom etc. are adjusted accordingly.
The value of used is returned in .
Suggested value:
.
Constraint:
or .
- mnstep – types::f77_integer
This optional parameter
may be set using the method
OptionalG02MA::mnstep
and accessed via
OptionalG02MA::get_mnstep.
Default: if : ; otherwise:
On entry: the maximum number of steps to carry out in the model fitting process.
If , i.e., a LARS is being performed, the maximum number of steps the algorithm will take is if , otherwise .
If , i.e., a forward linear stagewise regression is being performed, the maximum number of steps the algorithm will take is likely to be several orders of magnitude more and is no longer bound by or .
If or , i.e., a LASSO or positive LASSO model is being fit, the maximum number of steps the algorithm will take lies somewhere between that of the LARS and forward linear stagewise regression, again it is no longer bound by or .
Constraint:
.
- ropt – vector<double> array
This optional parameter
may be set using the method
OptionalG02MA::ropt
and accessed via
OptionalG02MA::get_ropt.
On entry: optional parameters to control various aspects of the LARS algorithm.
The default value will be used for
if the length of
ropt is less than
, therefore, to use the default values for all optional parameters
ropt need not be set. The default value will also be used if an invalid value is supplied for a particular argument, for example, setting
will use the default value for argument
.
- The minimum step size that will be taken.
Default is
, where
is the
machine precision returned by
precision.
- General tolerance, used amongst other things, for comparing correlations.
Default is .
- If set to , parameter estimates are rescaled before being returned.
If set to , no rescaling is performed.
This argument has no effect when or .
Default is for the parameter estimates to be rescaled.
- If set to , it is assumed that the model contains an intercept during the model fitting process and when calculating the degrees of freedom.
If set to , no intercept is assumed.
This has no effect on the amount of preprocessing performed on
y.
Default is to treat the model as having an intercept when and as not having an intercept when .
- As implemented, the LARS algorithm can either work directly with and , or it can work with the cross-product matrices, and . In most cases it is more efficient to work with the cross-product matrices. This flag allows you direct control over which method is used, however, the default value will usually be the best choice.
If , and are worked with directly.
If , the cross-product matrices are used.
Default is when and and otherwise.
Constraints:
- ;
- ;
- or ;
- or ;
- or .
5.1Additional Quantities
- 1:
- , the number of observations.
- 2:
- , the total number of independent variables.
- 3:
- Length of the isx array.
- 4:
- The first dimension of the array b.
- 5:
- Length of the options array ropt
6
Exceptions and Warnings
Errors or warnings detected by the function:
Note: in some cases lars may return useful information.
All errors and warnings have an associated numeric error code field,
errorid, stored either as a member of the thrown exception object (see
errorid), or as a member of
opt.
ifail, depending on how errors
and warnings are being handled (see
Error Handling for more details).
- Raises: ErrorException
-
- On entry, .
Constraint: .
- On entry, .
Constraint: .
- On entry, .
Constraint: .
- On entry, .
Constraint: .
- On entry, .
Constraint: .
- On entry, .
Constraint: , for all .
- On entry, all values of isx are zero.
Constraint: at least one value of isx must be nonzero.
- On entry, and .
Constraint: .
- On entry, .
Constraint: .
- On entry, and .
Constraint: if then .
- On entry, and .
Constraint: if then .
- On entry, .
Constraint: .
- On entry, argument must be a vector of size array.
Supplied argument has dimensions.
- On entry, argument must be a vector of size array.
Supplied argument was a vector of size .
- On entry, argument must be a vector of size array.
The size for the supplied array could not be ascertained.
- On entry, argument must be a x array.
Supplied argument has dimensions.
- On entry, argument must be a x array.
Supplied argument was a x array.
- On entry, argument must be a x array.
Not all of the sizes for the supplied array could be ascertained.
- On entry, the raw data component of is null.
- On entry, unable to ascertain a value for .
- On entry, the data in is stored in Major Order.
The data was expected to be in Major Order.
- An exception was thrown during IO (writing).
- An unexpected error has been triggered by this routine.
- Your licence key may have expired or may not have been installed correctly.
- Dynamic memory allocation failed.
- Raises: WarningException
-
- Fitting process did not finish in mnstep steps.
Try increasing the size of mnstep and supplying larger output arrays.
All output is returned as documented, up to step mnstep, however,
and the statistics may not be meaningful.
- is approximately zero and hence the -type criterion
cannot be calculated. All other output is returned as documented.
- , therefore, has been set to a large value.
Output is returned as documented.
- Degenerate model, no variables added and .
Output is returned as documented.
- Degenerate model, no variables added and .
Output is returned as documented.
7
Accuracy
Not applicable.
8
Parallelism and Performance
Please see the description for the underlying computational routine in this section of the
FL Interface documentation.
lars returns the parameter estimates at various points along the solution path of a LARS, LASSO or stagewise regression analysis. If the solution is required at a different set of points, for example when performing cross-validation, then
g02mcf (no CPP interface) can be used.
For datasets with a large number of observations,
, it may be impractical to store the full
matrix in memory in one go. In such instances the cross-product matrices
and
can be calculated, using for example, multiple calls to
g02buf (no CPP interface) and
g02bzf (no CPP interface), and
g02mbf (no CPP interface) called to perform the analysis.
The amount of workspace used by
lars depends on whether the cross-product matrices are being used internally (as controlled by
ropt). If the cross-product matrices are being used then
lars internally allocates approximately
elements of real storage compared to
elements when
and
are used directly. In both cases approximately
elements of integer storage are also used. If a forward linear stagewise analysis is performed than an additional
elements of real storage are required.
10
Example
This example performs a LARS on a simulated dataset with observations and independent variables.
10.1
Example Program
10.2
Plot