NAG Library Routine Document
G05PVF
1 Purpose
G05PVF generates training and validation datasets suitable for use in crossvalidation or jackknifing.
2 Specification
SUBROUTINE G05PVF ( 
K, FOLD, N, M, SORDX, X, LDX, USEY, Y, USEW, W, NT, STATE, IFAIL) 
INTEGER 
K, FOLD, N, M, SORDX, LDX, USEY, USEW, NT, STATE(*), IFAIL 
REAL (KIND=nag_wp) 
X(LDX,*), Y(*), W(*) 

3 Description
Let ${X}_{o}$ denote a matrix of $n$ observations on $m$ variables and ${y}_{o}$ and ${w}_{o}$ each denote a vector of length $n$. For example, ${X}_{o}$ might represent a matrix of independent variables, ${y}_{o}$ the dependent variable and ${w}_{o}$ the associated weights in a weighted regression.
G05PVF generates a series of training datasets, denoted by the matrix, vector, vector triplet $\left({X}_{t},{y}_{t},{w}_{t}\right)$ of ${n}_{t}$ observations, and validation datasets, denoted $\left({X}_{v},{y}_{v},{w}_{v}\right)$ with ${n}_{v}$ observations. These training and validation datasets are generated as follows.
Each of the original $n$ observations is randomly assigned to one of $K$ equally sized groups or folds. For the $k$th sample the validation dataset consists of those observations in group $k$ and the training dataset consists of all those observations not in group $k$. Therefore at most $K$ samples can be generated.
If $n$ is not divisible by $K$ then the observations are assigned to groups as evenly as possible, therefore any group will be at most one observation larger or smaller than any other group.
When using $K=n$ the resulting datasets are suitable for leaveoneout crossvalidation, or the training dataset on its own for jackknifing. When using $K\ne n$ the resulting datasets are suitable for $K$fold crossvalidation. Datasets suitable for reversed crossvalidation can be obtained by switching the training and validation datasets, i.e., use the $k$th group as the training dataset and the rest of the data as the validation dataset.
One of the initialization routines
G05KFF (for a repeatable sequence if computed sequentially) or
G05KGF (for a nonrepeatable sequence) must be called prior to the first call to G05PVF.
4 References
None.
5 Parameters
 1: $\mathrm{K}$ – INTEGERInput

On entry: $K$, the number of folds.
Constraint:
$2\le {\mathbf{K}}\le {\mathbf{N}}$.
 2: $\mathrm{FOLD}$ – INTEGERInput

On entry: the number of the fold to return as the validation dataset.
On the first call to G05PVF
${\mathbf{FOLD}}$ should be set to
$1$ and then incremented by one at each subsequent call until all
$K$ sets of training and validation datasets have been produced. See
Section 9 for more details on how a different calling sequence can be used.
Constraint:
$1\le {\mathbf{FOLD}}\le {\mathbf{K}}$.
 3: $\mathrm{N}$ – INTEGERInput

On entry: $n$, the number of observations.
Constraint:
${\mathbf{N}}\ge 1$.
 4: $\mathrm{M}$ – INTEGERInput

On entry: $m$, the number of variables.
Constraint:
${\mathbf{M}}\ge 1$.
 5: $\mathrm{SORDX}$ – INTEGERInput

On entry: determines how variables are stored in
X.
Constraint:
${\mathbf{SORDX}}=1$ or $2$.
 6: $\mathrm{X}\left({\mathbf{LDX}},*\right)$ – REAL (KIND=nag_wp) arrayInput/Output

