Classification

objective = "binary" fits a logistic classification model. The response must be 0/1 (numeric or logical) or a two-level factor.

library(fastgbm)

x <- as.matrix(mtcars[, c("mpg", "disp", "hp", "wt")])
y <- mtcars$am  # 0 = automatic, 1 = manual

fit <- fastgbm(
  x, y = y, objective = "binary",
  ntrees = 100L, learning_rate = 0.1, max_depth = 3L,
  seed = 1L, verbose = FALSE
)
fit
#> fastgbm model
#>   objective: binary 
#>   trees: 100 
#>   learning rate: 0.1 
#>   max depth: 3

objective can be omitted: fastgbm() defaults to "binary" whenever y is a 0/1 vector (or two-level factor).

Predictions and evaluation

prob <- predict(fit, x, type = "response")  # predicted probabilities
head(prob)
#> [1] 0.93921700 0.92192505 0.84997064 0.05151746 0.07929779 0.03379528

link <- predict(fit, x, type = "link")      # log-odds
head(link)
#> [1]  2.737736  2.468795  1.734371 -2.912943 -2.451926 -3.353055

metrics(fit, y = y)  # log loss
#> $objective
#> [1] "binary"
#> 
#> $metric
#> [1] "logloss"
#> 
#> $value
#> [1] 0.2412709
mean((prob > 0.5) == y)  # training accuracy
#> [1] 0.9375
importance(fit)
#>   feature      gain
#> 4      wt 50.837932
#> 2    disp  8.623295
#> 3      hp  3.579048
#> 1     mpg  2.740842

Formula interface

dat <- mtcars
dat$am <- factor(dat$am)
fit2 <- fastgbm(am ~ mpg + disp + hp + wt, data = dat, ntrees = 100L, verbose = FALSE)