Skip to contents

Classical additive decomposition into trend, seasonal and residual components, with the seasonal part extracted by keeping only the harmonics of the seasonal frequency.

Usage

ts_seasonal_decompose(ts, period)

Arguments

ts

a corehydro_ts.

period

the seasonal period in ordinates (12 for monthly data with an annual cycle). The series must span at least two complete periods.

Value

a data frame of date, trend, seasonal and residual, one row per input ordinate.

Details

The trend is a centred moving average over period ordinates, so it – and the residual with it – is undefined for the first period - 1 ordinates, reported as NA. The seasonal component is defined everywhere. Where all three are defined they add back to the original value exactly.

Examples

dates <- seq(as.Date("2000-01-01"), by = "month", length.out = 48)
value <- 100 + 0.5 * seq_along(dates) + 10 * sin(2 * pi * seq_along(dates) / 12)
head(ts_seasonal_decompose(time_series(dates, value, "one_month"), period = 12))
#>         date trend   seasonal residual
#> 1 2000-01-01   NaN  5.5872585      NaN
#> 2 2000-02-01   NaN  5.2646107      NaN
#> 3 2000-03-01   NaN  3.7037149      NaN
#> 4 2000-04-01   NaN  0.9308957      NaN
#> 5 2000-05-01   NaN -1.5856478      NaN
#> 6 2000-06-01   NaN -3.9513834      NaN