G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG02KBF

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

G02KBF calculates a ridge regression, with ridge parameters supplied by you.

## 2  Specification

 SUBROUTINE G02KBF ( N, M, X, LDX, ISX, IP, Y, LH, H, NEP, WANTB, B, LDB, WANTVF, VF, LDVF, LPEC, PEC, PE, LDPE, IFAIL)
 INTEGER N, M, LDX, ISX(M), IP, LH, WANTB, LDB, WANTVF, LDVF, LPEC, LDPE, IFAIL REAL (KIND=nag_wp) X(LDX,M), Y(N), H(LH), NEP(LH), B(LDB,*), VF(LDVF,*), PE(LDPE,*) CHARACTER(1) PEC(LPEC)

## 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 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, G02KBF 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.

## 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}}\ge 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 G02KBF 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:     Y(N) – REAL (KIND=nag_wp) arrayInput
On entry: the $n$ values of the dependent variable $y$.
8:     LH – INTEGERInput
On entry: the number of supplied ridge parameters.
Constraint: ${\mathbf{LH}}>0$.
9:     H(LH) – REAL (KIND=nag_wp) arrayInput
On entry: ${\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}}$.
10:   NEP(LH) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\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}}$.
11:   WANTB – INTEGERInput
On entry: 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$.
12:   B(LDB,$*$) – REAL (KIND=nag_wp) arrayOutput
Note: the second dimension of the array B must be at least ${\mathbf{LH}}$ if ${\mathbf{WANTB}}\ne 0$, and at least $1$ otherwise.
On exit: 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}}$.
13:   LDB – INTEGERInput
On entry: the first dimension of the array B as declared in the (sub)program from which G02KBF is called.
Constraints:
• if ${\mathbf{WANTB}}\ne 0$, ${\mathbf{LDB}}\ge {\mathbf{IP}}+1$;
• otherwise ${\mathbf{LDB}}\ge 1$.
14:   WANTVF – INTEGERInput
On entry: 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$.
15:   VF(LDVF,$*$) – REAL (KIND=nag_wp) arrayOutput
Note: the second dimension of the array VF must be at least ${\mathbf{LH}}$ if ${\mathbf{WANTVF}}\ne 0$, and at least $1$ otherwise.
On exit: 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}}$.
16:   LDVF – INTEGERInput
On entry: the first dimension of the array VF as declared in the (sub)program from which G02KBF is called.
Constraints:
• if ${\mathbf{WANTVF}}\ne 0$, ${\mathbf{LDVF}}\ge {\mathbf{IP}}$;
• otherwise ${\mathbf{LDVF}}\ge 1$.
17:   LPEC – INTEGERInput
On entry: the number of prediction error statistics to return; set ${\mathbf{LPEC}}\le 0$ for no prediction error estimates.
18:   PEC(LPEC) – CHARACTER(1) arrayInput
On entry: 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}}$.
19:   PE(LDPE,$*$) – REAL (KIND=nag_wp) arrayOutput
Note: the second dimension of the array PE must be at least ${\mathbf{LH}}$ if ${\mathbf{LPEC}}>0$, and at least $1$ otherwise.
On exit: 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}}$.
20:   LDPE – INTEGERInput
On entry: the first dimension of the array PE as declared in the (sub)program from which G02KBF is called.
Constraints:
• if ${\mathbf{LPEC}}>0$, ${\mathbf{LDPE}}\ge {\mathbf{LPEC}}$;
• otherwise ${\mathbf{LDPE}}\ge 1$.
21:   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$
 On entry, ${\mathbf{N}}<1$, or ${\mathbf{H}}\left(j\right)<0.0$, or ${\mathbf{LH}}\le 0$, or ${\mathbf{WANTB}}\ne 0$, $1$ or $2$, or ${\mathbf{WANTB}}\ne 0$ and ${\mathbf{LDB}}<{\mathbf{IP}}+1$, or ${\mathbf{WANTVF}}\ne 0$ or $1$, or an element of PEC is not defined.
${\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, or ${\mathbf{WANTVF}}\ne 0$ and ${\mathbf{LDVF}}<{\mathbf{IP}}$, or ${\mathbf{LDPE}}<{\mathbf{LPEC}}$.
${\mathbf{IFAIL}}=3$
Both WANTB and WANTVF are zero.
${\mathbf{IFAIL}}=4$
${\mathbf{IFAIL}}=-999$
Internal memory allocation failed.

## 7  Accuracy

The accuracy of G02KBF is closely related to that of the singular value decomposition.

G02KBF 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 selection of ridge regression models are calculated.

### 9.1  Program Text

Program Text (g02kbfe.f90)

### 9.2  Program Data

Program Data (g02kbfe.d)

### 9.3  Program Results

Program Results (g02kbfe.r)