NAG CL Interface
g02mbc (lars_​xtx)

1 Purpose

g02mbc performs Least Angle Regression (LARS), forward stagewise linear regression or Least Absolute Shrinkage and Selection Operator (LASSO) using cross-product matrices.

2 Specification

#include <nag.h>
void  g02mbc (Nag_LARSModelType mtype, Nag_LARSPreProcess pred, Nag_LARSPreProcess intcpt, Integer n, Integer m, const double dtd[], Integer pddtd, const Integer isx[], const double dty[], double yty, Integer mnstep, Integer *ip, Integer *nstep, double b[], Integer pdb, double fitsum[], const double ropt[], Integer lropt, NagError *fail)
The function may be called by the names: g02mbc, nag_correg_lars_xtx or nag_lars_xtx.

3 Description

g02mbc 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 n observed values, y = yi : i=1,2,,n and an n×p design matrix X, where the jth column of X, denoted xj, is a vector of length n representing the jth independent variable xj, standardized such that i=1 n xij =0 , and i=1 n xij2 =1 and a set of model parameters β to be estimated from the observed values, the LARS algorithm can be summarised as:
  1. 1.Set k=1 and all coefficients to zero, that is β=0.
  2. 2.Find the variable most correlated with y, say xj1. Add xj1 to the ‘most correlated’ set A. If p=1 go to 8.
  3. 3.Take the largest possible step in the direction of xj1 (i.e., increase the magnitude of βj1) until some other variable, say xj2, has the same correlation with the current residual, y-xj1βj1.
  4. 4.Increment k and add xjk to A.
  5. 5.If A=p go to 8.
  6. 6.Proceed in the ‘least angle direction’, that is, the direction which is equiangular between all variables in A, altering the magnitude of the parameter estimates of those variables in A, until the kth variable, xjk, has the same correlation with the current residual.
  7. 7.Go to 4.
  8. 8.Let K=k.
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, 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
minimize α , βk p y- α- XT βk 2   subject to   βk1 tk  
for all values of tk, where α=y¯=n-1 i=1 n yi. The positive LASSO model is the same as the standard LASSO model, given above, with the added constraint that
βkj 0 ,   j=1,2,,p .  
Unlike the standard LARS algorithm, when fitting either of the LASSO models, variables can be dropped as well as added to the set A. Therefore the total number of steps K is no longer bounded by p.
Forward stagewise linear regression is an iterative procedure of the form:
  1. 1.Initialize k=1 and the vector of residuals r0=y-α.
  2. 2.For each j=1,2,,p calculate cj=xjTrk-1. The value cj is therefore proportional to the correlation between the jth independent variable and the vector of previous residual values, rk.
  3. 3.Calculate jk = argmax j cj , the value of j with the largest absolute value of cj.
  4. 4.If cjk<ε then go to 7.
  5. 5.Update the residual values, with
    rk = rk-1 + δ ​ ​ signcjk xjk  
    where δ is a small constant and signcjk=-1 when cjk<0 and 1 otherwise.
  6. 6.Increment k and go to 2.
  7. 7.Set K=k.
If the largest possible step were to be taken, that is δ=cjk then forward stagewise linear regression reverts to the standard forward selection method as implemented in g02eec.
The LARS procedure results in K 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 Cp-type statistic given by
Cpk = y- XT βk 2 σ2 -n+2νk,  
where νk is the approximate degrees of freedom for the kth step and
σ2 = n-yTyνK .  
One way of choosing a model is therefore to take the one with the smallest value of Cpk.

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: mtype Nag_LARSModelType Input
On entry: indicates the type of model to fit.
mtype=Nag_LARS_LAR
LARS is performed.
mtype=Nag_LARS_ForwardStagewise
Forward linear stagewise regression is performed.
mtype=Nag_LARS_LASSO
LASSO model is fit.
mtype=Nag_LARS_PositiveLASSO
A positive LASSO model is fit.
Constraint: mtype=Nag_LARS_LAR, Nag_LARS_ForwardStagewise, Nag_LARS_LASSO or Nag_LARS_PositiveLASSO.
2: pred Nag_LARSPreProcess Input
On entry: indicates the type of preprocessing to perform on the cross-products involving the independent variables, i.e., those supplied in dtd and dty.
pred=Nag_LARS_None
No preprocessing is performed.
pred=Nag_LARS_Normalized
Each independent variable is normalized, with the jth variable scaled by 1/xjTxj. The scaling factor used by variable j is returned in b[nstep×pdb+j-1].
Constraint: pred=Nag_LARS_None or Nag_LARS_Normalized.
3: intcpt Nag_LARSPreProcess Input
On entry: indicates the type of data preprocessing that was perform on the dependent variable, y, prior to calling this function.
intcpt=Nag_LARS_None
No preprocessing was performed.
intcpt=Nag_LARS_Centered
The dependent variable, y, was mean centred.
Constraint: intcpt=Nag_LARS_None or Nag_LARS_Centered.
4: n Integer Input
On entry: n, the number of observations.
Constraint: n1.
5: m Integer Input
On entry: m, the total number of independent variables.
Constraint: m1.
6: dtd[dim] const double Input
Note: the dimension, dim, of the array dtd must be at least
  • pddtd×(mm+1/2) when pddtd=1;
  • pddtd×m when.
