hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_correg_ridge (g02kb)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

Purpose

nag_correg_ridge (g02kb) calculates a ridge regression, with ridge parameters supplied by you.

Syntax

[nep, b, vf, pe, ifail] = g02kb(x, isx, ip, y, h, wantb, wantvf, pec, 'n', n, 'm', m, 'lh', lh, 'lpec', lpec)
[nep, b, vf, pe, ifail] = nag_correg_ridge(x, isx, ip, y, h, wantb, wantvf, pec, 'n', n, 'm', m, 'lh', lh, 'lpec', lpec)

Description

A linear model has the form:
y = c+Xβ+ε ,  
where
Let X~ be the mean-centred X and y~ the mean-centred y. Furthermore, X~ is scaled such that the diagonal elements of the cross product matrix X~TX~ are one. The linear model now takes the form:
y~ = X~ β~ + ε .  
Ridge regression estimates the parameters β~ in a penalised least squares sense by finding the b~ that minimizes
X~ b~ - y~ 2 + h b~ 2 ,h>0 ,  
where · denotes the 2-norm and h is a scalar regularization or ridge parameter. For a given value of h, the parameters estimates b~ are found by evaluating
b~ = X~T X~+hI -1 X~T y~ .  
Note that if h=0 the ridge regression solution is equivalent to the ordinary least squares solution.
Rather than calculate the inverse of (X~TX~+hI) directly, nag_correg_ridge (g02kb) uses the singular value decomposition (SVD) of X~. After decomposing X~ into UDVT where U and V are orthogonal matrices and D is a diagonal matrix, the parameter estimates become
b~ = V DTD+hI -1 DUT y~ .  
A consequence of introducing the ridge parameter is that the effective number of parameters, γ, in the model is given by the sum of diagonal elements of
DT D DT D+hI -1 ,  
see Moody (1992) for details.
Any multi-collinearity in the design matrix X may be highlighted by calculating the variance inflation factors for the fitted model. The jth variance inflation factor, vj, is a scaled version of the multiple correlation coefficient between independent variable j and the other independent variables, Rj, and is given by
vj = 1 1-Rj ,j=1,2,,m .  
The m variance inflation factors are calculated as the diagonal elements of the matrix:
X~T X~+hI -1 X~T X~ X~T X~+hI-1 ,  
which, using the SVD of X~, is equivalent to the diagonal elements of the matrix:
V DT D+hI -1 DT D DT D+hI -1 VT .  
Given a value of h, any or all of the following prediction criteria are available:
(a) Generalized cross-validation (GCV):
ns n-γ 2 ;  
(b) Unbiased estimate of variance (UEV):
s n-γ ;  
(c) Future prediction error (FPE):
1n s+ 2γs n-γ ;  
(d) Bayesian information criterion (BIC):
1n s+ lognγs n-γ ;  
(e) Leave-one-out cross-validation (LOOCV),
where s is the sum of squares of residuals.
Although parameter estimates b~ are calculated by using X~, it is usual to report the parameter estimates b associated with X. These are calculated from b~, and the means and scalings of X. Optionally, either b~ or b may be calculated.

References

Hastie T, Tibshirani R and Friedman J (2003) The Elements of Statistical Learning: Data Mining, Inference and Prediction Springer Series in Statistics
Moody J.E. (1992) The effective number of parameters: An analysis of generalisation and regularisation in nonlinear learning systems In: Neural Information Processing Systems (eds J E Moody, S J Hanson, and R P Lippmann) 4 847–854 Morgan Kaufmann San Mateo CA

Parameters

Compulsory Input Parameters

1:     xldxm – double array
ldx, the first dimension of the array, must satisfy the constraint ldxn.
The values of independent variables in the data matrix X.
2:     isxm int64int32nag_int array
Indicates which m independent variables are included in the model.
isxj=1
The jth variable in x will be included in the model.
isxj=0
Variable j is excluded.
Constraint: isxj=0 ​ or ​ 1, for j=1,2,,m.
3:     ip int64int32nag_int scalar
m, the number of independent variables in the model.
Constraints:
  • 1ipm;
  • Exactly ip elements of isx must be equal to 1.
