Skip to contents

Trailing windows over the previous period ordinates, mirroring the C# MovingAverage and MovingSum. The result is shorter than the input by period - 1, and each ordinate carries the date of its window's LAST observation.

Usage

ts_moving_average(ts, period, min_valid_count = NULL)

ts_moving_sum(ts, period, min_valid_count = NULL)

Arguments

ts

a corehydro_ts.

period

the window length, which must be shorter than the series.

min_valid_count

the minimum number of observed values a window needs. Default NULL (strict).

Value

a corehydro_ts.

Details

min_valid_count controls what a window holding missing values does. The default – NULL, meaning period – propagates strictly: any missing value in the window gives a missing result, which matches pandas's default min_periods. A smaller value averages (or sums) the observed entries only; no rescaling is applied to a sum.

Examples

ts <- time_series(as.Date("2000-01-01") + 0:9, c(3, 1, 4, 1, 5, 9, 2, 6, 5, 3))
ts_moving_average(ts, period = 3)
#> <corehydro_ts> 8 ordinates, interval "one_day"
#>   2000-01-03 to 2000-01-10
#>   2000-01-03  2.666667
#>   2000-01-04  2
#>   2000-01-05  3.333333
#>   2000-01-06  5
#>   2000-01-07  5.333333
#>   ... 3 more