-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
158 lines (119 loc) · 5.37 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
---
output: github_document
---
<!-- 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%"
)
```
# important
<!-- badges: start -->
[![R-CMD-check](https://github.com/tidymodels/important/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/tidymodels/important/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/tidymodels/important/branch/main/graph/badge.svg)](https://app.codecov.io/gh/tidymodels/important?branch=main)
<!-- badges: end -->
The important package has a succinct interface for obtaining estimates of predictor importance with tidymodels objects. A few of the main features:
- Any performance metrics from the yardstick package can be used.
- Importance can be calculated for either the original columns or at the level of any derived model terms created during feature engineering.
- The computations that loop across permutation iterations and predictors columns are easily parallelized.
- The results are returned in a tidy format.
## Installation
You can install the development version of important from [GitHub](https://github.com/) with:
``` r
# Not yet!
# install.packages("devtools")
devtools::install_github("tidymodels/important")
```
## Do we really need another package that computes variable importances?
The main reason for making important is censored regression models. tidymodels released tools for fitting and qualifying models that have censored outcomes. This included some dynamic performance metrics that were evaluated at different time points. This was a substantial change for us, and it would have been even more challenging to add to other packages.
## Example
Let's look at an analysis that models [food delivery times](https://aml4td.org/chapters/whole-game.html#sec-delivery-times). The outcome is the time between an order being placed and the delivery (all data are complete - there is no censoring). We model this in terms of the order day/time, the distance to the restaurant, and which items are contained in the order. Exploratory data analysis shows several nonlinear trends in the data and some interactions between these trends.
We'll load the tidymodels and important packages to get started.
```{r}
#| label: startup-sshh
#| include: false
library(tidymodels)
library(important)
theme_set(theme_bw())
```
```{r}
#| label: startup
#| include: false
library(tidymodels)
library(important)
```
The data are split into training, validation, and testing sets.
```{r}
#| label: food-data
data(deliveries, package = "modeldata")
set.seed(991)
delivery_split <- initial_validation_split(deliveries, prop = c(0.6, 0.2), strata = time_to_delivery)
delivery_train <- training(delivery_split)
```
The model uses a recipe with spline terms for the hour and distances. The nonlinear trend over the time of order changes on the day, so we added interactions between these two sets of terms. Finally, a simple linear regression model is used for estimation:
```{r}
#| label: model
delivery_rec <-
recipe(time_to_delivery ~ ., data = delivery_train) %>%
step_dummy(all_factor_predictors()) %>%
step_zv(all_predictors()) %>%
step_spline_natural(hour, distance, deg_free = 10) %>%
step_interact(~ starts_with("hour_"):starts_with("day_"))
lm_wflow <- workflow(delivery_rec, linear_reg())
lm_fit <- fit(lm_wflow, delivery_train)
```
First, let’s capture the effect of the individual model terms. These terms are from the derived features in the models, such as dummy variables, spline terms, interaction columns, etc.
```{r}
#| label: derived-importance
set.seed(382)
lm_deriv_imp <-
importance_perm(
lm_fit,
data = delivery_train,
metrics = metric_set(mae, rsq),
times = 50,
type = "derived"
)
lm_deriv_imp
```
Using mean absolute error as the metric of interest, the top 5 features are:
```{r}
lm_deriv_imp %>%
filter(.metric == "mae") %>%
slice_max(importance, n = 5)
```
Two notes:
- The importance scores are the ratio of the mean change in performance and the associated standard error. The mean value is always increasing with importance, no matter which direction is preferred for the specific metric(s).
- We can run these in parallel by loading the future package and specifying a parallel backend using the `plan()` function.
There is a plot method that can help visualize the results:
```{r}
#| label: derived-plot
#| fig.height: 8
autoplot(lm_deriv_imp, top = 50)
```
Since there are spline terms and interactions for the hour column, we might not care about the importance of a term such as `hour_06` (the sixth spline feature). In aggregate, we might want to know the effect of the original predictor columns. The `type` option is used for this purpose:
```{r}
#| label: original-importance
set.seed(382)
lm_orig_imp <-
importance_perm(
lm_fit,
data = delivery_train,
metrics = metric_set(mae, rsq),
times = 50,
type = "original"
)
# Top five:
lm_orig_imp %>%
filter(.metric == "mae") %>%
slice_max(importance, n = 5)
```
```{r}
#| label: original-plot
autoplot(lm_orig_imp)
```
## Code of Conduct
Please note that the important project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/1/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.