Skip to content

Instantly share code, notes, and snippets.

@alexland
Last active August 29, 2015 14:03
Show Gist options
  • Save alexland/21a596e3a04cac0d719d to your computer and use it in GitHub Desktop.
Save alexland/21a596e3a04cac0d719d to your computer and use it in GitHub Desktop.
roll-up (aka 'group by') functions for time series data in R
require(zoo)
require(xts)
# TSR is an R time series comprised of a datetime index and a single column of data
# created like so:
# mock a datetime index, by creating a data vector comprised of the previous 500 days
nd = Sys.Date()
st = 500 - nd
idx = seq(from=st, to = nd, by='days')
data = 10* runif(length(idx))
# create the R time series, using the R package 'xts'
TSR = as.xts(data, order.by=idx)
# to roll-up this daily xts series:
# step 1: create index of endpoints for the time period to roll-up to
np_week = endpoints(TSR, on='weeks')
np_month = endpoints(TSR, on='months')
# step 2: rollup the time series
# this is essentially a a 'group by' operation so you need you to specify a function
# to which the continuous data is passed for aggregation (most common are sum, and mean)
tsr_week = period.apply(TSR, INDEX=np_week, FUN=mean)
trs_month = period.apply(TSR, INDEX=np_month, FUN=mean)
ts_split = function(x) {
as.Date(unlist(strsplit(as.character(x), split=' ',
fixed=TRUE))[1],
format="%Y-%m-%d",
origin="1970-01-01")
}
acf1 = acf(TSR_day,
lag.max=70,
drop.lag.0=T,
plot=F
)
#---------- plotting -------------#
op = par(mar=c(4,4,1,2), mfrow=c(2,1), cex=0.8,
col.lab="steelblue4", col=#D96A14"
plot(acf1$lag[,1,1], acf1$acf[,1,1],
ann=F, cex.axis=.8, xlab=F,
ylab=F, main=NULL, col="#D96A14",
type='h'
)
#---------------------------- filters -------------------------------#
# linear filter
lf1 = filter( x, filter, method=c("convolution", "recursive"),
sides=2, circular=F, init)
# three-point simple moving average
# ie, if the points reprsent days, then this is a 3-day moving avg
# just take a given day, and the day before, and the day after, then average them (/3)
mv_avg = function(x) {
y = numeric(length(x)-2)
for (i in 2:(length(x)-1)) {
y[i] = (x[i-1] + x[i] + x[i+1])/3
}
y
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment