Learn R Programming

tframePlus (version 2024.2-1)

rollAggregate: Rolling Time Series Aggregate

Description

Aggregate time series using a rolling window.

Usage

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

Value

time series

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

See Also

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

Examples

Run this code
  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))
    }

Run the code above in your browser using DataLab