Description Usage Arguments Value Examples
ReduceAnomalies
It reduces the number of detected anomalies. This function is
designed to reduce the number of false positives keeping only the first detection of all those
that are close to each other. This proximity distance is defined by a window
1 2 | ReduceAnomalies(data, windowLength, incremental = FALSE,
last.res = NULL)
|
data |
Numerical vector with anomaly labels. |
windowLength |
Window length. |
incremental |
TRUE for incremental processing and FALSE for classic processing |
last.res |
Last result returned by the algorithm. |
If incremental
= FALSE, new Numerical vector with reduced anomaly labels. Else,
a list of the following items.
result |
New Numerical vector with reduced anomaly labels. |
last.res |
Last result returned by the algorithm. It is a list with |
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 | ## EXAMPLE 1: Classic Processing ----------------------
## Generate data
set.seed(100)
n <- 350
x <- sample(1:100, n, replace = TRUE)
x[70:90] <- sample(110:115, 21, replace = TRUE)
x[25] <- 200
x[320] <- 170
df <- data.frame(timestamp = 1:n, value = x)
## Calculate anomalies
result <- IpSdEwma(
data = df$value,
n.train = 5,
threshold = 0.01,
l = 2
)
res <- cbind(df, result$result)
## Plot results
PlotDetections(res, title = "SD-EWMA ANOMALY DETECTOR")
## Reduce anomalies
res$is.anomaly <- ReduceAnomalies(res$is.anomaly, windowLength = 5)
## Plot results
PlotDetections(res, title = "SD-EWMA ANOMALY DETECTOR")
## EXAMPLE 2: Incremental Processing ----------------------
# install.packages("stream")
library("stream")
# Generate data
set.seed(100)
n <- 350
x <- sample(1:100, n, replace = TRUE)
x[70:90] <- sample(110:115, 21, replace = TRUE)
x[25] <- 200
x[320] <- 170
df <- data.frame(timestamp = 1:n, value = x)
dsd_df <- DSD_Memory(df)
# Initialize parameters for the loop
last.res <- NULL
red.res <- NULL
res <- NULL
nread <- 100
numIter <- ceiling(n/nread)
# Calculate anomalies
for(i in 1:numIter) {
# read new data
newRow <- get_points(dsd_df, n = nread, outofpoints = "ignore")
# calculate if it's an anomaly
last.res <- IpSdEwma(
data = newRow$value,
n.train = 5,
threshold = 0.01,
l = 2,
last.res = last.res$last.res
)
if(!is.null(last.res$result)){
# reduce anomalies
red.res <- ReduceAnomalies(last.res$result$is.anomaly,
windowLength = 5, incremental = TRUE, last.res = red.res$last.res)
last.res$result$is.anomaly <- red.res$result
# prepare the result
res <- rbind(res, cbind(newRow, last.res$result))
}
}
# Plot results
PlotDetections(res, title = "SD-EWMA ANOMALY DETECTOR")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.