Reputation: 575
I am trying to calculate calendar year GDP growth for the GDPC96
time series from FRED (i.e. for a xts object). I am looking for a simple function without loops which calculate the calendar year growth where the variables are the data object (here GDPC96
), the frequency (here quarterly) and whether deprecated periods (such as 2013) shall be shown or not.
For example:
library(quantmod)
getSymbols("GDPC96",src="FRED")
a <- annualReturn(GDPC96,leading=FALSE)
tail(a)
I would like it to be such that the changes are per calendar year, i.e. it should calculate from 01.01.1947 to 01.01.1948 and so on. Then, for 2012, where data is only available through Oct, it should be omitted.
As far as I have seen none of the functions in PerformanceAnalytics and the related packages can do this properly.
Upvotes: 0
Views: 589
Reputation: 176648
It seems you want something like a year-over-year return calculation. I'm not aware of a function that does this automatically, but it's easy to do with the ROC
function in the TTR package.
library(quantmod)
getSymbols("GDPC96",src="FRED")
ROC(GDPC96, 4) # 4-period returns for quarterly data
getSymbols("SPY")
spy <- to.monthly(SPY)
ROC(spy, 12) # 12-period returns for monthly data
Update based on comments:
first.obs.by.year <- lapply(split(GDPC96, "years"),first)
last.obs.by.year <- lapply(split(GDPC96, "years"),last)
ROC(do.call(rbind, first.obs.by.year))
ROC(do.call(rbind, last.obs.by.year))
Upvotes: 2