ts_analysis: Compute ts anomaly detection models for all timeseries within...

View source: R/ts_analysis.R

ts_analysisR Documentation

Compute ts anomaly detection models for all timeseries within a group.

Description

Compare each ts to a set of comparable timeseries in the pre-period and evaluate deviation from average in the post-period to detect anomalous behavior.

Usage

ts_analysis(
  group_timeseries_data,
  group_id = NA,
  start_date = NULL,
  period = "month",
  min_pre_periods,
  min_post_periods,
  min_timeseries = 30,
  sig_p = 0.05,
  save_model_data = TRUE,
  save_model = TRUE,
  use_cache = FALSE
)

Arguments

group_timeseries_data

contains time-series data for each ts over the same period of time. Requires the following columns:

  • date: the date by which activity was recorded.

  • ts_id: a unique identifier for the ts.

  • count: number of units of activity for the corresponding date/ts.

group_id

unique identifier for the group

start_date

date separating the pre-period (matching) vs post-period evaluating. Uses detect_changepoint if missing (NULL).

period

unit of time to compare timeseries with.

min_pre_periods

number of time units to qualify as an active ts in pre-period.

min_post_periods

number of time units to qualify as an active ts in post-period.

min_timeseries

minimum number of active timeseries to run the analysis.

sig_p

p-value threshold to determine statistical significance.

save_model_data

save_model_data whether to save analysis data.

save_model

save_model whether to save user models.

use_cache

use saved data and models from previous run.

Value

group_id string for completed group ts analysis

Examples

set.seed(143)
num_dates <- 90
num_timeseries <- 30
# generate synthetic data
test_data <- setNames(
  merge(as.character(1:num_timeseries),
        seq(Sys.Date(), Sys.Date() + (num_dates - 1), by = 1),
        colnames = c("ts_id", "date")), c("ts_id", "date"))
start_date <- Sys.Date() + floor(num_dates / 2)
test_data$count <- sapply(1:(num_dates*num_timeseries),
                          function(x) { rpois(1, 50) })
# ts anomalies
test_data[test_data$ts_id == "1" &
            test_data$date > start_date, "count"] <- 100
test_data[test_data$ts_id == "2" &
            test_data$date > start_date, "count"] <- 1

output_dir <- ts_analysis(
  test_data, "test_analysis", start_date = start_date, period = "day",
  sig_p = 0.01)
paste("ts analysis output in directory:", output_dir)

roboton/chwork documentation built on June 21, 2022, 6:18 a.m.