Rolling Time Series Aggregate

Share:

Description

Aggregate time series using a rolling window.

Usage

1
    rollAggregate(x, FUN=sum, na.rm=FALSE, aggPeriods=4, ...)

Arguments

x

a time series object.

FUN

the function to use for aggregating.

na.rm

Logical indicating if NA should be removed from the beginning and end of a series

aggPeriods

the number of observations to use in the rolling window.

...

additional arguments passed to apply

Details

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).

Value

time series

See Also

as.annually, as.quarterly, as.weekly, apply

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
  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))
    }