On entry: DTD, the cross-product matrix, which along with isx, defines the design matrix cross-product XTX.
If pddtd=1, dtd[i×i-1/2+j-1×pddtd] must contain the cross-product of the ith and jth variable, for i=1,2,,m and j=1,2,,m. That is the cross-product stacked by columns as returned by g02buc, for example.
Otherwise dtd[j-1×pddtd+i-1] must contain the cross-product of the ith and jth variable, for i=1,2,,m and j=1,2,,m. It should be noted that, even though DTD is symmetric, the full matrix must be supplied.
The matrix specified in dtd must be a valid cross-products matrix.
7: pddtd Integer Input
On entry: the stride separating row elements in the two-dimensional data stored in the array dtd.
Constraint: pddtd=1 or pddtdm.
8: isx[dim] const Integer Input
Note: the dimension, dim, of the array isx must be at least
  • m, when isxis notNULL.
On entry: indicates which independent variables from dtd will be included in the design matrix, X.
If isx is NULL, all variables are included in the design matrix.
Otherwiseisx[j-1] must be set as follows, for j=1,2,,m:
isx[j-1]=1
To indicate that the jth variable, as supplied in dtd, is included in the design matrix;
isx[j-1]=0
To indicate that the jth variable, as supplied in dtd, is not included in the design matrix;
and p= j=1 m isx[j-1].
Constraint: isx[j-1]=0 or 1 and at least one value of isx[j-1]0, for j=1,2,,m.
9: dty[m] const double Input
On entry: DTy, the cross-product between the dependent variable, y, and the independent variables D.
10: yty double Input
On entry: yTy, the sums of squares of the dependent variable.
Constraint: yty>0.0.
11: mnstep Integer Input
On entry: the maximum number of steps to carry out in the model fitting process.
If mtype=Nag_LARS_LAR, the maximum number of steps the algorithm will take is minp,n if intcpt=Nag_LARS_None, otherwise minp,n-1.
If mtype=Nag_LARS_ForwardStagewise, the maximum number of steps the algorithm will take is likely to be several orders of magnitude more and is no longer bound by p or n.
If mtype=Nag_LARS_LASSO or Nag_LARS_PositiveLASSO, 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 p or n.
Constraint: mnstep1.
12: ip Integer * Output
On exit: p, number of parameter estimates.
If isx is NULL, p=m, i.e., the number of variables in dtd.
Otherwise p is the number of nonzero values in isx.
13: nstep Integer * Output
On exit: K, the actual number of steps carried out in the model fitting process.
14: b[dim] double Output
Note: the dimension, dim, of the array b must be at least pdb×(mnstep+1).
On exit: β the parameter estimates, with b[k-1×pdb+j-1]=βkj, the parameter estimate for the jth variable, j=1,2,,p at the kth step of the model fitting process, k=1,2,,nstep.
By default, when pred=Nag_LARS_Normalized 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 ​pred=Nag_LARS_None b[nstep×pdb+j-1] = 1, if ​pred=Nag_LARS_Normalized b[nstep×pdb+j-1] = 1/ xjTxj,
for j=1,2,p.
15: pdb Integer Input
On entry: the stride separating row elements in the two-dimensional data stored in the array b.
Constraint: pdbp, where p is the number of parameter estimates as described in ip.
16: fitsum[6×mnstep+1] double Output
On exit: summaries of the model fitting process. When k=1,2,,nstep
fitsum[k-1×6]
βk1, the sum of the absolute values of the parameter estimates for the kth step of the modelling fitting process. If pred=Nag_LARS_Normalized, the scaled parameter estimates are used in the summation.
fitsum[k-1×6+1]
RSSk, the residual sums of squares for the kth step, where RSSk= y- XT βk 2 .
fitsum[k-1×6+2]
νk, approximate degrees of freedom for the kth step.
fitsum[k-1×6+3]
Cpk, a Cp-type statistic for the kth step, where Cpk=RSSkσ2-n+2νk.
fitsum[k-1×6+4]
C^k, correlation between the residual at step k-1 and the most correlated variable not yet in the active set A, where the residual at step 0 is y.
fitsum[k-1×6+5]
γ^k, the step size used at step k.
In addition
fitsum[nstep×6]
0.
fitsum[nstep×6+1]
RSS0, the residual sums of squares for the null model, where RSS0=yTy.
fitsum[nstep×6+2]
ν0, the degrees of freedom for the null model, where ν0=0 if intcpt=Nag_LARS_None and ν0=1 otherwise.
fitsum[nstep×6+3]
Cp0, a Cp-type statistic for the null model, where Cp0=RSS0σ2-n+2ν0.
fitsum[nstep×6+4]
σ2, where σ2=n-RSSKνK and K=nstep.
Although the Cp statistics described above are returned when fail.code= NW_LIMIT_REACHED they may not be meaningful due to the estimate σ2 not being based on the saturated model.
17: ropt[lropt] const double Input
On entry: optional parameters to control various aspects of the LARS algorithm.
The default value will be used for ropt[i-1] if lropt<i, therefore setting lropt=0 will use the default values for all optional parameters and ropt need not be set and may be NULL. The default value will also be used if an invalid value is supplied for a particular argument, for example, setting ropt[i-1]=-1 will use the default value for argument i.
ropt[0]
The minimum step size that will be taken.
Default is 100×eps is used, where eps is the machine precision returned by X02AJC.
ropt[1]
General tolerance, used amongst other things, for comparing correlations.
Default is ropt[0].
ropt[2]
If set to 1, parameter estimates are rescaled before being returned. If set to 0, no rescaling is performed. This argument has no effect when pred=Nag_LARS_None.
Default is for the parameter estimates to be rescaled.
Constraints:
  • ropt[0]>machine precision;
  • ropt[1]>machine precision.
