## Not run: ------------------------------------
# # First, we must load libraries: xgboost, data.table, and R.utils
# library(xgboost)
# library(data.table)
# library(R.utils)
#
# # Second, we load some data
# data(agaricus.train, package='xgboost')
# data(agaricus.test, package='xgboost')
#
# # Third, we create the xgb.DMatrices and the watchlist
# dtrain <- xgb.DMatrix(agaricus.train$data, label = agaricus.train$label)
# dtest <- xgb.DMatrix(agaricus.test$data, label = agaricus.test$label)
# watchlist <- list(train = dtrain, eval = dtest)
#
# # Fourth, we create a metric with Accuracy/Threshold logging
# xgboard.eval.error <- function(preds, dtrain, dump) {
#
# # Get xgboost label info
# y_true <- getinfo(dtrain, "label")
#
# # Do stuff to get best acc + best threshold
# DT <- data.table(y_true = y_true, y_prob = preds, key = "y_prob")
# cleaner <- !duplicated(DT[, "y_prob"], fromLast = TRUE)
# lens <- length(y_true)
# nump <- sum(y_true)
# DT[, tn_v := cumsum(y_true == 0)]
# DT[, tp_v := nump - cumsum(y_true == 1)]
# DT <- DT[cleaner, ]
# DT[, acc := (tn_v + tp_v) / lens]
#
# # Stuff is stored here
# best <- which.max(DT$acc)[1]
# metric <- c(DT$acc[best], DT$y_prob[best])
#
# # Dump (log) both Accuracy and Threshold
# xgboard.dump(metric, dump)
#
# # Return metric as typically done with xgboost custom evaluation metric
# return(list(metric = "error", value = metric[1]))
# }
## ---------------------------------------------
Run the code above in your browser using DataLab