Regression

library(fastgbm)

x <- as.matrix(mtcars[, c("cyl", "disp", "hp", "wt")])
y <- mtcars$mpg

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

objective can be omitted for a numeric response: fastgbm() defaults to "regression" for any y that is not a 0/1 vector, a two-level factor, or a survival::Surv object.

Predictions and evaluation

pred <- predict(fit, x, type = "response")
head(pred)
#> [1] 20.84815 21.03444 22.50698 20.36689 17.72464 18.67521

metrics(fit, y = y)
#> $objective
#> [1] "regression"
#> 
#> $metric
#> [1] "rmse"
#> 
#> $value
#> [1] 0.7226311
importance(fit)
#>   feature      gain
#> 4      wt 1097.6196
#> 2    disp  750.0978
#> 1     cyl  337.2986
#> 3      hp  319.9553

Formula interface

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

Early stopping

As with the other objectives, supplying validation/early_stopping is recommended whenever held-out performance matters – training every ntrees round without it tends to overfit small-to-medium datasets.

set.seed(1)
idx <- sample(nrow(mtcars), 24)
fit3 <- fastgbm(
  x[idx, ], y = y[idx], objective = "regression",
  ntrees = 200L, validation = list(x = x[-idx, ], y = y[-idx]),
  early_stopping = 10L, verbose = FALSE
)
fit3$stopping_reason
#> [1] "early_stopping"
fit3$best_iteration
#> [1] 44