smooth_outlier: Identify and replace outliers

View source: R/prepare_data.R

smooth_outlierR Documentation

Identify and replace outliers

Description

Identify outliers in a numeric time series and replace them with smoothed values.

Usage

smooth_outlier(x, periods, ...)

Arguments

x

Numeric vector containing the time series observations.

periods

Numeric vector giving the seasonal periods of the time series, for example 12 for monthly data with yearly seasonality or c(24, 168) for hourly data with daily and weekly seasonality.

...

Further arguments passed to forecast::msts() or forecast::tsoutliers().

Details

smooth_outlier() is a small wrapper around forecast::tsoutliers(). The input vector is first converted to an msts object using the seasonal periods supplied in periods.

For non-seasonal time series, forecast::tsoutliers() uses a supsmu-based approach. For seasonal time series, the series is decomposed using STL and outliers are identified on the remainder component. Detected outliers are replaced by the replacement values returned by forecast::tsoutliers().

The function returns a plain numeric vector with the same length as the input.

Value

A numeric vector where detected outliers are replaced by smoothed values.

See Also

Other data preparation: check_data(), interpolate_missing()

Examples

library(dplyr)

x <- M4_monthly_data |>
  filter(series == first(series)) |>
  pull(value)

x_outlier <- x
x_outlier[20] <- x_outlier[20] * 5

x_smoothed <- smooth_outlier(
  x = x_outlier,
  periods = 12
)

x_outlier[20]
x_smoothed[20]

hourly <- elec_price |>
  filter(bidding_zone == "DE") |>
  slice_head(n = 24 * 14) |>
  pull(value)

hourly_outlier <- hourly
hourly_outlier[48] <- hourly_outlier[48] * 5

smooth_outlier(
  x = hourly_outlier,
  periods = c(24, 168)
)

tscv documentation built on May 13, 2026, 9:07 a.m.