Note: the second dimension of the array
X
must be at least
${\mathbf{M}}$ if
${\mathbf{SORDX}}=1$ and at least
${\mathbf{N}}$ if
${\mathbf{SORDX}}=2$.
The way the data is stored in
X is defined by
SORDX.
If ${\mathbf{SORDX}}=1$, ${\mathbf{X}}\left(\mathit{i},\mathit{j}\right)$ contains the $\mathit{i}$th observation for the $\mathit{j}$th variable, for $i=1,2,\dots ,{\mathbf{N}}$ and $j=1,2,\dots ,{\mathbf{M}}$.
If ${\mathbf{SORDX}}=2$, ${\mathbf{X}}\left(\mathit{j},\mathit{i}\right)$ contains the $\mathit{i}$th observation for the $\mathit{j}$th variable, for $i=1,2,\dots ,{\mathbf{N}}$ and $j=1,2,\dots ,{\mathbf{M}}$.
On entry: if
${\mathbf{FOLD}}=1$,
X must hold
${X}_{o}$, the values of
$X$ for the original dataset, otherwise,
X must not be changed since the last call to G05PVF.
On exit: values of $X$ for the training and validation datasets, with ${X}_{t}$ held in observations $1$ to ${\mathbf{NT}}$ and ${X}_{v}$ in observations ${\mathbf{NT}}+1$ to ${\mathbf{N}}$.
 7: $\mathrm{LDX}$ – INTEGERInput

On entry: the first dimension of the array
X as declared in the (sub)program from which G05PVF is called.
Constraints:
 if ${\mathbf{SORDX}}=2$, ${\mathbf{LDX}}\ge {\mathbf{M}}$;
 otherwise ${\mathbf{LDX}}\ge {\mathbf{N}}$.
 8: $\mathrm{USEY}$ – INTEGERInput

On entry: if ${\mathbf{USEY}}=1$, the original dataset includes ${y}_{o}$ and ${y}_{o}$ will be processed alongside ${X}_{o}$.
Constraint:
${\mathbf{USEY}}=0$ or $1$.
 9: $\mathrm{Y}\left(*\right)$ – REAL (KIND=nag_wp) arrayInput/Output

Note: the dimension of the array
Y
must be at least
${\mathbf{N}}$ if
${\mathbf{USEY}}=1$.
If
${\mathbf{USEY}}=0$,
Y is not referenced on entry and will not be modified on exit.
On entry: if
${\mathbf{FOLD}}=1$,
Y must hold
${y}_{o}$, the values of
$y$ for the original dataset, otherwise
Y must not be changed since the last call to G05PVF.
On exit: values of $y$ for the training and validation datasets, with ${y}_{t}$ held in elements $1$ to ${\mathbf{NT}}$ and ${y}_{v}$ in elements ${\mathbf{NT}}+1$ to ${\mathbf{N}}$.
 10: $\mathrm{USEW}$ – INTEGERInput

On entry: if ${\mathbf{USEW}}=1$, the original dataset includes ${w}_{o}$ and ${w}_{o}$ will be processed alongside ${X}_{o}$.
Constraint:
${\mathbf{USEW}}=0$ or $1$.
 11: $\mathrm{W}\left(*\right)$ – REAL (KIND=nag_wp) arrayInput/Output

Note: the dimension of the array
W
must be at least
${\mathbf{N}}$ if
${\mathbf{USEW}}=1$.
If
${\mathbf{USEW}}=0$,
W is not referenced on entry and will not be modified on exit.
On entry: if
${\mathbf{FOLD}}=1$,
W must hold
${w}_{o}$, the values of
$w$ for the original dataset, otherwise
W must not be changed since the last call to G05PVF.
On exit: values of $w$ for the training and validation datasets, with ${w}_{t}$ held in elements $1$ to ${\mathbf{NT}}$ and ${w}_{v}$ in elements ${\mathbf{NT}}+1$ to ${\mathbf{N}}$.
 12: $\mathrm{NT}$ – INTEGEROutput

On exit: ${n}_{t}$, the number of observations in the training dataset.
 13: $\mathrm{STATE}\left(*\right)$ – INTEGER arrayCommunication Array

Note: the actual argument supplied
must be the array
STATE supplied to the initialization routines
G05KFF or
G05KGF.
On entry: contains information on the selected base generator and its current state.
On exit: contains updated information on the state of the generator.
 14: $\mathrm{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, because for this routine the values of the output parameters may be useful even if
${\mathbf{IFAIL}}\ne {\mathbf{0}}$ on exit, the recommended value is
$1$.
When the value $\mathbf{1}\text{ or}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).
Note: G05PVF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
 ${\mathbf{IFAIL}}=11$

