GetLabels: Get Lables

Description Usage Arguments Details Value References Examples

View source: R/GetDetectorScore_GetLabels.R

Description

GetLabels Calculates the start and end positions of each window that are focused on the real anomalies. This windows can be used to know if the detected anomaly is a true positive or not.

Usage

1

Arguments

data

All dataset with training and test datasets with at least timestamp, value, is.anomaly, is.real.anomaly, start.limit and end.limit columns.

Details

data must be a data.frame with timestamp, value, is.anomaly and is.real.anomaly columns. timestamp column can be numeric, of type POSIXct, or a character type date convertible to POSIXct. see GetWindowsLimits to know more about how to get start.limit and end.limit columns.

Value

Same data set with two additional columns label and first.tp. first.tp indicates for each window Which is the position of first true positive. label indicates for each detection if it is a TP, FP, TN or FN.

References

A. Lavin and S. Ahmad, “Evaluating Real-time Anomaly Detection Algorithms – the Numenta Anomaly Benchmark,” in 14th International Conference on Machine Learning and Applications (IEEE ICMLA’15), 2015.

Examples

 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
## Generate data
set.seed(100)
n <- 180
x <- sample(1:100, n, replace = TRUE)
x[70:90] <- sample(110:115, 21, replace = TRUE)
x[25] <- 200
x[150] <- 170
df <- data.frame(timestamp = 1:n, value = x)

# Add is.real.anomaly column
df$is.real.anomaly <- 0
df[c(25,80,150), "is.real.anomaly"] <- 1

## Calculate anomalies
result <- CpSdEwma(
  data = df$value,
  n.train = 5,
  threshold = 0.01,
  l = 3
)
res <- cbind(df, result)

# Get Window Limits
data <- GetWindowsLimits(res)
data[data$is.real.anomaly == 1,]

# Get labels
data <- GetLabels(data)
data[data$is.real.anomaly == 1 | data$is.anomaly == 1,]

# Plot results
PlotDetections(res, print.real.anomaly = TRUE, print.time.window = TRUE)

Example output

    timestamp value is.real.anomaly is.anomaly        lcl      ucl start.limit
25         25   200               1          1 -29.382821 123.0267          22
80         80   112               1          0  -2.043244 182.1024          77
150       150   170               1          1 -40.191861 144.9320         147
    end.limit
25         28
80         83
150       153
    timestamp value is.real.anomaly is.anomaly        lcl      ucl start.limit
25         25   200               1          1 -29.382821 123.0267          22
80         80   112               1          0  -2.043244 182.1024          77
150       150   170               1          1 -40.191861 144.9320         147
    end.limit first.tp label
25         28       25    tp
80         83       -1    fn
150       153      150    tp

otsad documentation built on Sept. 6, 2019, 5:02 p.m.