---
title: "Regression"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Regression}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
```

```{r}
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
```

`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

```{r}
pred <- predict(fit, x, type = "response")
head(pred)

metrics(fit, y = y)
importance(fit)
```

## Formula interface

```{r}
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.

```{r}
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
fit3$best_iteration
```
