Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_correg_ridge (g02kb)

## 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
• $y$ is an $n$ by $1$ matrix of values of a dependent variable;
• $c$ is a scalar intercept term;
• $X$ is an $n$ by $m$ matrix of values of independent variables;
• $\beta$ is a $m$ by $1$ matrix of unknown values of parameters;
• $\epsilon$ is an $n$ by $1$ matrix of unknown random errors such that variance of ${\epsilon =\sigma }^{2}I$.
Let $\stackrel{~}{X}$ be the mean-centred $X$ and $\stackrel{~}{y}$ the mean-centred $y$. Furthermore, $\stackrel{~}{X}$ is scaled such that the diagonal elements of the cross product matrix ${\stackrel{~}{X}}^{\mathrm{T}}\stackrel{~}{X}$ are one. The linear model now takes the form:
 $y~ = X~ β~ + ε .$
Ridge regression estimates the parameters $\stackrel{~}{\beta }$ in a penalised least squares sense by finding the $\stackrel{~}{b}$ that minimizes
 $X~ b~ - y~ 2 + h b~ 2 , h>0 ,$
where $‖·‖$ denotes the ${\ell }_{2}$-norm and $h$ is a scalar regularization or ridge parameter. For a given value of $h$, the parameters estimates $\stackrel{~}{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 (${\stackrel{~}{X}}^{\mathrm{T}}\stackrel{~}{X}+hI$) directly, nag_correg_ridge (g02kb) uses the singular value decomposition (SVD) of $\stackrel{~}{X}$. After decomposing $\stackrel{~}{X}$ into $UD{V}^{\mathrm{T}}$ 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, $\gamma$, 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 $j$th variance inflation factor, ${v}_{j}$, is a scaled version of the multiple correlation coefficient between independent variable $j$ and the other independent variables, ${R}_{j}$, 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 $\stackrel{~}{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 $\stackrel{~}{b}$ are calculated by using $\stackrel{~}{X}$, it is usual to report the parameter estimates $b$ associated with $X$. These are calculated from $\stackrel{~}{b}$, and the means and scalings of $X$. Optionally, either $\stackrel{~}{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:     $\mathrm{x}\left(\mathit{ldx},{\mathbf{m}}\right)$ – double array
ldx, the first dimension of the array, must satisfy the constraint $\mathit{ldx}\ge {\mathbf{n}}$.
The values of independent variables in the data matrix $X$.
2:     $\mathrm{isx}\left({\mathbf{m}}\right)$int64int32nag_int array
Indicates which $m$ independent variables are included in the model.
${\mathbf{isx}}\left(j\right)=1$
The $j$th variable in x will be included in the model.
${\mathbf{isx}}\left(j\right)=0$
Variable $j$ is excluded.
Constraint: ${\mathbf{isx}}\left(\mathit{j}\right)=0\text{​ or ​}1$, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
3:     $\mathrm{ip}$int64int32nag_int scalar
$m$, the number of independent variables in the model.
Constraints:
• $1\le {\mathbf{ip}}\le {\mathbf{m}}$;
• Exactly ip elements of isx must be equal to $1$.
4:     $\mathrm{y}\left({\mathbf{n}}\right)$ – double array
The $n$ values of the dependent variable $y$.
5:     $\mathrm{h}\left({\mathbf{lh}}\right)$ – double array
${\mathbf{h}}\left(j\right)$ is the value of the $j$th ridge parameter $h$.
Constraint: ${\mathbf{h}}\left(\mathit{j}\right)\ge 0.0$, for $\mathit{j}=1,2,\dots ,{\mathbf{lh}}$.
6:     $\mathrm{wantb}$int64int32nag_int scalar
Defines the options for parameter estimates.
${\mathbf{wantb}}=0$
Parameter estimates are not calculated and b is not referenced.
${\mathbf{wantb}}=1$
Parameter estimates $b$ are calculated for the original data.
${\mathbf{wantb}}=2$
Parameter estimates $\stackrel{~}{b}$ are calculated for the standardized data.
Constraint: ${\mathbf{wantb}}=0$, $1$ or $2$.
7:     $\mathrm{wantvf}$int64int32nag_int scalar
Defines the options for variance inflation factors.
${\mathbf{wantvf}}=0$
Variance inflation factors are not calculated and the array vf is not referenced.
${\mathbf{wantvf}}=1$
Variance inflation factors are calculated.
Constraints:
• ${\mathbf{wantvf}}=0$ or $1$;
• if ${\mathbf{wantb}}=0$, ${\mathbf{wantvf}}=1$.
8:     $\mathrm{pec}\left({\mathbf{lpec}}\right)$ – cell array of strings
If ${\mathbf{lpec}}>0$, ${\mathbf{pec}}\left(\mathit{j}\right)$ defines the $\mathit{j}$th prediction error, for $\mathit{j}=1,2,\dots ,{\mathbf{lpec}}$; otherwise pec is not referenced.
${\mathbf{pec}}\left(j\right)=\text{'B'}$
Bayesian information criterion (BIC).
${\mathbf{pec}}\left(j\right)=\text{'F'}$
Future prediction error (FPE).
${\mathbf{pec}}\left(j\right)=\text{'G'}$
Generalized cross-validation (GCV).
${\mathbf{pec}}\left(j\right)=\text{'L'}$
Leave-one-out cross-validation (LOOCV).
${\mathbf{pec}}\left(j\right)=\text{'U'}$
Unbiased estimate of variance (UEV).
Constraint: if ${\mathbf{lpec}}>0$, ${\mathbf{pec}}\left(\mathit{j}\right)=\text{'B'}$, $\text{'F'}$, $\text{'G'}$, $\text{'L'}$ or $\text{'U'}$, for $\mathit{j}=1,2,\dots ,{\mathbf{lpec}}$.

### Optional Input Parameters

1:     $\mathrm{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: ${\mathbf{n}}\ge 1$.
2:     $\mathrm{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: ${\mathbf{m}}\le {\mathbf{n}}$.
3:     $\mathrm{lh}$int64int32nag_int scalar
Default: the dimension of the array h.
The number of supplied ridge parameters.
Constraint: ${\mathbf{lh}}>0$.
4:     $\mathrm{lpec}$int64int32nag_int scalar
Default: the dimension of the array pec.
The number of prediction error statistics to return; set ${\mathbf{lpec}}\le 0$ for no prediction error estimates.

### Output Parameters

1:     $\mathrm{nep}\left({\mathbf{lh}}\right)$ – double array
${\mathbf{nep}}\left(\mathit{j}\right)$ is the number of effective parameters, $\gamma$, in the $\mathit{j}$th model, for $\mathit{j}=1,2,\dots ,{\mathbf{lh}}$.
2:     $\mathrm{b}\left(\mathit{ldb},:\right)$ – double array
The first dimension, $\mathit{ldb}$, of the array b will be
• if ${\mathbf{wantb}}\ne 0$, $\mathit{ldb}={\mathbf{ip}}+1$;
• otherwise $\mathit{ldb}=1$.
The second dimension of the array b will be ${\mathbf{lh}}$ if ${\mathbf{wantb}}\ne 0$ and $1$ otherwise.
If ${\mathbf{wantb}}\ne 0$, b contains the intercept and parameter estimates for the fitted ridge regression model in the order indicated by isx. ${\mathbf{b}}\left(1,\mathit{j}\right)$, for $\mathit{j}=1,2,\dots ,{\mathbf{lh}}$, contains the estimate for the intercept; ${\mathbf{b}}\left(\mathit{i}+1,j\right)$ contains the parameter estimate for the $\mathit{i}$th independent variable in the model fitted with ridge parameter ${\mathbf{h}}\left(j\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{ip}}$.
3:     $\mathrm{vf}\left(\mathit{ldvf},:\right)$ – double array
The first dimension, $\mathit{ldvf}$, of the array vf will be
• if ${\mathbf{wantvf}}\ne 0$, $\mathit{ldvf}={\mathbf{ip}}$;
• otherwise $\mathit{ldvf}=1$.
The second dimension of the array vf will be ${\mathbf{lh}}$ if ${\mathbf{wantvf}}\ne 0$ and $1$ otherwise.
If ${\mathbf{wantvf}}=1$, the variance inflation factors. For the $\mathit{i}$th independent variable in a model fitted with ridge parameter ${\mathbf{h}}\left(j\right)$, ${\mathbf{vf}}\left(\mathit{i},j\right)$ is the value of ${v}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{ip}}$.
4:     $\mathrm{pe}\left(\mathit{ldpe},:\right)$ – double array
The first dimension, $\mathit{ldpe}$, of the array pe will be
• if ${\mathbf{lpec}}>0$, $\mathit{ldpe}={\mathbf{lpec}}$;
• otherwise $\mathit{ldpe}=1$.
The second dimension of the array pe will be ${\mathbf{lh}}$ if ${\mathbf{lpec}}>0$ and $1$ otherwise.
If ${\mathbf{lpec}}\le 0$, pe is not referenced; otherwise ${\mathbf{pe}}\left(\mathit{i},\mathit{j}\right)$ contains the prediction error of criterion ${\mathbf{pec}}\left(\mathit{i}\right)$ for the model fitted with ridge parameter ${\mathbf{h}}\left(\mathit{j}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{lpec}}$ and $\mathit{j}=1,2,\dots ,{\mathbf{lh}}$.
5:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
Constraint: ${\mathbf{lh}}>0$.
Constraint: ${\mathbf{m}}\le {\mathbf{n}}$.
Constraint: ${\mathbf{n}}\ge 1$.
Constraint: ${\mathbf{wantb}}=0$, $1$ or $2$.
Constraint: ${\mathbf{wantvf}}=0$ or $1$.
On entry, ${\mathbf{h}}\left(i\right)<0$ for at least one $i$.
On entry, ${\mathbf{pec}}\left(i\right)$ is invalid for at least one $i$.
${\mathbf{ifail}}=2$
Constraint: if ${\mathbf{wantb}}\ne 0$, $\mathit{ldb}\ge {\mathbf{ip}}+1$.
Constraint: if ${\mathbf{wantvf}}\ne 0$, $\mathit{ldvf}\ge {\mathbf{ip}}$.
Constraint: $\mathit{ldpe}\ge {\mathbf{lpec}}$.
Constraint: $\mathit{ldx}\ge {\mathbf{n}}$.
ip does not equal the sum of elements in isx.
On entry, ${\mathbf{isx}}\left(i\right)\ne 0$ or $1$ for at least one $i$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{wantb}}=0$ and ${\mathbf{wantvf}}=0$.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

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

nag_correg_ridge (g02kb) allocates internally $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(5×\left({\mathbf{n}}-1\right),2×{\mathbf{ip}}×{\mathbf{ip}}\right)+\left({\mathbf{n}}+3\right)×{\mathbf{ip}}+{\mathbf{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
```