[R] resolve brace_linter warnings (#8564)

This commit is contained in:
James Lamb 2022-12-08 09:01:00 -06:00 committed by GitHub
parent 40a1a2ffa8
commit fbe40d00d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 31 additions and 20 deletions

View File

@ -615,9 +615,11 @@ cb.gblinear.history <- function(sparse=FALSE) {
coefs <- NULL coefs <- NULL
init <- function(env) { init <- function(env) {
if (!is.null(env$bst)) { # xgb.train: # xgb.train(): bst will be present
} else if (!is.null(env$bst_folds)) { # xgb.cv: # xgb.cv(): bst_folds will be present
} else stop("Parent frame has neither 'bst' nor 'bst_folds'") if (is.null(env$bst) && is.null(env$bst_folds)) {
stop("Parent frame has neither 'bst' nor 'bst_folds'")
}
} }
# convert from list to (sparse) matrix # convert from list to (sparse) matrix

View File

@ -251,8 +251,7 @@ generate.cv.folds <- function(nfold, nrows, stratified, label, params) {
# Creates CV folds stratified by the values of y. # Creates CV folds stratified by the values of y.
# It was borrowed from caret::createFolds and simplified # It was borrowed from caret::createFolds and simplified
# by always returning an unnamed list of fold indices. # by always returning an unnamed list of fold indices.
xgb.createFolds <- function(y, k = 10) xgb.createFolds <- function(y, k = 10) {
{
if (is.numeric(y)) { if (is.numeric(y)) {
## Group the numeric data based on their magnitudes ## Group the numeric data based on their magnitudes
## and sample within those groups. ## and sample within those groups.

View File

@ -104,7 +104,11 @@ xgb.importance <- function(feature_names = NULL, model = NULL, trees = NULL,
XGBoosterFeatureScore_R, model$handle, jsonlite::toJSON(args, auto_unbox = TRUE, null = "null") XGBoosterFeatureScore_R, model$handle, jsonlite::toJSON(args, auto_unbox = TRUE, null = "null")
) )
names(results) <- c("features", "shape", "weight") names(results) <- c("features", "shape", "weight")
n_classes <- if (length(results$shape) == 2) { results$shape[2] } else { 0 } if (length(results$shape) == 2) {
n_classes <- results$shape[2]
} else {
n_classes <- 0
}
importance <- if (n_classes == 0) { importance <- if (n_classes == 0) {
data.table(Feature = results$features, Weight = results$weight)[order(-abs(Weight))] data.table(Feature = results$features, Weight = results$weight)[order(-abs(Weight))]
} else { } else {

View File

@ -102,7 +102,9 @@ xgb.plot.importance <- function(importance_matrix = NULL, top_n = NULL, measure
original_mar <- par()$mar original_mar <- par()$mar
# reset margins so this function doesn't have side effects # reset margins so this function doesn't have side effects
on.exit({par(mar = original_mar)}) on.exit({
par(mar = original_mar)
})
mar <- original_mar mar <- original_mar
if (!is.null(left_margin)) if (!is.null(left_margin))

View File

@ -12,7 +12,7 @@ cat('running cross validation\n')
# do cross validation, this will print result out as # do cross validation, this will print result out as
# [iteration] metric_name:mean_value+std_value # [iteration] metric_name:mean_value+std_value
# std_value is standard deviation of the metric # std_value is standard deviation of the metric
xgb.cv(param, dtrain, nrounds, nfold = 5, metrics = {'error'}) xgb.cv(param, dtrain, nrounds, nfold = 5, metrics = 'error')
cat('running cross validation, disable standard deviation display\n') cat('running cross validation, disable standard deviation display\n')
# do cross validation, this will print result out as # do cross validation, this will print result out as

View File

@ -170,8 +170,9 @@ test_that("SHAPs sum to predictions, with or without DART", {
label = y, label = y,
nrounds = nrounds) nrounds = nrounds)
pr <- function(...) pr <- function(...) {
predict(fit, newdata = d, ...) predict(fit, newdata = d, ...)
}
pred <- pr() pred <- pr()
shap <- pr(predcontrib = TRUE) shap <- pr(predcontrib = TRUE)
shapi <- pr(predinteraction = TRUE) shapi <- pr(predinteraction = TRUE)

View File

@ -86,7 +86,10 @@ For that purpose, we will:
```{r classToIntegers} ```{r classToIntegers}
# Convert from classes to numbers # Convert from classes to numbers
y <- train[, nameLastCol, with = FALSE][[1]] %>% gsub('Class_','',.) %>% {as.integer(.) -1} y <- train[, nameLastCol, with = FALSE][[1]] %>%
gsub('Class_','',.) %>%
as.integer %>%
subtract(., 1)
# Display the first 5 levels # Display the first 5 levels
y[1:5] y[1:5]