# NAG FL Interfaceg02kbf (ridge)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g02kbf ( n, m, x, ldx, isx, ip, y, lh, h, nep, b, ldb, vf, ldvf, lpec, pec, pe, ldpe,
 Integer, Intent (In) :: n, m, ldx, isx(m), ip, lh, wantb, ldb, wantvf, ldvf, lpec, ldpe Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: x(ldx,m), y(n), h(lh) Real (Kind=nag_wp), Intent (Inout) :: b(ldb,*), vf(ldvf,*), pe(ldpe,*) Real (Kind=nag_wp), Intent (Out) :: nep(lh) Character (1), Intent (In) :: pec(lpec)
#include <nag.h>
 void g02kbf_ (const Integer *n, const Integer *m, const double x[], const Integer *ldx, const Integer isx[], const Integer *ip, const double y[], const Integer *lh, const double h[], double nep[], const Integer *wantb, double b[], const Integer *ldb, const Integer *wantvf, double vf[], const Integer *ldvf, const Integer *lpec, const char pec[], double pe[], const Integer *ldpe, Integer *ifail, const Charlen length_pec)
The routine may be called by the names g02kbf or nagf_correg_ridge.

## 3Description

A linear model has the form:
 $y = c+Xβ+ε ,$
where
• $y$ is an $n×1$ matrix of values of a dependent variable;
• $c$ is a scalar intercept term;
• $X$ is an $n×m$ matrix of values of independent variables;
• $\beta$ is an $m×1$ matrix of unknown values of parameters;
• $\epsilon$ is an $n×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~TX~+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 (DTD+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~TX~+hI)-1 X~T X~ (X~TX~+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 .$
Given a value of $h$, any or all of the following prediction criteria are available:
1. (a)Generalized cross-validation (GCV):
 $ns (n-γ) 2 ;$
2. (b)Unbiased estimate of variance (UEV):
 $s n-γ ;$
3. (c)Future prediction error (FPE):
 $1n (s+ 2γs n-γ ) ;$
4. (d)Bayesian information criterion (BIC):
 $1n (s+ log(n)γs n-γ ) ;$
5. (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.

## 4References

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

## 5Arguments

1: $\mathbf{n}$Integer Input
On entry: $n$, the number of observations.
Constraint: ${\mathbf{n}}\ge 1$.
2: $\mathbf{m}$Integer Input
On entry: the number of independent variables available in the data matrix $X$.
Constraint: ${\mathbf{m}}\le {\mathbf{n}}$.
3: $\mathbf{x}\left({\mathbf{ldx}},{\mathbf{m}}\right)$Real (Kind=nag_wp) array Input
On entry: the values of independent variables in the data matrix $X$.
4: $\mathbf{ldx}$Integer Input
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: $\mathbf{isx}\left({\mathbf{m}}\right)$Integer array Input
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$ or $1$, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
6: $\mathbf{ip}$Integer Input
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: $\mathbf{y}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: the $n$ values of the dependent variable $y$.
8: $\mathbf{lh}$Integer Input
On entry: the number of supplied ridge parameters.
Constraint: ${\mathbf{lh}}>0$.
9: $\mathbf{h}\left({\mathbf{lh}}\right)$Real (Kind=nag_wp) array Input
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: $\mathbf{nep}\left({\mathbf{lh}}\right)$Real (Kind=nag_wp) array Output
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: $\mathbf{wantb}$Integer Input
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: $\mathbf{b}\left({\mathbf{ldb}},*\right)$Real (Kind=nag_wp) array Output
Note: the second dimension of the array b must be at least ${\mathbf{lh}}$ if ${\mathbf{wantb}}\ne 0$.
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: $\mathbf{ldb}$Integer Input
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: $\mathbf{wantvf}$Integer Input
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: $\mathbf{vf}\left({\mathbf{ldvf}},*\right)$Real (Kind=nag_wp) array Output
Note: the second dimension of the array vf must be at least ${\mathbf{lh}}$ if ${\mathbf{wantvf}}\ne 0$.
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: $\mathbf{ldvf}$Integer Input
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: $\mathbf{lpec}$Integer Input
On entry: the number of prediction error statistics to return; set ${\mathbf{lpec}}\le 0$ for no prediction error estimates.
18: $\mathbf{pec}\left({\mathbf{lpec}}\right)$Character(1) array Input
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: $\mathbf{pe}\left({\mathbf{ldpe}},*\right)$Real (Kind=nag_wp) array Output
Note: the second dimension of the array pe must be at least ${\mathbf{lh}}$ if ${\mathbf{lpec}}>0$.
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: $\mathbf{ldpe}$Integer Input
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: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ 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).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-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{h}}\left(j\right)<0$ for at least one $j$.
Constraint: ${\mathbf{h}}\left(j\right)\le 0.0$, for all $j$.
On entry, ${\mathbf{lh}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{lh}}>0$.
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m}}\le {\mathbf{n}}$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 1$.
On entry, ${\mathbf{pec}}\left(j\right)$ is invalid for at least one $j$.
Constraint: if ${\mathbf{lpec}}>0$, ${\mathbf{pec}}\left(\mathit{j}\right)=\text{'B'}$, $\text{'F'}$, $\text{'G'}$, $\text{'L'}$ or $\text{'U'}$, for all $j$.
On entry, ${\mathbf{wantb}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{wantb}}=0$, $1$ or $2$.
On entry, ${\mathbf{wantvf}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{wantvf}}=0$ or $1$.
${\mathbf{ifail}}=2$
On entry, ip is not equal to the sum of elements in isx.
Constraint: exactly ip elements of isx must be equal to $1$.
On entry, ${\mathbf{isx}}\left(j\right)\ne 0$ or $1$ for at least one $j$.
Constraint: ${\mathbf{isx}}\left(j\right)=0$ or $1$, for all $j$.
On entry, ${\mathbf{ldb}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{ip}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{wantb}}\ne 0$, ${\mathbf{ldb}}\ge {\mathbf{ip}}+1$.
On entry, ${\mathbf{ldpe}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{lpec}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ldpe}}\ge {\mathbf{lpec}}$.
On entry, ${\mathbf{ldvf}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{ip}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{wantvf}}\ne 0$, ${\mathbf{ldvf}}\ge {\mathbf{ip}}$.
On entry, ${\mathbf{ldx}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{n}}$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{wantb}}=0$ and ${\mathbf{wantvf}}=0$.
Constraint: ${\mathbf{wantb}}=0$, ${\mathbf{wantvf}}=1$.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

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

## 8Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
g02kbf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g02kbf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

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.

## 10Example

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

### 10.1Program Text

Program Text (g02kbfe.f90)

### 10.2Program Data

Program Data (g02kbfe.d)

### 10.3Program Results

Program Results (g02kbfe.r)