diff --git a/DESCRIPTION b/DESCRIPTION index 5dd6950..e676fce 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,7 +10,7 @@ Authors@R: role = c("aut", "cre"), email = "flystar233@gmail.com") Maintainer: Tengfei Xu -Description: Outqrf package provides a method to find the outlier in custom data by quantile random forests("Quantile Regression Forests", Journal of Machine Learning Research, 7(Jun), 983-999, 2006.). It directly calls the ranger function of the ranger package to perform data fitting and prediction.In outqrf, we also implement the evaluation of outlier prediction results. +Description: This package provides a method to find the outlier in custom data by quantile random forests("Quantile Regression Forests", Journal of Machine Learning Research, 7(Jun), 983-999, 2006.). It directly calls the ranger function of the ranger package to perform data fitting and prediction.In outqrf, we also implement the evaluation of outlier prediction results. LazyData: true License: MIT + file LICENSE Depends: @@ -24,8 +24,9 @@ Imports: dplyr, missRanger, ggpubr, + ggplot2, tidyr -URL: https://github.com/flystar233/outqrf, flystar233.github.io/outqrf +URL: https://github.com/flystar233/outqrf BugReports: https://github.com/flystar233/outqrf/issues Suggests: renv, diff --git a/NAMESPACE b/NAMESPACE index 837b94e..46df0d2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,5 @@ # Generated by roxygen2: do not edit by hand - +importFrom("stats", "predict", "rnorm", "sd") S3method(plot,outqrf) export(evaluateOutliers) export(find_index) diff --git a/R/plot.r b/R/plot.r index 1dfbcfb..02e693a 100644 --- a/R/plot.r +++ b/R/plot.r @@ -10,9 +10,6 @@ #' qrf <- outqrf(irisWithOutliers) #' plot(qrf) plot.outqrf<- function(qrf) { - library(ggpubr) - library(dplyr) - library(tidyr) result_df <- data.frame() data <- qrf$Data for (i in seq_along(qrf$outMatrixs)) { @@ -24,15 +21,15 @@ plot.outqrf<- function(qrf) { } } names(result_df) = names(qrf$outMatrixs) - result_df <- mutate(result_df,tag = "predicted") + result_df <- dplyr::mutate(result_df,tag = "predicted") numeric_features <- names(data)[sapply(data,is.numeric)] data <- data[numeric_features] - data <- mutate(data,tag = "observed") + data <- dplyr::mutate(data,tag = "observed") plot_in <-rbind(result_df,data) - plot_in_longer<- plot_in|>pivot_longer(!tag,names_to ="features",values_to ="value" ) - p<- ggpaired(plot_in_longer, x="tag", y="value", + plot_in_longer<- plot_in|>tidyr::pivot_longer(!tag,names_to ="features",values_to ="value" ) + p<- ggpubr::ggpaired(plot_in_longer, x="tag", y="value", fill="tag", palette = "jco", line.color = "grey", line.size =0.8, width = 0.4,short.panel.labs = FALSE)+ - stat_compare_means(label = "p.format", paired = TRUE)+theme(legend.position = "none")+facet_wrap(~features, scales = "free") + ggpubr::stat_compare_means(label = "p.format", paired = TRUE)+ggplot2::theme(legend.position = "none")+ggplot2::facet_wrap(~features, scales = "free") return(p) } diff --git a/inst/doc/outqrf.Rmd b/inst/doc/outqrf.Rmd new file mode 100644 index 0000000..88e70e1 --- /dev/null +++ b/inst/doc/outqrf.Rmd @@ -0,0 +1,83 @@ +--- +title: "Using outqrf" +author: "lucian xu" +date: "`r Sys.Date()`" +link-citations: true +vignette: > + %\VignetteIndexEntry{Using 'outqrf'} + %\VignetteEncoding{UTF-8} + %\VignetteEngine{knitr::rmarkdown} +output: html_document +--- + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>", + warning = FALSE, + message = FALSE, + fig.width = 6, + fig.height = 4, + fig.align = "center" +) +``` + +## Overview + +outqrf is an R package used for outlier detection. Each numeric variable is regressed onto all other variables using a quantile random forest (QRF). We use ranger to perform the fitting and prediction of quantile regression forests (QRF). Next, we will compute the rank of the observed values in the predicted results' quantiles. If the rank of the observed value exceeds the threshold, the observed value is considered an outlier. + +Since the same predicted value might be distributed across multiple quantiles in the predicted quantile results, this affects our location finding for the observed value. Therefore, we also used a method similar to the outForest package to compare the observed value with the 50% quantile value again to determine the final quantile result. + +## Installation + +```{r install, eval=FALSE, include=FALSE} +# Development version +devtools::install_github("flystar233/outqrf") +``` + +## Usage + +```{r usage, echo=TRUE} +library(outqrf) +#Generate data with outliers in numeric columns +irisWithOutliers <- generateOutliers(iris, p = 0.05,seed =2024) +# Find outliers by quantile random forest regressions +out <- outqrf(irisWithOutliers,quantiles_type=400) +out$outliers +``` + +## Evaluation on diamonds (Small Dataset) + +```{r Evaluation1, echo=TRUE} +library(outqrf) +irisWithOutliers <- generateOutliers(iris, p = 0.05,seed =2024) +qrf <- outqrf(irisWithOutliers,quantiles_type=400) + +evaluateOutliers(iris,irisWithOutliers,qrf$outliers) +``` + + + +```{r Evaluation1_1, eval=FALSE} +plot(qrf) +``` + +```{r Evaluation1_2, echo=FALSE} +library(outqrf) +irisWithOutliers <- generateOutliers(iris, p = 0.05,seed =2024) +qrf <- outqrf(irisWithOutliers,quantiles_type=400) +plot(qrf) +``` + +## Evaluation on diamonds (Big Dataset) + +```{r Evaluation2, echo=TRUE} +library(outqrf) +library(tidyverse) +data <- diamonds|>select(price,carat,cut,color,clarity) +data2 <- outqrf::generateOutliers(data, p = 0.001,seed =2024) +# 108 +qrf <- outqrf(data2,num.threads=8,quantiles_type=400) +#The process can be slow because it needs to predict the value at 400|1000 quantiles for each observation. +evaluateOutliers(data,data2,qrf$outliers) +``` diff --git a/inst/doc/outqrf.html b/inst/doc/outqrf.html new file mode 100644 index 0000000..fbb127b --- /dev/null +++ b/inst/doc/outqrf.html @@ -0,0 +1,506 @@ + + + + + + + + + + + + + + + +Using outqrf + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +
+

