frolladapt {data.table} | R Documentation |
Adapt rolling window to irregularly spaced time series
Description
Helper 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.
Usage
frolladapt(x, n, align="right", partial=FALSE, give.names=FALSE)
Arguments
x |
Integer. Must be sorted with no duplicates or missing values. Other objects with numeric storage (including most commonly |
n |
Integer, positive, rolling window size. Up to |
align |
Character, default |
partial |
Logical, default |
give.names |
Logical, default |
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
.
See Also
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
dt[, n3 := frolladapt(index, n=3L)]
dt
dt[, rollmean3 := frollmean(value, n3, adaptive=TRUE)]
dt
dt[, n3p := frolladapt(index, n=3L, partial=TRUE)]
dt[, rollmean3p := frollmean(value, n3p, adaptive=TRUE)]
dt
n34 = frolladapt(idx, c(small=3, big=4), give.names=TRUE)
n34
dt[, frollmean(value, n34, adaptive=TRUE, give.names=TRUE)]