| detector_update | R Documentation |
Adds a new observation to the detector's internal state, updates the sufficient statistics and prunes the set of candidate changepoints.
detector_update(det_ptr, y, lambda = 1)
det_ptr |
A |
y |
Numeric vector with the new observation. For univariate detectors, this should be a scalar (length-1 vector). For multivariate detectors, this should be a vector matching the number of dimensions. |
lambda |
Numeric scalar. Weight of the new observation: the internal
time counter is incremented by |
The same "focus_detector" object that was passed in. The detector is
updated in place (no copy is made), so the return value is provided
only for convenience, for example to chain calls with the native pipe
operator, as in
det |> detector_update(y) |> get_statistics(family = "gaussian").
# 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
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.