| rollAggregate | R Documentation |
Aggregate time series using a rolling window.
rollAggregate(x, FUN=sum, na.rm=FALSE, aggPeriods=4, ...)
x |
a time series object. |
FUN |
the function to use for aggregating. |
na.rm |
Logical indicating if |
aggPeriods |
the number of observations to use in the rolling window. |
... |
additional arguments passed to |
Functions rollAggregate uses apply with FUN on a rolling
window set by aggPeriods.
Partial windows may result in NA,
depending on FUN. NA in the beginning or end are
dropped if na.rm=TRUE.
The frequency of the resulting series remains the same as the original, but
the result will be aggPeriods shorter (i.e. have NAs).
The default will be a four period moving sum (which roughly converts weekly
data to something approximating monthly, but at a weekly frequency).
time series
as.annually,
as.quarterly,
as.weekly,
apply
rollAggregate(ts(1:20, start = c(1999,1), frequency=1))
rollAggregate(ts(1:20, start = c(1999,1), frequency=12), aggPeriods=3)
if(require("zoo")) {
z <- zoo(rnorm(100), order.by = Sys.Date() + 1:100)
rollAggregate(as.weekly(z), aggPeriods=4, FUN=mean)
require("tfplot")
tfplot(as.weekly(z),rollAggregate(as.weekly(z), aggPeriods=4,
FUN=mean, na.rm=TRUE))
tfplot(z,rollAggregate(z, aggPeriods=28, FUN=mean))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.