4:     yn – double array
The n values of the dependent variable y.
5:     hlh – double array
hj is the value of the jth ridge parameter h.
Constraint: hj0.0, for j=1,2,,lh.
6:     wantb int64int32nag_int scalar
Defines the options for parameter estimates.
wantb=0
Parameter estimates are not calculated and b is not referenced.
wantb=1
Parameter estimates b are calculated for the original data.
wantb=2
Parameter estimates b~ are calculated for the standardized data.
Constraint: wantb=0, 1 or 2.
7:     wantvf int64int32nag_int scalar
Defines the options for variance inflation factors.
wantvf=0
Variance inflation factors are not calculated and the array vf is not referenced.
wantvf=1
Variance inflation factors are calculated.
Constraints:
  • wantvf=0 or 1;
  • if wantb=0, wantvf=1.
8:     peclpec – cell array of strings
If lpec>0, pecj defines the jth prediction error, for j=1,2,,lpec; otherwise pec is not referenced.
pecj='B'
Bayesian information criterion (BIC).
pecj='F'
Future prediction error (FPE).
pecj='G'
Generalized cross-validation (GCV).
pecj='L'
Leave-one-out cross-validation (LOOCV).
pecj='U'
Unbiased estimate of variance (UEV).
Constraint: if lpec>0, pecj='B', 'F', 'G', 'L' or 'U', for j=1,2,,lpec.

Optional Input Parameters

1:     n int64int32nag_int scalar
Default: the dimension of the array y and the first dimension of the array x. (An error is raised if these dimensions are not equal.)
n, the number of observations.
Constraint: n1.
2:     m int64int32nag_int scalar
Default: the dimension of the array isx and the second dimension of the array x. (An error is raised if these dimensions are not equal.)
The number of independent variables available in the data matrix X.
Constraint: mn.
3:     lh int64int32nag_int scalar
Default: the dimension of the array h.
The number of supplied ridge parameters.
Constraint: lh>0.
4:     lpec int64int32nag_int scalar
Default: the dimension of the array pec.
The number of prediction error statistics to return; set lpec0 for no prediction error estimates.

Output Parameters

1:     neplh – double array
nepj is the number of effective parameters, γ, in the jth model, for j=1,2,,lh.
2:     bldb: – double array
The first dimension, ldb, of the array b will be
  • if wantb0, ldb=ip+1;
  • otherwise ldb=1.
The second dimension of the array b will be lh if wantb0 and 1 otherwise.
If wantb0, b contains the intercept and parameter estimates for the fitted ridge regression model in the order indicated by isx. b1j, for j=1,2,,lh, contains the estimate for the intercept; bi+1j contains the parameter estimate for the ith independent variable in the model fitted with ridge parameter hj, for i=1,2,,ip.
3:     vfldvf: – double array
The first dimension, ldvf, of the array vf will be
  • if wantvf0, ldvf=ip;
  • otherwise ldvf=1.
The second dimension of the array vf will be lh if wantvf0 and 1 otherwise.
If wantvf=1, the variance inflation factors. For the ith independent variable in a model fitted with ridge parameter hj, vfij is the value of vi, for i=1,2,,ip.
4:     peldpe: – double array
The first dimension, ldpe, of the array pe will be
  • if lpec>0, ldpe=lpec;
  • otherwise ldpe=1.
The second dimension of the array pe will be lh if lpec>0 and 1 otherwise.
If lpec0, pe is not referenced; otherwise peij contains the prediction error of criterion peci for the model fitted with ridge parameter hj, for i=1,2,,lpec and j=1,2,,lh.
5:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Errors or warnings detected by the function:
   ifail=1
Constraint: lh>0.
Constraint: mn.
Constraint: n1.
Constraint: wantb=0, 1 or 2.
Constraint: wantvf=0 or 1.
On entry, hi<0 for at least one i.
On entry, peci is invalid for at least one i.
   ifail=2
Constraint: if wantb0, ldbip+1.
Constraint: if wantvf0, ldvfip.
Constraint: ldpelpec.
Constraint: ldxn.
ip does not equal the sum of elements in isx.
On entry, isxi0 or 1 for at least one i.
   ifail=3
