NAG CL Interface
g05pvc (kfold_​xyw)

1 Purpose

g05pvc generates training and validation datasets suitable for use in cross-validation or jack-knifing.

2 Specification

#include <nag.h>
void  g05pvc (Integer k, Integer fold, Integer n, Integer m, Nag_DataByObsOrVar sordx, double x[], Integer pdx, double y[], double w[], Integer *nt, Integer state[], NagError *fail)
The function may be called by the names: g05pvc or nag_rand_kfold_xyw.

3 Description

Let Xo denote a matrix of n observations on m variables and yo and wo each denote a vector of length n. For example, Xo might represent a matrix of independent variables, yo the dependent variable and wo the associated weights in a weighted regression.
g05pvc generates a series of training datasets, denoted by the matrix, vector, vector triplet Xt,yt,wt of nt observations, and validation datasets, denoted Xv,yv,wv with nv 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 kth 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 leave-one-out cross-validation, or the training dataset on its own for jack-knifing. When using Kn the resulting datasets are suitable for K-fold cross-validation. Datasets suitable for reversed cross-validation can be obtained by switching the training and validation datasets, i.e., use the kth group as the training dataset and the rest of the data as the validation dataset.
One of the initialization functions g05kfc (for a repeatable sequence if computed sequentially) or g05kgc (for a non-repeatable sequence) must be called prior to the first call to g05pvc.

4 References

None.

5 Arguments

1: k Integer Input
On entry: K, the number of folds.
Constraint: 2kn.
2: fold Integer Input
On entry: the number of the fold to return as the validation dataset.
On the first call to g05pvc 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: 1foldk.
3: n Integer Input
On entry: n, the number of observations.
Constraint: n1.
4: m Integer Input
On entry: m, the number of variables.
Constraint: m1.
5: sordx Nag_DataByObsOrVar Input
On entry: determines how variables are stored in x.
Constraint: sordx=Nag_DataByVar or Nag_DataByObs.
6: x[dim] double Input/Output
Note: the dimension, dim, of the array x must be at least
  • pdx×m when sordx=Nag_DataByVar;
  • pdx×n when sordx=Nag_DataByObs.
The way the data is stored in x is defined by sordx.
If sordx=Nag_DataByVar, x[j-1×pdx+i-1] contains the ith observation for the jth variable, for i=1,2,,n and j=1,2,,m.
If sordx=Nag_DataByObs, x[i-1×pdx+j-1] contains the ith observation for the jth variable, for i=1,2,,n and j=1,2,,m.
On entry: if fold=1, x must hold Xo, the values of X for the original dataset, otherwise, x must not be changed since the last call to g05pvc.
On exit: values of X for the training and validation datasets, with Xt held in observations 1 to nt and Xv in observations nt+1 to n.
7: pdx Integer Input
On entry: the stride separating row elements in the two-dimensional data stored in the array x.
Constraints:
  • if sordx=Nag_DataByObs, pdxm;
  • otherwise pdxn.
8: y[dim] double Input/Output
Note: the dimension, dim, of the array y must be at least
  • n, when yis notNULL;
  • otherwise y is not referenced and may be NULL.
If the original dataset does not include yo then y must be set to NULL.
On entry: if fold1, y must not be changed since the last call to g05pvc.
On exit: values of y for the training and validation datasets, with yt held in elements 1 to nt and yv in elements nt+1 to n.
9: w[dim] double Input/Output
Note: the dimension, dim, of the array w must be at least
  • n, when wis notNULL;
  • otherwise w is not referenced and may be NULL.
If the original dataset does not include wo then w must be set to NULL.
On entry: if fold1, w must not be changed since the last call to g05pvc.
On exit: values of w for the training and validation datasets, with wt held in elements 1 to nt and wv in elements nt+1 to n.
10: nt Integer * Output
On exit: nt, the number of observations in the training dataset.
11: state[dim] Integer Communication Array
Note: the dimension, dim, of this array is dictated by the requirements of associated functions that must have been previously called. This array MUST be the same array passed as argument state in the previous call to nag_rand_init_repeatable (g05kfc) or nag_rand_init_nonrepeatable (g05kgc).
On entry: contains information on the selected base generator and its current state.
On exit: contains updated information on the state of the generator.
12: fail NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

6 Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
NE_ARRAY_SIZE
On entry, pdx=value and m=value.
Constraint: if sordx=Nag_DataByObs, pdxm.
On entry, pdx=value and n=value.
Constraint: if sordx=Nag_DataByVar, pdxn.
NE_BAD_PARAM
On entry, argument value had an illegal value.
NE_INT
On entry, m=value.
Constraint: m1.
On entry, n=value.
Constraint: n1.
NE_INT_2
On entry, fold=value and k=value.
Constraint: 1foldk.
On entry, k=value and n=value.
Constraint: 2kn.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_INVALID_STATE
On entry, state vector has been corrupted or not initialized.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NW_POTENTIAL_PROBLEM
More than 50% of the data did not move when the data was shuffled. value of the value observations stayed put.

7 Accuracy

Not applicable.

8 Parallelism and Performance

g05pvc is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g05pvc 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 function. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9 Further Comments

g05pvc will be computationality more efficient if each observation in x is contiguous, that is sordx=Nag_DataByObs.
Because of the way g05pvc stores the data you should usually generate the K training and validation datasets in order, i.e., set 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 cross-validation analyses on different threads. This is possible, as long as the following is borne in mind:
For example, if you have three threads, you would call g05pvc once with fold=1. You would then copy the x returned onto each thread and generate the remaing k-1 sets of data by splitting them between the threads. For example, the first thread runs with fold=2,,L1, the second with fold=L1+1,,L2 and the third with fold=L2+1,,k.

10 Example

This example uses g05pvc to facilitate K-fold cross-validation.
A set of simulated data is split into 5 training and validation datasets. g02gbc is used to fit a logistic regression model to each training dataset and then g02gpc 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 (g05pvce.c)

10.2 Program Data

Program Data (g05pvce.d)

10.3 Program Results

Program Results (g05pvce.r)