| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/xgb.cv.R |
| 3 |
\name{xgb.cv} |
| 4 |
\alias{xgb.cv} |
| 5 |
\title{Cross Validation} |
| 6 |
\usage{ |
| 7 |
xgb.cv( |
| 8 |
params = xgb.params(), |
| 9 |
data, |
| 10 |
nrounds, |
| 11 |
nfold, |
| 12 |
prediction = FALSE, |
| 13 |
showsd = TRUE, |
| 14 |
metrics = list(), |
| 15 |
objective = NULL, |
| 16 |
custom_metric = NULL, |
| 17 |
stratified = "auto", |
| 18 |
folds = NULL, |
| 19 |
train_folds = NULL, |
| 20 |
verbose = TRUE, |
| 21 |
print_every_n = 1L, |
| 22 |
early_stopping_rounds = NULL, |
| 23 |
maximize = NULL, |
| 24 |
callbacks = list(), |
| 25 |
... |
| 26 |
) |
| 27 |
} |
| 28 |
\arguments{ |
| 29 |
\item{params}{List of XGBoost parameters which control the model building process. |
| 30 |
See the \href{https://xgboost.readthedocs.io/en/latest/parameter.html}{online documentation} |
| 31 |
and the documentation for \code{\link[=xgb.params]{xgb.params()}} for details. |
| 32 |
|
| 33 |
Should be passed as list with named entries. Parameters that are not specified in this |
| 34 |
list will use their default values. |
| 35 |
|
| 36 |
A list of named parameters can be created through the function \code{\link[=xgb.params]{xgb.params()}}, which |
| 37 |
accepts all valid parameters as function arguments.} |
| 38 |
|
| 39 |
\item{data}{An \code{xgb.DMatrix} object, with corresponding fields like \code{label} or bounds as required |
| 40 |
for model training by the objective. |
| 41 |
|
| 42 |
Note that only the basic \code{xgb.DMatrix} class is supported - variants such as \code{xgb.QuantileDMatrix} |
| 43 |
or \code{xgb.ExtMemDMatrix} are not supported here.} |
| 44 |
|
| 45 |
\item{nrounds}{Max number of boosting iterations.} |
| 46 |
|
| 47 |
\item{nfold}{The original dataset is randomly partitioned into \code{nfold} equal size subsamples.} |
| 48 |
|
| 49 |
\item{prediction}{A logical value indicating whether to return the test fold predictions |
| 50 |
from each CV model. This parameter engages the \code{\link[=xgb.cb.cv.predict]{xgb.cb.cv.predict()}} callback.} |
| 51 |
|
| 52 |
\item{showsd}{Logical value whether to show standard deviation of cross validation.} |
| 53 |
|
| 54 |
\item{metrics}{List of evaluation metrics to be used in cross validation, |
| 55 |
when it is not specified, the evaluation metric is chosen according to objective function. |
| 56 |
Possible options are: |
| 57 |
\itemize{ |
| 58 |
\item \code{error}: Binary classification error rate |
| 59 |
\item \code{rmse}: Root mean square error |
| 60 |
\item \code{logloss}: Negative log-likelihood function |
| 61 |
\item \code{mae}: Mean absolute error |
| 62 |
\item \code{mape}: Mean absolute percentage error |
| 63 |
\item \code{auc}: Area under curve |
| 64 |
\item \code{aucpr}: Area under PR curve |
| 65 |
\item \code{merror}: Exact matching error used to evaluate multi-class classification |
| 66 |
}} |
| 67 |
|
| 68 |
\item{objective}{Customized objective function. Should take two arguments: the first one will be the |
| 69 |
current predictions (either a numeric vector or matrix depending on the number of targets / classes), |
| 70 |
and the second one will be the \code{data} DMatrix object that is used for training. |
| 71 |
|
| 72 |
It should return a list with two elements \code{grad} and \code{hess} (in that order), as either |
| 73 |
numeric vectors or numeric matrices depending on the number of targets / classes (same |
| 74 |
dimension as the predictions that are passed as first argument).} |
| 75 |
|
| 76 |
\item{custom_metric}{Customized evaluation function. Just like \code{objective}, should take two arguments, |
| 77 |
with the first one being the predictions and the second one the \code{data} DMatrix. |
| 78 |
|
| 79 |
Should return a list with two elements \code{metric} (name that will be displayed for this metric, |
| 80 |
should be a string / character), and \code{value} (the number that the function calculates, should |
| 81 |
be a numeric scalar). |
| 82 |
|
| 83 |
Note that even if passing \code{custom_metric}, objectives also have an associated default metric that |
| 84 |
will be evaluated in addition to it. In order to disable the built-in metric, one can pass |
| 85 |
parameter \code{disable_default_eval_metric = TRUE}.} |
| 86 |
|
| 87 |
\item{stratified}{Logical flag indicating whether sampling of folds should be stratified |
| 88 |
by the values of outcome labels. For real-valued labels in regression objectives, |
| 89 |
stratification will be done by discretizing the labels into up to 5 buckets beforehand. |
| 90 |
|
| 91 |
If passing "auto", will be set to \code{TRUE} if the objective in \code{params} is a classification |
| 92 |
objective (from XGBoost's built-in objectives, doesn't apply to custom ones), and to |
| 93 |
\code{FALSE} otherwise. |
| 94 |
|
| 95 |
This parameter is ignored when \code{data} has a \code{group} field - in such case, the splitting |
| 96 |
will be based on whole groups (note that this might make the folds have different sizes). |
| 97 |
|
| 98 |
Value \code{TRUE} here is \strong{not} supported for custom objectives.} |
| 99 |
|
| 100 |
\item{folds}{List with pre-defined CV folds (each element must be a vector of test fold's indices). |
| 101 |
When folds are supplied, the \code{nfold} and \code{stratified} parameters are ignored. |
| 102 |
|
| 103 |
If \code{data} has a \code{group} field and the objective requires this field, each fold (list element) |
| 104 |
must additionally have two attributes (retrievable through \code{attributes}) named \code{group_test} |
| 105 |
and \code{group_train}, which should hold the \code{group} to assign through \code{\link[=setinfo.xgb.DMatrix]{setinfo.xgb.DMatrix()}} to |
| 106 |
the resulting DMatrices.} |
| 107 |
|
| 108 |
\item{train_folds}{List specifying which indices to use for training. If \code{NULL} |
| 109 |
(the default) all indices not specified in \code{folds} will be used for training. |
| 110 |
|
| 111 |
This is not supported when \code{data} has \code{group} field.} |
| 112 |
|
| 113 |
\item{verbose}{If 0, xgboost will stay silent. If 1, it will print information about performance. |
| 114 |
If 2, some additional information will be printed out. |
| 115 |
Note that setting \code{verbose > 0} automatically engages the |
| 116 |
\code{xgb.cb.print.evaluation(period=1)} callback function.} |
| 117 |
|
| 118 |
\item{print_every_n}{When passing \code{verbose>0}, evaluation logs (metrics calculated on the |
| 119 |
data passed under \code{evals}) will be printed every nth iteration according to the value passed |
| 120 |
here. The first and last iteration are always included regardless of this 'n'. |
| 121 |
|
| 122 |
Only has an effect when passing data under \code{evals} and when passing \code{verbose>0}. The parameter |
| 123 |
is passed to the \code{\link[=xgb.cb.print.evaluation]{xgb.cb.print.evaluation()}} callback.} |
| 124 |
|
| 125 |
\item{early_stopping_rounds}{Number of boosting rounds after which training will be stopped |
| 126 |
if there is no improvement in performance (as measured by the evaluatiation metric that is |
| 127 |
supplied or selected by default for the objective) on the evaluation data passed under |
| 128 |
\code{evals}. |
| 129 |
|
| 130 |
Must pass \code{evals} in order to use this functionality. Setting this parameter adds the |
| 131 |
\code{\link[=xgb.cb.early.stop]{xgb.cb.early.stop()}} callback. |
| 132 |
|
| 133 |
If \code{NULL}, early stopping will not be used.} |
| 134 |
|
| 135 |
\item{maximize}{If \code{feval} and \code{early_stopping_rounds} are set, then this parameter must be set as well. |
| 136 |
When it is \code{TRUE}, it means the larger the evaluation score the better. |
| 137 |
This parameter is passed to the \code{\link[=xgb.cb.early.stop]{xgb.cb.early.stop()}} callback.} |
| 138 |
|
| 139 |
\item{callbacks}{A list of callback functions to perform various task during boosting. |
| 140 |
See \code{\link[=xgb.Callback]{xgb.Callback()}}. Some of the callbacks are automatically created depending on the |
| 141 |
parameters' values. User can provide either existing or their own callback methods in order |
| 142 |
to customize the training process.} |
| 143 |
|
| 144 |
\item{...}{Not used. |
| 145 |
|
| 146 |
Some arguments that were part of this function in previous XGBoost versions are currently |
| 147 |
deprecated or have been renamed. If a deprecated or renamed argument is passed, will throw |
| 148 |
a warning (by default) and use its current equivalent instead. This warning will become an |
| 149 |
error if using the \link[=xgboost-options]{'strict mode' option}. |
| 150 |
|
| 151 |
If some additional argument is passed that is neither a current function argument nor |
| 152 |
a deprecated or renamed argument, a warning or error will be thrown depending on the |
| 153 |
'strict mode' option. |
| 154 |
|
| 155 |
\bold{Important:} \code{...} will be removed in a future version, and all the current |
| 156 |
deprecation warnings will become errors. Please use only arguments that form part of |
| 157 |
the function signature.} |
| 158 |
} |
| 159 |
\value{ |
| 160 |
An object of class 'xgb.cv.synchronous' with the following elements: |
| 161 |
\itemize{ |
| 162 |
\item \code{call}: Function call. |
| 163 |
\item \code{params}: Parameters that were passed to the xgboost library. Note that it does not |
| 164 |
capture parameters changed by the \code{\link[=xgb.cb.reset.parameters]{xgb.cb.reset.parameters()}} callback. |
| 165 |
\item \code{evaluation_log}: Evaluation history stored as a \code{data.table} with the |
| 166 |
first column corresponding to iteration number and the rest corresponding to the |
| 167 |
CV-based evaluation means and standard deviations for the training and test CV-sets. |
| 168 |
It is created by the \code{\link[=xgb.cb.evaluation.log]{xgb.cb.evaluation.log()}} callback. |
| 169 |
\item \code{niter}: Number of boosting iterations. |
| 170 |
\item \code{nfeatures}: Number of features in training data. |
| 171 |
\item \code{folds}: The list of CV folds' indices - either those passed through the \code{folds} |
| 172 |
parameter or randomly generated. |
| 173 |
} |
| 174 |
|
| 175 |
Plus other potential elements that are the result of callbacks, such as a list \code{cv_predict} with |
| 176 |
a sub-element \code{pred} when passing \code{prediction = TRUE}, which is added by the \code{\link[=xgb.cb.cv.predict]{xgb.cb.cv.predict()}} |
| 177 |
callback (note that one can also pass it manually under \code{callbacks} with different settings, |
| 178 |
such as saving also the models created during cross validation); or a list \code{early_stop} which |
| 179 |
will contain elements such as \code{best_iteration} when using the early stopping callback (\code{\link[=xgb.cb.early.stop]{xgb.cb.early.stop()}}). |
| 180 |
} |
| 181 |
\description{ |
| 182 |
The cross validation function of xgboost. |
| 183 |
} |
| 184 |
\details{ |
| 185 |
The original sample is randomly partitioned into \code{nfold} equal size subsamples. |
| 186 |
|
| 187 |
Of the \code{nfold} subsamples, a single subsample is retained as the validation data for testing the model, |
| 188 |
and the remaining \code{nfold - 1} subsamples are used as training data. |
| 189 |
|
| 190 |
The cross-validation process is then repeated \code{nrounds} times, with each of the |
| 191 |
\code{nfold} subsamples used exactly once as the validation data. |
| 192 |
|
| 193 |
All observations are used for both training and validation. |
| 194 |
|
| 195 |
Adapted from \url{https://en.wikipedia.org/wiki/Cross-validation_\%28statistics\%29} |
| 196 |
} |
| 197 |
\examples{ |
| 198 |
data(agaricus.train, package = "xgboost") |
| 199 |
|
| 200 |
dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2)) |
| 201 |
|
| 202 |
cv <- xgb.cv( |
| 203 |
data = dtrain, |
| 204 |
nrounds = 20, |
| 205 |
early_stopping_rounds = 1, |
| 206 |
params = xgb.params( |
| 207 |
nthread = 2, |
| 208 |
max_depth = 3, |
| 209 |
objective = "binary:logistic" |
| 210 |
), |
| 211 |
nfold = 5, |
| 212 |
metrics = list("rmse","auc"), |
| 213 |
prediction = TRUE |
| 214 |
) |
| 215 |
print(cv) |
| 216 |
print(cv, verbose = TRUE) |
| 217 |
|
| 218 |
# Callbacks might add additional attributes, separated by the name of the callback |
| 219 |
cv$early_stop$best_iteration |
| 220 |
head(cv$cv_predict$pred) |
| 221 |
} |