On entry, ${\mathbf{K}}=\u2329\mathit{\text{value}}\u232a$ and ${\mathbf{N}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: $2\le {\mathbf{K}}\le {\mathbf{N}}$.
 ${\mathbf{IFAIL}}=21$

On entry, ${\mathbf{FOLD}}=\u2329\mathit{\text{value}}\u232a$ and ${\mathbf{K}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: $1\le {\mathbf{FOLD}}\le {\mathbf{K}}$.
 ${\mathbf{IFAIL}}=31$

On entry, ${\mathbf{N}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{N}}\ge 1$.
 ${\mathbf{IFAIL}}=41$

On entry, ${\mathbf{M}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{M}}\ge 1$.
 ${\mathbf{IFAIL}}=51$

On entry, ${\mathbf{SORDX}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{SORDX}}=1$ or $2$.
 ${\mathbf{IFAIL}}=61$

More than $50\%$ of the data did not move when the data was shuffled. $\u2329\mathit{\text{value}}\u232a$ of the $\u2329\mathit{\text{value}}\u232a$ observations stayed put.
 ${\mathbf{IFAIL}}=71$

On entry, ${\mathbf{LDX}}=\u2329\mathit{\text{value}}\u232a$ and ${\mathbf{N}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: if ${\mathbf{SORDX}}=1$, ${\mathbf{LDX}}\ge {\mathbf{N}}$.
 ${\mathbf{IFAIL}}=72$

On entry, ${\mathbf{LDX}}=\u2329\mathit{\text{value}}\u232a$ and ${\mathbf{M}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: if ${\mathbf{SORDX}}=2$, ${\mathbf{LDX}}\ge {\mathbf{M}}$.
 ${\mathbf{IFAIL}}=81$

Constraint: ${\mathbf{USEY}}=0$ or $1$.
 ${\mathbf{IFAIL}}=101$

Constraint: ${\mathbf{USEW}}=0$ or $1$.
 ${\mathbf{IFAIL}}=131$

On entry,
STATE vector has been corrupted or not initialized.
 ${\mathbf{IFAIL}}=99$
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 3.8 in the Essential Introduction for further information.
 ${\mathbf{IFAIL}}=399$
Your licence key may have expired or may not have been installed correctly.
See
Section 3.7 in the Essential Introduction for further information.
 ${\mathbf{IFAIL}}=999$
Dynamic memory allocation failed.
See
Section 3.6 in the Essential Introduction for further information.
7 Accuracy
Not applicable.
8 Parallelism and Performance
G05PVF is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
G05PVF 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 implementationspecific information.
G05PVF will be computationality more efficient if each observation in
X is contiguous, that is
${\mathbf{SORDX}}=2$.
Because of the way G05PVF stores the data you should usually generate the
$K$ training and validation datasets in order, i.e., set
${\mathbf{FOLD}}=1$ on the first call and increment it by one at each subsequent call. However, there are times when a different calling sequence would be beneficial, for example, when performing different crossvalidation analyses on different threads. This is possible, as long as the following is borne in mind:
 G05PVF must be called with ${\mathbf{FOLD}}=1$ first.
 Other than the first set, you can obtain the training and validation dataset in any order, but for a given X you can only obtain each once.
For example, if you have three threads, you would call G05PVF once with
${\mathbf{FOLD}}=1$. You would then copy the
X returned onto each thread and generate the remaing
${\mathbf{K}}1$ sets of data by splitting them between the threads. For example, the first thread runs with
${\mathbf{FOLD}}=2,\dots ,{L}_{1}$, the second with
${\mathbf{FOLD}}={L}_{1}+1,\dots ,{L}_{2}$ and the third with
${\mathbf{FOLD}}={L}_{2}+1,\dots ,{\mathbf{K}}$.
10 Example
This example uses G05PVF to facilitate $K$fold crossvalidation.
A set of simulated data is split into
$5$ training and validation datasets.
G02GBF is used to fit a logistic regression model to each training dataset and then
G02GPF is used to predict the response for the observations in the validation dataset.
The counts of true and false positives and negatives along with the sensitivity and specificity is then reported.
10.1 Program Text
Program Text (g05pvfe.f90)
10.2 Program Data
Program Data (g05pvfe.d)
10.3 Program Results
Program Results (g05pvfe.r)