detector_update: Update detector with new observation(s)

View source: R/RcppExports.R

detector_updateR Documentation

Update detector with new observation(s)

Description

Adds new observation(s) to the detector's internal state and updates sufficient statistics.

Usage

detector_update(info_ptr, y)

Arguments

info_ptr

External pointer to detector created by detector_create().

y

Numeric vector of new observation(s). For univariate detectors, this should be a scalar (length-1 vector). For multivariate detectors, this should be a vector matching the number of dimensions.

Value

An external pointer to the detector (the same object that was passed in). The detector is updated *in place* — no copy is made — so this return value is provided only for convenience, for example when using the native R pipe operator ('|>') e.g., det |> detector_update(y) |> get_statistics().

Examples

# Univariate example
det <- detector_create(type = "univariate")
detector_update(det, 0.5)
detector_update(det, 1.2)

# Multivariate example
det_mv <- detector_create(type = "multivariate")
detector_update(det_mv, c(0.5, 1.2, -0.3))

## Online (sequential) example
# Generate data with a changepoint
set.seed(123)
Y <- c(rnorm(500, mean = 0), rnorm(500, mean = 1))
det <- detector_create(type = "univariate")
stat_trace <- numeric(length(Y))
threshold <- 20
for (i in seq_along(Y)) {
  detector_update(det, Y[i])
  r <- get_statistics(det, family = "gaussian")
  stat_trace[i] <- r$stat
  if (!is.null(r$stat) && r$stat > threshold) {
    cat("Online detection at", i, "estimate tau =", r$changepoint, "\n")
    plot(stat_trace[1:i], type = "l", ylab = "Test Statistic", xlab = "Time")
    break
  }
}



focus documentation built on March 30, 2026, 5:08 p.m.