inst/doc/heimdall.R

## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 3.5
)

## ----setup, message = FALSE---------------------------------------------------
library(daltoolbox)
library(heimdall)

## -----------------------------------------------------------------------------
detect <- function(model, stream) {
  output <- list(obj = model, drift = FALSE)
  drifts <- integer(0)
  for (i in seq_along(stream)) {
    output <- update_state(output$obj, stream[i])
    if (output$drift) {
      drifts <- c(drifts, i)
      output$obj <- reset_state(output$obj)
    }
  }
  drifts
}

## -----------------------------------------------------------------------------
data(st_drift_examples)
serie <- st_drift_examples$uv_virtual_drift

drifts <- detect(dfr_adwin(), serie$serie)
drifts

## -----------------------------------------------------------------------------
plot(serie$serie, type = "l", col = "grey40", xlab = "observation", ylab = "serie")
abline(v = which(serie$event), lty = 2, col = "grey70")
abline(v = drifts, col = "firebrick", lwd = 2)
legend("bottomleft", c("true change", "detection"),
  lty = c(2, 1),
  col = c("grey70", "firebrick"), bty = "n", cex = 0.8
)

## -----------------------------------------------------------------------------
set.seed(42)
errors <- c(rbinom(500, 1, 0.1), rbinom(500, 1, 0.4))

detect(dfr_ddm(), errors)
detect(dfr_hddm(), errors)

## -----------------------------------------------------------------------------
plot(cumsum(errors) / seq_along(errors),
  type = "l", xlab = "observation",
  ylab = "cumulative error rate"
)
abline(v = 500, lty = 2, col = "grey70")
abline(v = detect(dfr_ddm(), errors), col = "firebrick", lwd = 2)

## -----------------------------------------------------------------------------
model <- fit(dfr_ddm(), errors)
model$drifted

## -----------------------------------------------------------------------------
model <- dfr_multi_criteria(
  drifter_list = list(adwin = dfr_adwin(), ph = dfr_page_hinkley()),
  combination = "or"
)
model <- fit(model, data.frame(serie = serie$serie))
model$drifted
model$detections

Try the heimdall package in your browser

Any scripts or data that you put into this service are public.

heimdall documentation built on Dec. 2, 2026, 5:08 p.m.