Overview

+

outqrf is an R package used for outlier detection. Each numeric +variable is regressed onto all other variables using a quantile random +forest (QRF). We use ranger to perform the fitting and prediction of +quantile regression forests (QRF). Next, we will compute the rank of the +observed values in the predicted results’ quantiles. If the rank of the +observed value exceeds the threshold, the observed value is considered +an outlier.

+

Since the same predicted value might be distributed across multiple +quantiles in the predicted quantile results, this affects our location +finding for the observed value. Therefore, we also used a method similar +to the outForest package to compare the observed value with the 50% +quantile value again to determine the final quantile result.

+
+
+

Installation

+
+
+

Usage

+
library(outqrf)
+#Generate data with outliers in numeric columns
+irisWithOutliers <- generateOutliers(iris, p = 0.05,seed =2024)
+# Find outliers by quantile random forest regressions
+out <- outqrf(irisWithOutliers,quantiles_type=400)
+#> 
+#> Outlier identification by quantiles random forests
+#> 
+#>   Variables to check:        Sepal.Length, Sepal.Width, Petal.Length, Petal.Width
+#>   Variables used to check:   Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species
+#> 
+#>   Checking: Sepal.Length  Sepal.Width  Petal.Length  Petal.Width
+out$outliers
+#>    row          col observed predicted   rank
+#> 1   32 Sepal.Length     14.9      5.40 0.9975
+#> 2   35 Sepal.Length     -1.8      4.60 0.0025
+#> 3   84 Sepal.Length     11.5      6.80 0.9975
+#> 4  129 Sepal.Length     -5.6      6.30 0.0025
+#> 5   49  Sepal.Width     10.8      3.85 0.9975
+#> 6  131  Sepal.Width     -2.1      2.70 0.0025
+#> 7  137  Sepal.Width     11.5      3.20 0.9975
+#> 8   36 Petal.Length     12.8      1.60 0.9975
+#> 9   73 Petal.Length    -17.2      4.40 0.0025
+#> 10 107 Petal.Length     13.7      5.60 0.9975
+#> 11 123 Petal.Length     -9.0      5.20 0.0025
+#> 12 140 Petal.Length     13.5      5.80 0.9975
+#> 13  10  Petal.Width    -11.8      0.20 0.0025
+#> 14  14  Petal.Width     -6.3      0.20 0.0025
+#> 15  34  Petal.Width      7.6      0.40 0.9975
+#> 16  66  Petal.Width      7.0      2.00 0.9950
+#> 17 113  Petal.Width     -6.1      1.80 0.0025
+
+
+

Evaluation on diamonds (Small Dataset)

+
library(outqrf)
+irisWithOutliers <- generateOutliers(iris, p = 0.05,seed =2024)
+qrf <- outqrf(irisWithOutliers,quantiles_type=400)
+#> 
+#> Outlier identification by quantiles random forests
+#> 
+#>   Variables to check:        Sepal.Length, Sepal.Width, Petal.Length, Petal.Width
+#>   Variables used to check:   Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species
+#> 
+#>   Checking: Sepal.Length  Sepal.Width  Petal.Length  Petal.Width
+
+evaluateOutliers(iris,irisWithOutliers,qrf$outliers)
+#>     Actual  Predicted      Cover   Coverage Efficiency 
+#>      32.00      17.00      17.00       0.53       1.00
+
plot(qrf)
+
#> 
+#> Outlier identification by quantiles random forests
+#> 
+#>   Variables to check:        Sepal.Length, Sepal.Width, Petal.Length, Petal.Width
+#>   Variables used to check:   Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species
+#> 
+#>   Checking: Sepal.Length  Sepal.Width  Petal.Length  Petal.Width
+

+

+
+
+

Evaluation on diamonds (Big Dataset)

+
library(outqrf)
+library(tidyverse)
+data <- diamonds|>select(price,carat,cut,color,clarity)
+data2 <- outqrf::generateOutliers(data, p = 0.001,seed =2024)
+# 108
+qrf <- outqrf(data2,num.threads=8,quantiles_type=400)
+#> 
+#> Outlier identification by quantiles random forests
+#> 
+#>   Variables to check:        price, carat
+#>   Variables used to check:   price, carat, cut, color, clarity
+#> 
+#>   Checking: price  carat
+#The process can be slow because it needs to predict the value at 400|1000 quantiles for each observation. 
+evaluateOutliers(data,data2,qrf$outliers)
+#>     Actual  Predicted      Cover   Coverage Efficiency 
+#>     108.00     369.00     103.00       0.95       0.28
+
+ + + + +
+ + + + + + + + + + + + + + +