On entry, wantb=0 and wantvf=0.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

The accuracy of nag_correg_ridge (g02kb) is closely related to that of the singular value decomposition.

Further Comments

nag_correg_ridge (g02kb) allocates internally max5×n-1,2×ip×ip+n+3×ip+n elements of double precision storage.

Example

This example reads in data from an experiment to model body fat, and a selection of ridge regression models are calculated.
function g02kb_example


fprintf('g02kb example results\n\n');

% Data
x = [19.5, 43.1, 29.1;
     24.7, 49.8, 28.2;
     30.7, 51.9, 37.0;
     29.8, 54.3, 31.1;
     19.1, 42.2, 30.9;
     25.6, 53.9, 23.7;
     31.4, 58.5, 27.6;
     27.9, 52.1, 30.6;
     22.1, 49.9, 23.2;
     25.5, 53.5, 24.8;
     31.1, 56.6, 30.0;
     30.4, 56.7, 28.3;
     18.7, 46.5, 23.0;
     19.7, 44.2, 28.6;
     14.6, 42.7, 21.3;
     29.5, 54.4, 30.1;
     27.7, 55.3, 25.7;
     30.2, 58.6, 24.6;
     22.7, 48.2, 27.1;
     25.2, 51.0, 27.5];

[n,m] = size(x);
isx   = ones(m,1,'int64');
ip    = int64(m);
y = [11.9; 22.8; 18.7; 20.1; 12.9; 21.7; 27.1; 25.4; 21.3; 19.3;
     25.4; 27.2; 11.7; 17.8; 12.8; 23.9; 22.6; 25.4; 14.8; 21.1];

% Ridge coeficients
h = [0;         0.002;     0.004;     0.006;
     0.008;     0.01;      0.012;     0.014;
     0.016;     0.018;     0.02;      0.022;
     0.024;     0.026;     0.028;     0.030];
lh = numel(h);
% Parameters
wantb = int64(1);
wantvf = int64(1);
pec = {'L'; 'G'; 'U'; 'F'; 'B'};

% Fit ridge regression model
[nep, b, vf, pe, ifail] = g02kb( ...
                                 x, isx, ip, y, h, wantb, wantvf, pec);

