knitr::opts_chunk$set( collapse = TRUE, comment = "#>", warning = FALSE, message = FALSE )
The aim of this vignette is to introduce the R package outForest
for multivariate outlier detection and replacement.
outForest
is a random forest based implementation of the method described in Chapter 7.1.2 ("Regression model based anomaly detection") of [@Chandola2009]. Each numeric variable is regressed onto all other variables using a random forest. If the scaled absolute difference between observed value and out-of-bag prediction is suspiciouly large (e.g. more than three times the RMSE of the out-of-bag predictions), then a value is considered an outlier. After identification of outliers, they can be replaced e.g. by predictive mean matching from the non-outliers.
The method can be viewed as a multivariate extension of a basic univariate outlier detection method where a value is considered an outlier if it is more than e.g. three times the standard deviation away from its mean. In the multivariate case, instead of comparing a value with the overall mean, rather the difference to the conditional mean is considered. The outForest
package estimates this conditional mean by a random forest.
Specificly, the outlier score of the $i$-th observed value $x_{ij}$ of variable $j$ is defined as $$ s_{ij} = \frac{x_{ij} - \text{pred}{ij}}{\text{rmse}_j}, $$ where $\text{pred}{ij}$ is is the corresponding out-of-bag prediction of the j-th random forest with RMSE $\text{rmse}j$. If $|s{ij}| > L$ with threshold $L$, then $x_{ij}$ is considered an outlier.
Why using random forests for calculating the conditional means? They often work well without parameter tuning, outliers in the input variables are no issue and the out-of-bag mechanism helps to provide fair outlier scores.
outForest
utilizes the high-performance random forest implementation ranger
[@wright2017]. Since it does not allow for missing values, any missing value is first being imputed by chained random forests, see [@buuren2011].
In the examples below, we will meet different functions from the outForest
package:
outForest
: This is the main function. It identifies and replaces outliers in numeric variables of a data frame.
print
, summary
and plot
: To inspect outlier information.
outliers
and Data
: To extract information on outliers and the data with replaced outliers.
generateOutliers
: To add nasty outliers to a data set.
predict
: To apply a fitted outForest
object to fresh data.
From CRAN:
install.packages("outForest")
Latest version from github:
library(devtools) install_github("mayer79/outForest")
We first generate a data set with two truely multivariate outliers. Then, we use outForest
to hunt them down.
library(outForest) # Create data set with truely multivariate outlier set.seed(3) t <- seq(0, pi, by = 0.01) dat <- data.frame(x = cos(t), y = sin(t) + runif(length(t), -0.1, 0.1)) dat[c(100, 200), ] <- cbind(c(-0.5, 0.5), c(0.4, 0.4)) plot(y ~ x, data = dat) # Let's run outForest on that data ch <- outForest(dat) # What outliers did we find? outliers(ch) # Bingo! How does the fixed data set looks like? plot(y ~ x, data = Data(ch)) # The number of outliers per variable plot(ch)
Note that outForest
offers a ...
argument to pass options to its workhorse random forest implementation ranger
, e.g. num.trees
or mtry
. How would we use its "extra trees" variant with 50 trees? As data set, we add a couple of outliers to the famous iris flower data set.
head(irisWithOutliers <- generateOutliers(iris, p = 0.02)) out <- outForest(irisWithOutliers, splitrule = "extratrees", num.trees = 50, verbose = 0) # The worst outliers head(outliers(out)) # Summary of outliers summary(out) # Basic plot of the number of outliers per variable plot(out) # Basic plot of the scores of the outliers per variable plot(out, what = "scores") # The fixed data head(Data(out))
Further note that outForest
does not rely on tidyverse
but you can embed it into a dplyr
pipeline (without group_by
). Make sure to set verbose = 0
in order to prevent messages flooding your screen.
library(dplyr) irisWithOutliers %>% outForest(verbose = 0) %>% Data() %>% head()
Once an outForest
object is fitted with option allow_predictions = TRUE
, it can be used to find and replace outliers in new data without refitting. Note that random forests can be huge, so use this option with care. Further note that currently, missing values in the new data are only allowed in one single variable to be checked.
out <- outForest(iris, allow_predictions = TRUE, verbose = 0) iris1 <- iris[1:2, ] iris1$Sepal.Length[1] <- -1 pred <- predict(out, newdata = iris1) outliers(pred) Data(pred)
By default outForest
uses all columns in the data set to check all numeric columns with missings. To override this behaviour, you can use its formula interface: The left hand side specifies the variables to be checked (variable names separated by a +
), while the right hand side lists the variables used for checking.
So if you e.g. want to check only variable Sepal.Length
based on Species
, then use this syntax.
out <- outForest(irisWithOutliers, Sepal.Length ~ Species, verbose = 0) summary(out)
If you want to prevent outForest
to identify and replace outliers in the response variable of a subsequent model, Sepal.Length
say, subtract that variable from the left hand side.
out <- outForest(irisWithOutliers, . - Sepal.Length ~ ., verbose = 0) summary(out)
For large data sets, just by chance, many values can surpass the default threshold of 3. To reduce the number of outliers, the threshold can be increased. Alternatively, the number of outliers can be limited by the two arguments max_n_outliers
and max_prop_outliers
. E.g. if at most three outliers are to be identified, set max_n_outliers = 3
. If at most 1% of the values are to be declared as outliers, set max_prop_outliers = 0.01
.
outliers(outForest(irisWithOutliers, max_n_outliers = 3, verbose = 0))
By default outForest
replaces outliers by predictive mean matching on the out-of-bag predictions. Alternatively, you can set the replace
argument to a different value:
"no": Outliers are not replaced.
"predicted": Outliers are replaced by the out-of-bag predictions (without predictive mean matching).
"NA": Outliers are replaced by missing values.
So if you would like to replace outliers by NAs, write:
out <- outForest(irisWithOutliers, replace = "NA", verbose = 0) head(Data(out))
outForest
is based on fitting one random forest for each numeric variable. Since the underlying random forest implementation ranger
uses 500 trees per default, a huge number of trees might be calculated. For larger data sets, the overall process can take very long.
Here are tweaks to make things faster:
Use less trees, e.g. by setting num.trees = 50
. Don't use a too low value because this can lead to missing out-of-bag predictions.
Use smaller number of variables to be considered in a split, e.g. mtry = 2
.
Use smaller bootstrap samples by setting e.g. sample.fraction = 0.1
.
Use a low tree depth max.depth = 6
.
Use large leafs, e.g. min.node.size = 10000
.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.