-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
99 lines (71 loc) · 2.88 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
---
output: github_document
editor_options:
chunk_output_type: console
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
set.seed(1)
```
# randomPlantedForest
<!-- badges: start -->
[![R-CMD-check](https://github.com/PlantedML/randomPlantedForest/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/PlantedML/randomPlantedForest/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/PlantedML/randomPlantedForest/branch/master/graph/badge.svg)](https://app.codecov.io/gh/PlantedML/randomPlantedForest?branch=master)
[![randomPlantedForest status badge](https://plantedml.r-universe.dev/badges/randomPlantedForest)](https://plantedml.r-universe.dev/randomPlantedForest)
<!-- badges: end -->
`randomPlantedForest` implements "Random Planted Forest", a directly interpretable tree ensemble [(arxiv)][arxiv1].
## Installation
You can install the development version of `randomPlantedForest` from [GitHub](https://github.com/) with
```r
# install.packages("remotes")
remotes::install_github("PlantedML/randomPlantedForest")
```
or from [r-universe][r-universe] with
```r
install.packages("randomPlantedForest", repos = "https://plantedml.r-universe.dev")
```
## Example
Model fitting uses a familiar interface:
```{r}
library(randomPlantedForest)
mtcars$cyl <- factor(mtcars$cyl)
rpfit <- rpf(mpg ~ cyl + wt + hp, data = mtcars, ntrees = 25, max_interaction = 2)
rpfit
predict(rpfit, new_data = mtcars) |>
cbind(mpg = mtcars$mpg) |>
head()
```
Prediction components can be accessed via `predict_components`, including the intercept, main effects, and interactions up to a specified degree.
The returned object also contains the original data as `x`, which is required for visualization.
The `glex` package can be used as well: `glex(rpfit)` yields the same result.
```{r}
components <- predict_components(rpfit, new_data = mtcars)
str(components)
```
Various visualization options are available via `glex`, e.g. for main and second-order interaction effects:
```{r}
# install glex if not available:
if (!requireNamespace("glex")) remotes::install_github("PlantedML/glex")
library(glex)
library(ggplot2)
library(patchwork) # For plot arrangement
p1 <- autoplot(components, "wt")
p2 <- autoplot(components, "hp")
p3 <- autoplot(components, "cyl")
p4 <- autoplot(components, c("wt", "hp"))
(p1 + p2) / (p3 + p4) +
plot_annotation(
title = "Selected effects for mtcars",
caption = "(It's a tiny dataset but it has to fit in a README, okay?)"
)
```
See the [Bikesharing decomposition][bikes] article for more examples.
[arxiv1]: https://arxiv.org/abs/2012.14563
[bikes]: https://plantedml.com/glex/articles/Bikesharing-Decomposition-rpf.html
[r-universe]: https://plantedml.r-universe.dev/packages