18: lropt Integer Input
On entry: length of the options array ropt.
Constraint: 0lropt3.
19: fail NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

6 Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
NE_ARRAY_SIZE
On entry, lropt=value.
Constraint: 0lropt3.
On entry, pdb=value and m=value.
Constraint: if isx is NULL then pdbm.
On entry, pdb=value and p=value.
Constraint: if isx is not NULL, pdbp.
On entry, pddtd=value and m=value
Constraint: pddtd=1 or pddtdm.
NE_BAD_PARAM
On entry, argument value had an illegal value.
NE_INT
On entry, m=value.
Constraint: m1.
On entry, n=value.
Constraint: n1.
NE_INT_ARRAY
On entry, all values of isx are zero.
Constraint: at least one value of isx must be nonzero.
On entry, isx[value]=value.
Constraint: isx[i]=0 or 1, for all i.
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.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_MAX_STEP
On entry, mnstep=value.
Constraint: mnstep1.
NE_NEG_ELEMENT
On entry, dtd[value×pddtd]=value.
Constraint: diagonal elements of DTD must be positive.
On entry, i=value and dtd[i-1×pddtd+i-1]=value.
Constraint: diagonal elements of DTD must be positive.
NE_NEG_SX
A negative value for the residual sums of squares was obtained. Check the values of dtd, dty and yty.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_REAL
On entry, yty=value.
Constraint: yty>0.0.
NE_SYMM_MATRIX
The cross-product matrix supplied in dtd is not symmetric.
NW_LIMIT_REACHED
Fitting process did not finished 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 Cp statistics may not be meaningful.
NW_OVERFLOW_WARN
νK=n, therefore sigma has been set to a large value. Output is returned as documented.
σ2 is approximately zero and hence the Cp-type criterion cannot be calculated. All other output is returned as documented.
NW_POTENTIAL_PROBLEM
Degenerate model, no variables added and nstep=0. Output is returned as documented.

7 Accuracy

Not applicable.

8 Further Comments

The solution path to the LARS, LASSO and stagewise regression analysis is a continuous, piecewise linear. g02mbc returns the parameter estimates at various points along this path. g02mcc can be used to obtain estimates at different points along the path.
If you have the raw data values, that is D and y, then g02mac can be used instead of g02mbc.

9 Example

This example performs a LARS on a simulated dataset with 20 observations and 6 independent variables.
The example uses g02buc to get the cross-products of the augmented matrix Dy. The first mm+1/2 elements of the (column packed) cross-products matrix returned therefore contain the elements of DTD, the next m elements contain DTy and the last element yTy.

9.1 Program Text

Program Text (g02mbce.c)

9.2 Program Data

Program Data (g02mbce.d)

9.3 Program Results

Program Results (g02mbce.r)
This example plot shows the regression coefficients (βk) plotted against the scaled absolute sum of the parameter estimates (βk1).
GnuplotProduced by GNUPLOT 4.6 patchlevel 3 −1 0 1 2 3 4 0 20 40 60 80 100 120 140 160 180 200 220 Parameter Estimates (βkj) ||βk||1 Example Program Parameter estimates for a LARS model fitted to a simulated dataset gnuplot_plot_1 βk1 gnuplot_plot_2 βk2 gnuplot_plot_3 βk3 gnuplot_plot_4 βk4 gnuplot_plot_5 βk5 gnuplot_plot_6 βk6