Adapt rolling window to irregularly spaced time series
frolladapt.RdHelper function to generate adaptive window size based on the irregularly spaced time series index, to be passed as n argument to adaptive froll function (or N argument to adaptive frollapply). Experimental.
Arguments
- x
Integer. Must be sorted with no duplicates or missing values. Other objects with numeric storage (including most commonly
DateandPOSIXct) will be coerced to integer, which, for example, in case ofPOSIXctmeans truncating to whole seconds. It does not support vectorized input.- n
Integer, positive, rolling window size. Up to
nvalues nearest to each value ofx, with distance in the units ofxand according to the window implied byalign, are included in each rolling aggregation window. Thus whenxis aPOSIXct,nare seconds, and whenxis aDate,nare days. It supports vectorized input, then it needs to be a vector.- align
Character, default
"right". Other alignments have not yet been implemented.- partial
Logical, default
FALSE. Should the rolling window size(s) provided innbe trimmed to available observations? For details seefroll.- give.names
Logical, default
FALSE. WhenTRUE, names are automatically generated corresponding to names ofn. If answer is an integer vector, then the argument is ignored, see examples.
Details
Argument n allows multiple values to generate multiple adaptive windows, unlike x, as mixing different time series would make no sense.
Value
When length(n)==1L then integer vector (adaptive window size) of length of x. Otherwise a list of length(n) having integer vectors (adaptive window sizes) of length of x for each window size provided in n.
Examples
idx = as.Date("2022-10-23") + c(0,1,4,5,6,7,9,10,14)
dt = data.table(index=idx, value=seq_along(idx))
dt
#> index value
#> <Date> <int>
#> 1: 2022-10-23 1
#> 2: 2022-10-24 2
#> 3: 2022-10-27 3
#> 4: 2022-10-28 4
#> 5: 2022-10-29 5
#> 6: 2022-10-30 6
#> 7: 2022-11-01 7
#> 8: 2022-11-02 8
#> 9: 2022-11-06 9
dt[, n3 := frolladapt(index, n=3L)]
#> index value n3
#> <Date> <int> <int>
#> 1: 2022-10-23 1 3
#> 2: 2022-10-24 2 3
#> 3: 2022-10-27 3 1
#> 4: 2022-10-28 4 2
#> 5: 2022-10-29 5 3
#> 6: 2022-10-30 6 3
#> 7: 2022-11-01 7 2
#> 8: 2022-11-02 8 2
#> 9: 2022-11-06 9 1
dt
#> index value n3
#> <Date> <int> <int>
#> 1: 2022-10-23 1 3
#> 2: 2022-10-24 2 3
#> 3: 2022-10-27 3 1
#> 4: 2022-10-28 4 2
#> 5: 2022-10-29 5 3
#> 6: 2022-10-30 6 3
#> 7: 2022-11-01 7 2
#> 8: 2022-11-02 8 2
#> 9: 2022-11-06 9 1
dt[, rollmean3 := frollmean(value, n3, adaptive=TRUE)]
#> index value n3 rollmean3
#> <Date> <int> <int> <num>
#> 1: 2022-10-23 1 3 NA
#> 2: 2022-10-24 2 3 NA
#> 3: 2022-10-27 3 1 3.0
#> 4: 2022-10-28 4 2 3.5
#> 5: 2022-10-29 5 3 4.0
#> 6: 2022-10-30 6 3 5.0
#> 7: 2022-11-01 7 2 6.5
#> 8: 2022-11-02 8 2 7.5
#> 9: 2022-11-06 9 1 9.0
dt
#> index value n3 rollmean3
#> <Date> <int> <int> <num>
#> 1: 2022-10-23 1 3 NA
#> 2: 2022-10-24 2 3 NA
#> 3: 2022-10-27 3 1 3.0
#> 4: 2022-10-28 4 2 3.5
#> 5: 2022-10-29 5 3 4.0
#> 6: 2022-10-30 6 3 5.0
#> 7: 2022-11-01 7 2 6.5
#> 8: 2022-11-02 8 2 7.5
#> 9: 2022-11-06 9 1 9.0
dt[, n3p := frolladapt(index, n=3L, partial=TRUE)]
#> index value n3 rollmean3 n3p
#> <Date> <int> <int> <num> <int>
#> 1: 2022-10-23 1 3 NA 1
#> 2: 2022-10-24 2 3 NA 2
#> 3: 2022-10-27 3 1 3.0 1
#> 4: 2022-10-28 4 2 3.5 2
#> 5: 2022-10-29 5 3 4.0 3
#> 6: 2022-10-30 6 3 5.0 3
#> 7: 2022-11-01 7 2 6.5 2
#> 8: 2022-11-02 8 2 7.5 2
#> 9: 2022-11-06 9 1 9.0 1
dt[, rollmean3p := frollmean(value, n3p, adaptive=TRUE)]
#> index value n3 rollmean3 n3p rollmean3p
#> <Date> <int> <int> <num> <int> <num>
#> 1: 2022-10-23 1 3 NA 1 1.0
#> 2: 2022-10-24 2 3 NA 2 1.5
#> 3: 2022-10-27 3 1 3.0 1 3.0
#> 4: 2022-10-28 4 2 3.5 2 3.5
#> 5: 2022-10-29 5 3 4.0 3 4.0
#> 6: 2022-10-30 6 3 5.0 3 5.0
#> 7: 2022-11-01 7 2 6.5 2 6.5
#> 8: 2022-11-02 8 2 7.5 2 7.5
#> 9: 2022-11-06 9 1 9.0 1 9.0
dt
#> index value n3 rollmean3 n3p rollmean3p
#> <Date> <int> <int> <num> <int> <num>
#> 1: 2022-10-23 1 3 NA 1 1.0
#> 2: 2022-10-24 2 3 NA 2 1.5
#> 3: 2022-10-27 3 1 3.0 1 3.0
#> 4: 2022-10-28 4 2 3.5 2 3.5
#> 5: 2022-10-29 5 3 4.0 3 4.0
#> 6: 2022-10-30 6 3 5.0 3 5.0
#> 7: 2022-11-01 7 2 6.5 2 6.5
#> 8: 2022-11-02 8 2 7.5 2 7.5
#> 9: 2022-11-06 9 1 9.0 1 9.0
n34 = frolladapt(idx, c(small=3, big=4), give.names=TRUE)
n34
#> $small
#> [1] 3 3 1 2 3 3 2 2 1
#>
#> $big
#> [1] 4 4 2 2 3 4 3 3 1
#>
dt[, frollmean(value, n34, adaptive=TRUE, give.names=TRUE)]
#> small big
#> <num> <num>
#> 1: NA NA
#> 2: NA NA
#> 3: 3.0 2.5
#> 4: 3.5 3.5
#> 5: 4.0 4.0
#> 6: 5.0 4.5
#> 7: 6.5 6.0
#> 8: 7.5 7.0
#> 9: 9.0 9.0