Skip to content

Commit bf05f23

Browse files
committed
add examples for bart() and gen_additive_mod()
1 parent 3fd6a35 commit bf05f23

File tree

3 files changed

+171
-4
lines changed

3 files changed

+171
-4
lines changed

DESCRIPTION

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,25 @@ Suggests:
7070
VignetteBuilder:
7171
knitr
7272
ByteCompile: true
73-
Config/Needs/website: C50, earth, glmnet, keras, kernlab, kknn, LiblineaR,
74-
mgcv, nnet, parsnip, randomForest, ranger, rpart, rstanarm, tidymodels,
75-
tidyverse/tidytemplate, xgboost
73+
Config/Needs/website:
74+
C50,
75+
dbarts,
76+
earth,
77+
glmnet,
78+
keras,
79+
kernlab,
80+
kknn,
81+
LiblineaR,
82+
mgcv,
83+
nnet,
84+
parsnip,
85+
randomForest,
86+
ranger,
87+
rpart,
88+
rstanarm,
89+
tidymodels,
90+
tidyverse/tidytemplate,
91+
xgboost
7692
Encoding: UTF-8
7793
LazyData: true
7894
Roxygen: list(markdown = TRUE)

vignettes/articles/Examples.Rmd

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,81 @@ These examples show how to *fit* and *predict* with different combinations of mo
2424
The following examples use consistent data sets throughout. For regression, we use the Chicago ridership data. For classification, we use an artificial data set for a binary example and the Palmer penguins data for a multiclass example.
2525

2626

27+
## `bart()` models
28+
29+
<details id="bart-dbarts">
30+
31+
<summary>With the `"dbarts"` engine</summary>
32+
33+
<h3>Regression Example (`dbarts`)</h3>
34+
35+
```{r echo=FALSE}
36+
knitr::spin_child("template-reg-chicago.R")
37+
```
38+
39+
We can define the model with specific parameters:
40+
41+
```{r}
42+
bt_reg_spec <-
43+
bart(trees = 15) %>%
44+
# This model can be used for classification or regression, so set mode
45+
set_mode("regression") %>%
46+
set_engine("dbarts")
47+
bt_reg_spec
48+
```
49+
50+
Now we create the model fit object:
51+
52+
```{r}
53+
set.seed(1)
54+
bt_reg_fit <- bt_reg_spec %>% fit(ridership ~ ., data = Chicago_train)
55+
bt_reg_fit
56+
```
57+
58+
The holdout data can be predicted:
59+
60+
```{r}
61+
predict(bt_reg_fit, Chicago_test)
62+
```
63+
64+
65+
<h3>Classification Example (`dbarts`)</h3>
66+
67+
```{r echo=FALSE}
68+
knitr::spin_child("template-cls-two-class.R")
69+
```
70+
71+
We can define the model with specific parameters:
72+
73+
```{r}
74+
bt_cls_spec <-
75+
bart(trees = 15) %>%
76+
# This model can be used for classification or regression, so set mode
77+
set_mode("classification") %>%
78+
set_engine("dbarts")
79+
bt_cls_spec
80+
```
81+
82+
Now we create the model fit object:
83+
84+
```{r}
85+
set.seed(1)
86+
bt_cls_fit <- bt_cls_spec %>% fit(Class ~ ., data = data_train)
87+
bt_cls_fit
88+
```
89+
90+
The holdout data can be predicted for both hard class predictions and probabilities. We'll bind these together into one tibble:
91+
92+
```{r}
93+
bind_cols(
94+
predict(bt_cls_fit, data_test),
95+
predict(bt_cls_fit, data_test, type = "prob")
96+
)
97+
```
98+
99+
</details>
100+
101+
27102
## `boost_tree()` models
28103

29104
<details id="boost-tree-xgboost">
@@ -252,6 +327,82 @@ The following examples use consistent data sets throughout. For regression, we u
252327
```
253328

254329
</details>
330+
331+
## `gen_additive_mod()` models
332+
333+
<details id="gen-additive-mod-mgcv">
334+
335+
<summary>With the `"mgcv"` engine</summary>
336+
337+
<h3>Regression Example (`mgcv`)</h3>
338+
339+
```{r echo=FALSE}
340+
knitr::spin_child("template-reg-chicago.R")
341+
```
342+
343+
We can define the model with specific parameters:
344+
345+
```{r}
346+
gam_reg_spec <-
347+
gen_additive_mod(select_features = FALSE, adjust_deg_free = 10) %>%
348+
# This model can be used for classification or regression, so set mode
349+
set_mode("regression") %>%
350+
set_engine("mgcv")
351+
gam_reg_spec
352+
```
353+
354+
Now we create the model fit object:
355+
356+
```{r}
357+
set.seed(1)
358+
gam_reg_fit <- gam_reg_spec %>%
359+
fit(ridership ~ Clark_Lake + Quincy_Wells, data = Chicago_train)
360+
gam_reg_fit
361+
```
362+
363+
The holdout data can be predicted:
364+
365+
```{r}
366+
predict(gam_reg_fit, Chicago_test)
367+
```
368+
369+
370+
<h3>Classification Example (`mgcv`)</h3>
371+
372+
```{r echo=FALSE}
373+
knitr::spin_child("template-cls-two-class.R")
374+
```
375+
376+
We can define the model with specific parameters:
377+
378+
```{r}
379+
gam_cls_spec <-
380+
gen_additive_mod(select_features = FALSE, adjust_deg_free = 10) %>%
381+
# This model can be used for classification or regression, so set mode
382+
set_mode("classification") %>%
383+
set_engine("mgcv")
384+
gam_cls_spec
385+
```
386+
387+
Now we create the model fit object:
388+
389+
```{r}
390+
set.seed(1)
391+
gam_cls_fit <- gam_cls_spec %>% fit(Class ~ A + B, data = data_train)
392+
gam_cls_fit
393+
```
394+
395+
The holdout data can be predicted for both hard class predictions and probabilities. We'll bind these together into one tibble:
396+
397+
```{r}
398+
bind_cols(
399+
predict(gam_cls_fit, data_test),
400+
predict(gam_cls_fit, data_test, type = "prob")
401+
)
402+
```
403+
404+
</details>
405+
255406

256407
## `linear_reg()` models
257408

vignettes/articles/template-cls-two-class.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' The example data has two predictors and an outcome with two classes. Both predictors are in the same units
1+
#' The example data has two predictors and an outcome with two classes. Both predictors are in the same units.
22

33
#+ results = "hide", messages = FALSE
44
library(tidymodels)

0 commit comments

Comments
 (0)