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?
"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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With