Calendar Year Return Calculation - r

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.

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))

Related

Analyzing disparate time series in R

Are there tools in R that simplify analysis of lagged and disparate time series. For example:
Daily values that only occur on weekdays (no entry on weekends or holidays)
vs
Bi-annual values
What I'm seeking is ways to:
Complete the missing daily values (with interpolated, or last value rolled forward, etc.)
Look for correlation between daily values and the bi-annual value (only the values that came before the bi-annual event)
As an example:
10-year treasury note interest rate (daily on non-holiday weekdays) as "X" and i-bond fixed rate as "Y" (set May 1/Nov 1)
Any suggestions appreciated.
I've built a test dataset manually for "x" and used functions in zoo to populate the missing values (interpolated), but I'm hoping for a less "brute-force" method for looking at analyzing the disparate time series. I've used lag functions in the past, but those were on matching interval time series.
What Jon commented is what I had in mind:
expand a weekday time series to full week using missing value function(s) in zoo
Sample the daily value - say April 15 for the May 1, Oct 15 for Nov 1
Ideally be able to automate - say loop through April 1-30, Oct 1-30 to look for highest RSqr for the model of choice (linear, polynomial, etc.)
Not have to build discrete datasets for each of the above - but if that is what is required I can do it programmatically - I've done that with stock data in the past. I was looking for a more efficient means of selecting the datasets ad hoc during the analysis.
I don't have code to post, because I'm clueless as to the feature/function that would make the date selection I'm after possible (at least in R).
Thanks for the input so far. It has already been useful in helping me look at alternative methods to achieve what I'm after.

Forecasting future quarters using SMA or EMA in r by group

I am trying to forecast using the SMA function or EMA function (TTR Package https://www.rdocumentation.org/packages/TTR/versions/0.24.2/topics/SMA) in r for time series. Here is an example of the data frame I am working with:
Product<- c("Watch","Watch","Watch","Phone","Phone","Phone","Computer","Computer","Computer")
Region<- c("US","US","US","China","China","China","US","US","US")
Quarter<- c("03/31/2020","06/30/2020","09/30/2020","03/31/2020","06/30/2020","09/30/2020","03/31/2020","06/30/2020","09/30/2020")
Amount<-c(1000,2000,2100,500,600,620,100,110,105)
df<-data.frame(Product,Region,Quarter,Amount)
How can I use the sma function or ema to forecast a select amount of quarters in r? I was able to do something similar with the auto.arima function, but the forecast was way off so I want to just implement a simple moving average or an ema. I also want to keep the data frame in the same format, so I just want to add extra observations or even convert it to wide with the new columns being the quarter with the amount, either is fine.
Auto.Arima Example:
test1<-df %>% dplyr::group_by(Product,Region) %>% do(data.frame(amount_2 = forecast(auto.arima(.$Amount)$mean, h=3)))

Quantmod - Chop data and constructing matrix of return series

I am having trouble with my R assignment I am working on this semester.
Here is the part that I am tasked with doing that I am confused about:
iv. Download 3 month TBill rate from Fred for the same sample period 01/01/1993 to 12/31/2013.
Useful Hints: You may have to chop the data to match the sample period.
v. Construct a matrix of return series combining Stock, S&P500, and TBill for the sample period.
Useful Hints:
Note that the rownames for the TBill may not match with the other two return series, as the dates do not match, although the month and year matches
You have to construct the row names for each of the series as Year – Month format (e.g. 1993-01) or delete the rownames from T-bill before you can combine all three series into one Return matrix.
You have to convert the Return matrix to a dataframe before you use the lm() function.
I tried this below like I have used getSymbols before for SPY and AAPL but it pulls an entire data set rather than the specific date range. How can I chop the data so it fits the desired date range?
getSymbols('TB3MS', src = 'FRED', from = "1993-01-01", to = "2013-12-31")
Next, how would I go about constructing the matrix of return series combining all of the stocks? Can anyone point me in the right direction?
Filtering an xts object: see examples in the xts documentation ?xts.
# filter 1993 until 2013
TB3MS["1993/2013"]
But these dates are of, because tbills are at the first day of the month, the stock dates are the last day of the month. With the coredata you can extract the tbill data and stick it into the other timeseries if the rows match.
Taking the data example from your previous question, you could do something like this (and I'm creating more steps than needed, you could combine a few statements into one):
# create monthly returns of the spy data and give the column a better name than monthly.returns
spy_returns <- monthlyReturn(SPY)
colnames(spy_returns) <- "SPY_returns"
# filter the tbill data
TB3MS_1993_2013 <- TB3MS["1993/2013"]
# add tbill data to spy data
spy_returns$TB3MS <- coredata(TB3MS_1993_2013)
Merging xts objects can just be done with merge. They will be merged on the dates.
merge(spy_returns, aapl_returns) would combine these two. If you have a lot of tickers, use Reduce (check help and SO on how to use Reduce with merge) but better would be to use the tidyquant package if allowed.

time series in R with sales prediction with only date values

i have a data with date(2015)with mm/dd/yy format and sales. I need to predict sales for 2016 with the given data. I just know, I need to use time series forecasting. However no idea. Since, many examples have only year like(1960,1970,..) my data has only one year with several months. Don't know how to plot too. can you give me a clear structure how to proceed?
Assuming that the date is in string and in the format mm/dd/yy
convert string into date by using this code
a <- "07/23/15"
b <- as.Date(a, format = "%m/%d/%y")
fullYear <- format(b,'%Y') // to get 2015 as year
halfYear <- format(b, '%y') //to get 15 as year
After this you can work on
I have found the solution. Converted sales figure into time series format.
plotted the data and seen whether there is any trend/Seasonality.
Since the data has only trend applied holts exponential smoothing under forecast package. Sales of 2016 has been found and plotted.

Simple time series analysis with R: aggregating and subsetting

I want to convert monthly data into quarterly averages. These are my 2 datasets:
gas <- UKgas
dd <- UKDriverDeaths
I was able to accomplish (I think) for the dd data as so:
dd.zoo <- zoo(dd)
ddq <- aggregate(dd.zoo, as.yearqtr, mean)
However I cannot figure out how to do this with the gas data...any help?
Follow-up
When I try to subset the data based on date (1969-1984) the resulting data does not include 1969 Q1 and instead includes 1985 Q1...any suggestions on how to fix this? I was just trying to subset as gas[1969:1984].
Originally I did not plan to post answer, as it looks like you did not pre-check your UKgas dataset to see that it is already a quarterly time series.
But the follow-up question is worth answering. "ts" object comes with many handy generic functions. We can use window to easily subset a time series. To extract the section between first quarter of 1969 and the final quarter of 1984, we can use
window(UKgas, start = c(1969,1), end = c(1984,4))
The result will still be a quarterly time series.
On the other hand, if we use "[" for subsetting, we lose object class:
class(UKgas[1:12])
#[1] "numeric"

Resources