Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregating time series to yearly data

Consider we have daily time series of stock prices (let's say the FTSE Index). We want to calculate daily, monthly and yearly returns.

In order to compute monthly and yearly returns we have to aggregate time series data into months and years. In package "zoo" we have the aggregate function whoch can help us aggregating data to a monthly frequency. Below the code lines using the as.yearmon class:

# Computing simple returns
FTSERet = diff(FTSE)/lag(FTSE,k=-1)

# Monthly simple returns
MonRet <- aggregate(FTSERet+1, as.yearmon, prod)-1

# Quarterly simple returns
QuartRet <- aggregate(FTSERet+1, as.yearqtr, prod)-1

I have not found an equivalent class as as.yearmon for monthly data or as.yearqtr for quarterly data for aggregating to year data. Do you have any hint about that stuff?

like image 627
Lorenzo Rigamonti Avatar asked Oct 05 '22 13:10

Lorenzo Rigamonti


1 Answers

"yearmon" and "yearqtr" classes represent dates as year + fraction so:

as.year <- function(x) as.integer(as.yearmon(x))

Also note this construct: diff(x, arithmetic = FALSE) - 1

like image 150
G. Grothendieck Avatar answered Oct 10 '22 13:10

G. Grothendieck