-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
92 lines (66 loc) · 2.32 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
---
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%",
message = FALSE,
warning = FALSE
)
```
# anomalyrecipes
<!-- badges: start -->
<!-- badges: end -->
Anomaly recipes provides ```{recipes}``` step functions for anomaly detection.
Right now the package contains one step function: ```step_isofor``` (short for isolation forest). This is an implementation of the isolation forest algorithm from the R ```{solitude}``` package. The plan is to also add a matrix profile step function from the ```{tsmp}``` package.
This is a work in progress (and mostly a pet project at this point), but if you have a chance to try it out, please let me know how it goes.
## Installation
You can install the development version of anomalyrecipes from github:
``` r
devtools::install_github("kevin-m-kent/anomalyrecipes")
```
## Getting Started
Let's start with a case where we are building some pre-processing steps for a linear model. We would like to add a feature that captures how anomalous observations are.
```{r step_isofor_example, message=FALSE, warning=FALSE}
library(anomalyrecipes)
library(tidymodels)
library(tidyverse)
library(solitude)
tidymodels_prefer()
splits <- initial_split(mtcars)
train <- training(splits)
test <- testing(splits)
resamples <- bootstraps(train, times = 5)
rec_obj <-
recipe(mpg ~ ., data = mtcars) %>%
step_dummy(all_nominal_predictors()) %>%
step_isofor(all_predictors(), sample_size = 10, max_depth = 5)
baked_data <- rec_obj %>%
prep() %>%
bake(train)
baked_data %>%
head()
```
You can also tune the isolation forest pre-processing step:
```{r tuning, message=FALSE, warning=FALSE}
lm_mod <- linear_reg() %>%
set_engine("lm")
rec_obj_tuned <-
recipe(mpg ~ ., data = mtcars) %>%
step_dummy(all_nominal_predictors()) %>%
step_isofor(all_predictors(), sample_size = tune(), max_depth = tune())
wf_linear <- workflow() %>%
add_recipe(rec_obj_tuned) %>%
add_model(lm_mod)
iso_param <- wf_linear %>%
parameters() %>%
update(sample_size = anomalyrecipes::sample_size(c(1, 24)))
tuned_results <- wf_linear %>%
tune_grid(resamples = resamples, param_info = iso_param)
tuned_results %>%
autoplot()
```