/
test_pkg_workflows.R
60 lines (49 loc) · 1.37 KB
/
test_pkg_workflows.R
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
# Exits
if (!requireNamespace("parsnip", quietly = TRUE)) {
exit_file("Package parsnip missing")
}
if (!requireNamespace("workflows", quietly = TRUE)) {
exit_file("Package workflows missing")
}
# Load required packages
suppressMessages({
library(parsnip)
library(workflows)
})
# Generate Friedman benchmark data
friedman1 <- gen_friedman(seed = 101)
# Fit a linear model
mod <- parsnip::linear_reg()
wflow <- workflow() %>% add_model(mod) %>% add_formula(y ~ .)
fitted <- generics::fit(wflow, data = friedman1)
# Compute model-based VI scores
vis <- vi(fitted, scale = TRUE)
expect_error(vi(wflow), "The workflow does not have a model fit")
# Expect `vi()` and `vi_model()` to both work
expect_identical(
current = vi(fitted, sort = FALSE),
target = vi_model(fitted)
)
# Check class
expect_identical(class(vis), target = c("vi", "tbl_df", "tbl", "data.frame"))
# Check dimensions (should be one row for each feature)
expect_identical(ncol(friedman1) - 1L, target = nrow(vis))
# Display VIP
vip(vis, geom = "point")
# Try permutation importance
set.seed(953) # for reproducibility
p <- vip(
object = fitted,
method = "permute",
train = friedman1,
target = "y",
pred_wrapper = predict,
metric = "rmse",
nsim = 30,
geom = "violin",
jitter = TRUE,
all_permutation = TRUE,
mapping = aes(color = Variable)
)
expect_true(inherits(p, what = "ggplot"))
p # display VIP