interpolate_missing: Interpolate missing values

View source: R/prepare_data.R

interpolate_missingR Documentation

Interpolate missing values

Description

Interpolate missing values in a numeric time series.

Usage

interpolate_missing(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::na.interp().

Details

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

For non-seasonal time series, missing values are replaced using linear interpolation. For seasonal time series, forecast::na.interp() uses an STL-based approach: the series is decomposed, the seasonally adjusted series is interpolated, and the seasonal component is added back.

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

Value

A numeric vector with missing values interpolated.

See Also

Other data preparation: check_data(), smooth_outlier()

Examples

library(dplyr)

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

x_missing <- x
x_missing[c(10, 20, 30)] <- NA

x_interpolated <- interpolate_missing(
  x = x_missing,
  periods = 12
)

anyNA(x_missing)
anyNA(x_interpolated)

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

hourly_missing <- hourly
hourly_missing[c(24, 48, 72)] <- NA

interpolate_missing(
  x = hourly_missing,
  periods = c(24, 168)
)

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