2
$\begingroup$

I have the daily closed values of the initial index for DJUSER, MSCI, SP500, SPGSCI from 1 January 1999 to 31 December 2011. I want to transform them in to data of rolling annual returns.

How to do it using R? which package do I need to use?

The density of the rolling annual returns associate to each data should be similar to the graph in the picture :

enter image description here

I can send to you the initial data if you want to try.

$\endgroup$
4
  • 1
    $\begingroup$ This question is probably offtopic. Note that R is a programming language and you can code any algorithm you want. It would help a lot if you define a formula of what is a rolling annual return and be more precise of what kind of data you have (daily, hourly, equally spaced, etc.) $\endgroup$
    – mpiktas
    Sep 17, 2013 at 11:08
  • $\begingroup$ I have daily closed values. $\endgroup$
    – Lea
    Sep 17, 2013 at 12:32
  • $\begingroup$ Please define exactly (with mathematic formula preferably) what do you mean by rolling annual return. Judging by quick google search on term rolling annual return, I can think a number of ways these returns can be calculated. From implementation point of view they are all similar, but for you naturally only one precise definition matters. $\endgroup$
    – mpiktas
    Sep 18, 2013 at 10:27
  • $\begingroup$ The graphs do not help, since it is not clear what are the blue and red lines. $\endgroup$
    – mpiktas
    Sep 18, 2013 at 10:29

2 Answers 2

2
$\begingroup$

Using the TTR package's ROC() function makes this incredibly easy:

ROC(prices, n = 252, type = "discrete")
$\endgroup$
4
  • $\begingroup$ @Downvoter: It is good practice to give a reason for the downvote and, even better, give an idea how to improve the answer - Thank you. $\endgroup$
    – vonjd
    Sep 17, 2013 at 16:06
  • $\begingroup$ I used this package's TTR and Roc(), but I did get the right answer and even I can not plot the density of the transformed data. $\endgroup$
    – Lea
    Sep 17, 2013 at 18:47
  • $\begingroup$ @timelyportfolio: Good to see you here :-) $\endgroup$
    – vonjd
    Sep 18, 2013 at 10:17
  • $\begingroup$ This answer assumes that the data has a very nice structure. Wouldn't leap years cause a problem? $\endgroup$
    – mpiktas
    Sep 18, 2013 at 10:37
2
$\begingroup$

You can use the zoo package:
http://cran.r-project.org/web/packages/zoo/index.html

You are very, very flexible to do all kinds of things, including aggregation and rolling functions:
http://cran.r-project.org/web/packages/zoo/vignettes/zoo-quickref.pdf

See e.g. p. 6ff. there

$\endgroup$
2
  • $\begingroup$ I used the package Performance analytics but it does not solve my problem $\endgroup$
    – Lea
    Sep 17, 2013 at 12:34
  • $\begingroup$ library(zoo) SP500 = read.table("SP500.csv",header=TRUE,sep=";") SP500 = as.numeric(SP500[,2]) ret_SP500 = diff(log(SP500)) ret_SP500 <- rollapply(ret_SP500,12,mean) $\endgroup$
    – Lea
    Sep 17, 2013 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.