## ----setup, include = FALSE----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) backup_options <- options() options(width = 1000) set.seed(1991) xgbAvail <- requireNamespace('xgboost', quietly = TRUE) ## ----eval = xgbAvail, echo=TRUE, results = 'hide'------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- library("xgboost") library("ParBayesianOptimization") data(agaricus.train, package = "xgboost") Folds <- list( Fold1 = as.integer(seq(1,nrow(agaricus.train$data),by = 3)) , Fold2 = as.integer(seq(2,nrow(agaricus.train$data),by = 3)) , Fold3 = as.integer(seq(3,nrow(agaricus.train$data),by = 3)) ) ## ----eval = xgbAvail------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ scoringFunction <- function(max_depth, min_child_weight, subsample) { dtrain <- xgb.DMatrix(agaricus.train$data,label = agaricus.train$label) Pars <- list( booster = "gbtree" , eta = 0.01 , max_depth = max_depth , min_child_weight = min_child_weight , subsample = subsample , objective = "binary:logistic" , eval_metric = "auc" ) xgbcv <- xgb.cv( params = Pars , data = dtrain , nround = 100 , folds = Folds , prediction = TRUE , showsd = TRUE , early_stopping_rounds = 5 , maximize = TRUE , verbose = 0) return( list( Score = max(xgbcv$evaluation_log$test_auc_mean) , nrounds = xgbcv$best_iteration ) ) } ## ----eval = xgbAvail------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ bounds <- list( max_depth = c(2L, 10L) , min_child_weight = c(1, 25) , subsample = c(0.25, 1) ) ## ----eval = xgbAvail------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ set.seed(1234) optObj <- bayesOpt( FUN = scoringFunction , bounds = bounds , initPoints = 4 , iters.n = 3 ) ## ----eval = xgbAvail------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ optObj$scoreSummary ## ----eval = xgbAvail------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ getBestPars(optObj) ## ----revert_options, include=FALSE-------------------------------------------- options(backup_options)