G02KAF (PDF version)
G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG02KAF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G02KAF calculates a ridge regression, optimizing the ridge parameter according to one of four prediction error criteria.

## 2  Specification

 SUBROUTINE G02KAF ( N, M, X, LDX, ISX, IP, TAU, Y, H, OPT, NITER, TOL, NEP, ORIG, B, VIF, RES, RSS, DF, OPTLOO, PERR, IFAIL)
 INTEGER N, M, LDX, ISX(M), IP, OPT, NITER, ORIG, DF, OPTLOO, IFAIL REAL (KIND=nag_wp) X(LDX,M), TAU, Y(N), H, TOL, NEP, B(IP+1), VIF(IP), RES(N), RSS, PERR(5)

## 3  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 an $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 parameter 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, G02KAF 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 D UT 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 DTD+hI -1 DT D DTD+hI -1 VT .$
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.
The method can adopt one of four criteria to minimize while calculating a suitable value for $h$:
(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-γ ;$
where $s$ is the sum of squares of residuals. However, the function returns all four of the above prediction errors regardless of the one selected to minimize the ridge parameter, $h$. Furthermore, the function will optionally return the leave-one-out cross-validation (LOOCV) prediction error.

## 4  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

## 5  Parameters

1:     N – INTEGERInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{N}}>1$.
2:     M – INTEGERInput
On entry: the number of independent variables available in the data matrix $X$.
Constraint: ${\mathbf{M}}\le {\mathbf{N}}$.
3:     X(LDX,M) – REAL (KIND=nag_wp) arrayInput
On entry: the values of independent variables in the data matrix $X$.
4:     LDX – INTEGERInput
On entry: the first dimension of the array X as declared in the (sub)program from which G02KAF is called.
Constraint: ${\mathbf{LDX}}\ge {\mathbf{N}}$.
5:     ISX(M) – INTEGER arrayInput
On entry: 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}}$.
6:     IP – INTEGERInput
On entry: $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$.
7:     TAU – REAL (KIND=nag_wp)Input
On entry: singular values less than TAU of the SVD of the data matrix $X$ will be set equal to zero.
Suggested value: ${\mathbf{TAU}}=0.0$
Constraint: ${\mathbf{TAU}}\ge 0.0$.
8:     Y(N) – REAL (KIND=nag_wp) arrayInput
On entry: the $n$ values of the dependent variable $y$.
9:     H – REAL (KIND=nag_wp)Input/Output
On entry: an initial value for the ridge regression parameter $h$; used as a starting point for the optimization.
Constraint: ${\mathbf{H}}>0.0$.
On exit: H is the optimized value of the ridge regression parameter $h$.
10:   OPT – INTEGERInput
On entry: the measure of prediction error used to optimize the ridge regression parameter $h$. The value of OPT must be set equal to one of:
${\mathbf{OPT}}=1$
Generalized cross-validation (GCV);
${\mathbf{OPT}}=2$
Unbiased estimate of variance (UEV)
${\mathbf{OPT}}=3$
Future prediction error (FPE)
${\mathbf{OPT}}=4$
Bayesian information criteron (BIC).
Constraint: ${\mathbf{OPT}}=1$, $2$, $3$ or $4$.
11:   NITER – INTEGERInput/Output
On entry: the maximum number of iterations allowed to optimize the ridge regression parameter $h$.
Constraint: ${\mathbf{NITER}}\ge 1$.
On exit: the number of iterations used to optimize the ridge regression parameter $h$ within TOL.
12:   TOL – REAL (KIND=nag_wp)Input
On entry: iterations of the ridge regression parameter $h$ will halt when consecutive values of $h$ lie within TOL.
Constraint: ${\mathbf{TOL}}>0.0$.
13:   NEP – REAL (KIND=nag_wp)Output
On exit: the number of effective parameters, $\gamma$, in the model.
14:   ORIG – INTEGERInput
On entry: if ${\mathbf{ORIG}}=1$, the parameter estimates $b$ are calculated for the original data; otherwise ${\mathbf{ORIG}}=2$ and the parameter estimates $\stackrel{~}{b}$ are calculated for the standardized data.
Constraint: ${\mathbf{ORIG}}=1$ or $2$.
15:   B(${\mathbf{IP}}+1$) – REAL (KIND=nag_wp) arrayOutput
On exit: contains the intercept and parameter estimates for the fitted ridge regression model in the order indicated by ISX. The first element of B contains the estimate for the intercept; ${\mathbf{B}}\left(\mathit{j}+1\right)$ contains the parameter estimate for the $\mathit{j}$th independent variable in the model, for $\mathit{j}=1,2,\dots ,{\mathbf{IP}}$.
16:   VIF(IP) – REAL (KIND=nag_wp) arrayOutput
On exit: the variance inflation factors in the order indicated by ISX. For the $\mathit{j}$th independent variable in the model, ${\mathbf{VIF}}\left(\mathit{j}\right)$ is the value of ${v}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,{\mathbf{IP}}$.
17:   RES(N) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{RES}}\left(\mathit{i}\right)$ is the value of the $\mathit{i}$th residual for the fitted ridge regression model, for $\mathit{i}=1,2,\dots ,{\mathbf{N}}$.
18:   RSS – REAL (KIND=nag_wp)Output
On exit: the sum of squares of residual values.
19:   DF – INTEGEROutput
On exit: the degrees of freedom for the residual sum of squares RSS.
20:   OPTLOO – INTEGERInput
On entry: if ${\mathbf{OPTLOO}}=2$, the leave-one-out cross-validation estimate of prediction error is calculated; otherwise no such estimate is calculated and ${\mathbf{OPTLOO}}=1$.
Constraint: ${\mathbf{OPTLOO}}=1$ or $2$.
21:   PERR($5$) – REAL (KIND=nag_wp) arrayOutput
On exit: the first four elements contain, in this order, the measures of prediction error: GCV, UEV, FPE and BIC.
If ${\mathbf{OPTLOO}}=2$, ${\mathbf{PERR}}\left(5\right)$ is the LOOCV estimate of prediction error; otherwise ${\mathbf{PERR}}\left(5\right)$ is not referenced.
22:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=-1$
Maximum number of iterations used.
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{N}}\le 1$; or ${\mathbf{TAU}}<0.0$; or ${\mathbf{OPT}}\ne 1$, $2$, $3$ or $4$; or ${\mathbf{H}}\le 0.0$; or ${\mathbf{OPTLOO}}\ne 1$ or $2$; or ${\mathbf{TOL}}\le 0.0$; or ${\mathbf{NITER}}<1$; or ${\mathbf{ORIG}}\ne 1$ or $2$
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{M}}>{\mathbf{N}}$; or ${\mathbf{LDX}}<{\mathbf{N}}$; or ${\mathbf{IP}}<1$ or ${\mathbf{IP}}>{\mathbf{M}}$; or An element of ${\mathbf{ISX}}\ne 0$ or $1$; or IP does not equal the sum of elements in ISX.
${\mathbf{IFAIL}}=3$
SVD failed to converge.
${\mathbf{IFAIL}}=4$
Internal error. Check all array sizes and calls to G02KAF. Please contact NAG.

Not applicable.

## 8  Further Comments

G02KAF 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.

## 9  Example

This example reads in data from an experiment to model body fat, and a ridge regression is calculated that optimizes GCV prediction error.

### 9.1  Program Text

Program Text (g02kafe.f90)

### 9.2  Program Data

Program Data (g02kafe.d)

### 9.3  Program Results

Program Results (g02kafe.r)

G02KAF (PDF version)
G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual