## ----setup, include = FALSE-----------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
old_options <- options(width = 70)

## ----load-----------------------------------------------------------
library(deriva)

## ----data-----------------------------------------------------------
head(credit_monitoring)

## ----quickstart-----------------------------------------------------
result <- detect_drift(credit_monitoring, .col = error, method = "ddm")

# Where was drift flagged?
subset(result, .drift)

## ----spec-----------------------------------------------------------
spec <- drift_detector("ddm", min_instances = 30)
spec

## ----fit------------------------------------------------------------
baseline <- credit_monitoring[credit_monitoring$t <= 500, ]

fitted <- fit(spec, baseline, signal = error)
fitted

## ----advance--------------------------------------------------------
batch1 <- credit_monitoring[credit_monitoring$t >= 501 & credit_monitoring$t <= 700, ]
batch2 <- credit_monitoring[credit_monitoring$t >= 701, ]

fitted2 <- advance(fitted,  batch1)
fitted3 <- advance(fitted2, batch2)
fitted3

## ----augment--------------------------------------------------------
history <- augment(fitted3)
tail(history[, c("t", "error", ".phase", ".warning", ".drift")], 10)

## ----tidy-----------------------------------------------------------
tidy(fitted3)

## ----glance---------------------------------------------------------
glance(fitted3)

## ----autoplot, eval = requireNamespace("ggplot2", quietly = TRUE), fig.width = 6.5, fig.height = 3.5----
library(ggplot2)
autoplot(fitted3)

## ----bridge---------------------------------------------------------
# Simulate tidymodels augment() output for a classifier
predictions <- data.frame(
  time     = 1:8,
  truth    = factor(c("yes","no","yes","yes","no","yes","no","yes")),
  .pred_class = factor(c("yes","no","yes","no" ,"no","no" ,"no","yes"))
)

add_prediction_error(predictions, truth = truth)

## ----cleanup, include = FALSE-------------------------------------------------
options(old_options)

