rollAggregate: Rolling Time Series Aggregate

Description Usage Arguments Details Value See Also Examples

View source: R/utils.R

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

Example output

Loading required package: tframe
Time Series:
Start = 1999 
End = 2018 
Frequency = 1 
      Series 1
 [1,]       NA
 [2,]       NA
 [3,]       NA
 [4,]       10
 [5,]       14
 [6,]       18
 [7,]       22
 [8,]       26
 [9,]       30
[10,]       34
[11,]       38
[12,]       42
[13,]       46
[14,]       50
[15,]       54
[16,]       58
[17,]       62
[18,]       66
[19,]       70
[20,]       74
     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1999  NA  NA   6   9  12  15  18  21  24  27  30  33
2000  36  39  42  45  48  51  54  57                
Loading required package: zoo

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric

Loading required package: tfplot

tframePlus documentation built on May 29, 2017, 9:18 p.m.