aggregate_xts {SEI}R Documentation

Aggregate values in xts objects

Description

Inputs an xts time series and outputs an xts time series whose values have been aggregated over a moving window of a user-specified length.

Usage

aggregate_xts(
  x,
  len,
  scale = c("days", "hours", "weeks", "quarters", "years"),
  fun = "sum",
  timescale = c("days", "hours", "weeks", "quarters", "years"),
  na_thres = 10
)

Arguments

x

xts object to be aggregated.

len

length of the aggregation period.

scale

timescale of the aggregation period, default is 'days'.

fun

function to apply to the aggregated data, default is 'sum'.

timescale

timescale of x, default is 'days'.

na_thres

threshold for the percentage of NA values allowed in the aggregation period, default = 10.

Details

This has been adapted from code available at https://github.com/WillemMaetens/standaRdized.

len is a single numeric value specifying over how many time units the data x is to be aggregated. By default, len is assumed to correspond to a number of days, but this can also be specified manually using the argument scale. scale must be one of: "days", "weeks", "months", "quarters", and "years".

fun determines the function used to aggregate the time series. By default, fun = "sum", meaning the aggregation results in accumulations over the aggregation period. Alternative functions can also be used. For example, specifying fun = "mean" would return the mean over the aggregation period.

timescale is the timescale of the input data x. By default, this is assumed to be "days".

Since the time series x aggregates data over the aggregation period, problems may arise when x contains missing values. For example, if interest is on daily accumulations, but 50% of the values in the aggregation period are missing, the accumulation over this aggregation period will not be accurate. This can be controlled using the argument na_thres. na_thres specifies the percentage of NA values in the aggregation period before a NA value is returned. i.e. the proportion of values that are allowed to be missing. The default is na_thres = 10.

Value

An xts time series with aggregated values.

Author(s)

Sam Allen, Noelia Otero

Examples



data(data_supply, package = "SEI")
# consider hourly German energy production data in 2019
supply_de <- subset(data_supply, country == "Germany", select = c("date", "PWS"))
supply_de <- xts::xts(supply_de$PWS, order.by = supply_de$date)

# daily accumulations
supply_de_daily <- aggregate_xts(supply_de, len = 1, timescale = "hours")

# weekly means
supply_de_weekly <- aggregate_xts(supply_de, len = 1, scale = "weeks", fun = "mean", "hours")

plot(supply_de, main = "Hourly energy production in Germany")
plot(supply_de_daily, main = "Daily energy production in Germany")
plot(supply_de_weekly, main = "Weekly energy production in Germany")




[Package SEI version 0.1.1 Index]