NAG Library Function Document
nag_rand_kfold_xyw (g05pvc)
1
Purpose
nag_rand_kfold_xyw (g05pvc) generates training and validation datasets suitable for use in cross-validation or jack-knifing.
2
Specification
#include <nag.h> |
#include <nagg05.h> |
void |
nag_rand_kfold_xyw (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) |
|
3
Description
Let denote a matrix of observations on variables and and each denote a vector of length . For example, might represent a matrix of independent variables, the dependent variable and the associated weights in a weighted regression.
nag_rand_kfold_xyw (g05pvc) generates a series of training datasets, denoted by the matrix, vector, vector triplet of observations, and validation datasets, denoted with observations. These training and validation datasets are generated as follows.
Each of the original observations is randomly assigned to one of equally sized groups or folds. For the th sample the validation dataset consists of those observations in group and the training dataset consists of all those observations not in group . Therefore at most samples can be generated.
If is not divisible by 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 the resulting datasets are suitable for leave-one-out cross-validation, or the training dataset on its own for jack-knifing. When using the resulting datasets are suitable for -fold cross-validation. Datasets suitable for reversed cross-validation can be obtained by switching the training and validation datasets, i.e., use the th group as the training dataset and the rest of the data as the validation dataset.
One of the initialization functions
nag_rand_init_repeatable (g05kfc) (for a repeatable sequence if computed sequentially) or
nag_rand_init_nonrepeatable (g05kgc) (for a non-repeatable sequence) must be called prior to the first call to
nag_rand_kfold_xyw (g05pvc).
4
References
None.
5
Arguments
- 1:
– IntegerInput
-
On entry: , the number of folds.
Constraint:
.
- 2:
– IntegerInput
-
On entry: the number of the fold to return as the validation dataset.
On the first call to
nag_rand_kfold_xyw (g05pvc) should be set to
and then incremented by one at each subsequent call until all
sets of training and validation datasets have been produced. See
Section 8 for more details on how a different calling sequence can be used.
Constraint:
.
- 3:
– IntegerInput
-
On entry: , the number of observations.
Constraint:
.
- 4:
– IntegerInput
-
On entry: , the number of variables.
Constraint:
.
- 5:
– Nag_DataByObsOrVarInput
-
On entry: determines how variables are stored in
x.
Constraint:
or .
- 6:
– doubleInput/Output
-
Note: the dimension,
dim, of the array
x
must be at least
- when
;
- when
.
The way the data is stored in
x is defined by
sordx.
If , contains the th observation for the th variable, for and .
If , contains the th observation for the th variable, for and .
On entry: if
,
x must hold
, the values of
for the original dataset, otherwise,
x must not be changed since the last call to
nag_rand_kfold_xyw (g05pvc).
On exit: values of for the training and validation datasets, with held in observations to and in observations to .
- 7:
– IntegerInput
-
On entry: the stride separating row elements in the two-dimensional data stored in the array
x.
Constraints:
- if , ;
- otherwise .
- 8:
– doubleInput/Output
-
If the original dataset does not include
then
y must be set to
NULL.
On entry: if
,
y must not be changed since the last call to
nag_rand_kfold_xyw (g05pvc).
On exit: values of for the training and validation datasets, with held in elements to and in elements to .
- 9:
– doubleInput/Output
-
If the original dataset does not include
then
w must be set to
NULL.
On entry: if
,
w must not be changed since the last call to
nag_rand_kfold_xyw (g05pvc).
On exit: values of for the training and validation datasets, with held in elements to and in elements to .
- 10:
– Integer *Output
-
On exit: , the number of observations in the training dataset.
- 11:
– IntegerCommunication Array
-
Note: the dimension,
, 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:
– NagError *Input/Output
-
The NAG error argument (see
Section 3.7 in How to Use the NAG Library and its Documentation).
6
Error Indicators and Warnings
- NE_ALLOC_FAIL
-
Dynamic memory allocation failed.
See
Section 2.3.1.2 in How to Use the NAG Library and its Documentation for further information.
- NE_ARRAY_SIZE
-
On entry, and .
Constraint: if , .
On entry, and .
Constraint: if , .
- NE_BAD_PARAM
-
On entry, argument had an illegal value.
- NE_INT
-
On entry, .
Constraint: .
On entry, .
Constraint: .
- NE_INT_2
-
On entry, and .
Constraint: .
On entry, and .
Constraint: .
- 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 2.7.6 in How to Use the NAG Library and its Documentation 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 2.7.5 in How to Use the NAG Library and its Documentation for further information.
- NW_POTENTIAL_PROBLEM
-
More than of the data did not move when the data was shuffled. of the observations stayed put.
7
Accuracy
Not applicable.
nag_rand_kfold_xyw (g05pvc) will be computationality more efficient if each observation in
x is contiguous, that is
.
Because of the way
nag_rand_kfold_xyw (g05pvc) stores the data you should usually generate the
training and validation datasets in order, i.e., set
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:
- nag_rand_kfold_xyw (g05pvc) must be called with 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
nag_rand_kfold_xyw (g05pvc) once with
. You would then copy the
x returned onto each thread and generate the remaing
sets of data by splitting them between the threads. For example, the first thread runs with
, the second with
and the third with
.
9
Example
This example uses nag_rand_kfold_xyw (g05pvc) to facilitate -fold cross-validation.
A set of simulated data is split into
training and validation datasets.
nag_glm_binomial (g02gbc) is used to fit a logistic regression model to each training dataset and then
nag_glm_predict (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.
9.1
Program Text
Program Text (g05pvce.c)
9.2
Program Data
Program Data (g05pvce.d)
9.3
Program Results
Program Results (g05pvce.r)