impute_anomalies: Impute detected anomalies before fitting

View source: R/diagnostics.R

impute_anomaliesR Documentation

Impute detected anomalies before fitting

Description

Runs detect_anomalies(), replaces flagged points with NA, and re-interpolates with the same fill idiom used throughout this package (zoo::na.approx(), remaining edge NAs filled with the series mean) – a cleaned series to fit on instead of the raw one, so a handful of spikes/dips don't distort the fitted model the way they would untouched. Distinct from decompose_series()'s robust argument, which downweights outliers during decomposition rather than removing them before fitting; the two can be used independently or together.

Usage

impute_anomalies(
  df,
  date_agg = "day",
  method = c("iqr", "zscore"),
  threshold = 1.5
)

Arguments

df

A tibble with ds/y columns.

date_agg

Aggregation frequency: one of "hour", "day", "week", "month", "quarter", "year".

method

"iqr" (the default) or "zscore" – passed through to detect_anomalies().

threshold

Numeric sensitivity threshold, passed through to detect_anomalies(). Default 1.5.

Value

A tibble with columns ds, y (the cleaned series). The count of points that were flagged and replaced is attached as attr(result, "n_imputed").

Examples

df <- tibble::tibble(ds = as.Date("2024-01-01") + 0:29, y = c(rep(10, 29), 100))
impute_anomalies(df, date_agg = "day", method = "iqr")

forecastsuite documentation built on Oct. 18, 2026, 5:08 p.m.