% Display results
fprintf('Number of parameters used           : %5d\n', ip+1);
fprintf('Number of effective parameters (nep): \n');
fprintf('   Ridge coeff     nep\n');
ivar = double([1:lh]);
fprintf('     %4d      %10.4f\n',[ivar; nep(ivar)']);
fprintf('\nParameter estimates (original scalings)\n');
fprintf(' Ridge coef Intercept');
fprintf('%9d ',[1:ip]);
fprintf('\n');
for j = 1:lh
  fprintf('%10.4f',h(j),b(1:ip+1,j));
  fprintf('\n');
end
% Variance inflation factors
fprintf('\nVariance Inflation Factors\n');
fprintf(' Ridge coef');
fprintf('%9d ',[1:ip]);
fprintf('\n');
for j = 1:lh
  fprintf('%10.4f',h(j),vf(1:ip,j));
  fprintf('\n');
end
% Prediction error criterion
fprintf('\nPrediction error criterion\n');
pl = numel(pec);
fprintf(' Ridge coef');
fprintf('%9d ',[1:pl]);
fprintf('\n');
for j = 1:lh
  fprintf('%10.4f',h(j),pe(1:pl,j));
  fprintf('\n');
  if pl<ip
    fprintf('%10s',' ');
    fprintf('%10.4f',pe(pl+1:ip,j));
    fprintf('\n');
  end
end
fprintf('\nKey:\n');
for i = 1:pl
  switch pec{i}
    case {'L'}
      fprintf(' %5d %s\n', i, 'Leave one out cross-validation');
    case {'G'}
      fprintf(' %5d %s\n', i, 'Generalised cross-validation');
    case {'U'}
      fprintf(' %5d %s\n', i, 'Unbiased estimate of variance');
    case {'F'}
      fprintf(' %5d %s\n', i, 'Final prediction error');
    case {'B'}
      fprintf(' %5d %s\n', i, 'Bayesian information criterion');
  end
end


g02kb example results

Number of parameters used           :     4
Number of effective parameters (nep): 
   Ridge coeff     nep
        1          4.0000
        2          3.2634
        3          3.1475
        4          3.0987
        5          3.0709
        6          3.0523
        7          3.0386
        8          3.0278
        9          3.0189
       10          3.0112
       11          3.0045
       12          2.9984
       13          2.9928
       14          2.9876
       15          2.9828
       16          2.9782

Parameter estimates (original scalings)
 Ridge coef Intercept        1         2         3 
    0.0000  117.0847    4.3341   -2.8568   -2.1861
    0.0020   22.2748    1.4644   -0.4012   -0.6738
    0.0040    7.7209    1.0229   -0.0242   -0.4408
    0.0060    1.8363    0.8437    0.1282   -0.3460
    0.0080   -1.3396    0.7465    0.2105   -0.2944
    0.0100   -3.3219    0.6853    0.2618   -0.2619
    0.0120   -4.6734    0.6432    0.2968   -0.2393
    0.0140   -5.6511    0.6125    0.3222   -0.2228
    0.0160   -6.3891    0.5890    0.3413   -0.2100
    0.0180   -6.9642    0.5704    0.3562   -0.1999
    0.0200   -7.4236    0.5554    0.3681   -0.1916
    0.0220   -7.7978    0.5429    0.3779   -0.1847
    0.0240   -8.1075    0.5323    0.3859   -0.1788
    0.0260   -8.3673    0.5233    0.3926   -0.1737
    0.0280   -8.5874    0.5155    0.3984   -0.1693
    0.0300   -8.7758    0.5086    0.4033   -0.1653

Variance Inflation Factors
 Ridge coef        1         2         3 
    0.0000  708.8429  564.3434  104.6060
    0.0020   50.5592   40.4483    8.2797
    0.0040   16.9816   13.7247    3.3628
    0.0060    8.5033    6.9764    2.1185
    0.0080    5.1472    4.3046    1.6238
    0.0100    3.4855    2.9813    1.3770
    0.0120    2.5434    2.2306    1.2356
    0.0140    1.9581    1.7640    1.1463
    0.0160    1.5698    1.4541    1.0859
    0.0180    1.2990    1.2377    1.0428
    0.0200    1.1026    1.0805    1.0105
    0.0220    0.9556    0.9627    0.9855
    0.0240    0.8427    0.8721    0.9655
    0.0260    0.7541    0.8007    0.9491
    0.0280    0.6832    0.7435    0.9353
    0.0300    0.6257    0.6969    0.9235

Prediction error criterion
 Ridge coef        1         2         3         4         5 
    0.0000    8.0368    7.6879    6.1503    7.3804    8.6052
    0.0020    7.5464    7.4238    6.2124    7.2261    8.2355
    0.0040    7.5575    7.4520    6.2793    7.2675    8.2515
    0.0060    7.5656    7.4668    6.3100    7.2876    8.2611
    0.0080    7.5701    7.4749    6.3272    7.2987    8.2661
    0.0100    7.5723    7.4796    6.3381    7.3053    8.2685
    0.0120    7.5732    7.4823    6.3455    7.3095    8.2695
    0.0140    7.5734    7.4838    6.3508    7.3122    8.2696
    0.0160    7.5731    7.4845    6.3548    7.3140    8.2691
    0.0180    7.5724    7.4848    6.3578    7.3151    8.2683
    0.0200    7.5715    7.4847    6.3603    7.3158    8.2671
    0.0220    7.5705    7.4843    6.3623    7.3161    8.2659
    0.0240    7.5694    7.4838    6.3639    7.3162    8.2645
    0.0260    7.5682    7.4832    6.3654    7.3162    8.2630
    0.0280    7.5669    7.4825    6.3666    7.3161    8.2615
    0.0300    7.5657    7.4818    6.3677    7.3159    8.2600

Key:
     1 Leave one out cross-validation
     2 Generalised cross-validation
     3 Unbiased estimate of variance
     4 Final prediction error
     5 Bayesian information criterion

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

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