I am relatively new to R and have read as much as I have been able to on the topic but can't seem to find what I am looking for on the other questions.
I am looking to calculate the rate of change (momentum) using TTR and ROC for 12 periods, using monthly data but I would like to ignore the most recent month. In other words I am looking to find the ROC for t-2 till t-12 (January 2016 12 month momentum of a stock excluding January 2016). This is the norm in calculating momentum in portfolio construction literature.
My data is all stocks that have been listed on the South African stock exchange (JSE). The date header is the 1st column (i.e. the date is the variable in the rows) and the stocks are listed in the subsequent columns.
I know my code below is pretty straightforward however I have tried a few things and they have given errors. As I have about 250 stocks (columns) over 20 years, its not advisable to create a new lagged variable for each observation.
x <- Prices.df
x$DATE <- as.Date(x$DATE, format = "%Y/%m/%d")
y <- xts(x[,-1], order.by = x$DATE) library(TTR)
roc <- ROC(y, n = 12, type = "discrete")
Any help would be much appreciated.