Hi Martin,<div><br></div><div>Attached is a script I made for you to demonstrate a few of the many possible approaches you could take.  Below, I pasted the output from the script.</div><div><br></div><div>HTH,</div><div>Garrett</div>
<div><br></div><div><div>R> # There are lots of ways to do this. First, I'll do something similar to </div><div>R> # what you have.  i.e. use the "monthlyReturn" function and do each stock separately</div>
<div>R> </div><div>R> library(quantmod) </div><div>R> s <- c("DD", "DIS", "PFE") </div><div>R> getSymbols(s)</div><div>[1] "DD"  "DIS" "PFE"</div>
<div><br></div><div>R> ### 1</div><div>R> </div><div>R> # use leading=FALSE so that monthlyReturn won't replace the first observation </div><div>R> # The first row will be NA, so use -1 subset to remove it</div>
<div>R> DD.m1 <- monthlyReturn(Ad(DD), leading=FALSE)[-1,]</div><div>R> # apply a rolling function to convert 1 month returns to 2 and 3 month returns</div><div>R> DD.m2 <- rollapplyr(DD.m1, width=2, FUN=function(x) prod(1 + x) - 1)</div>
<div>R> DD.m3 <- rollapplyr(DD.m1, width=3, FUN=function(x) prod(1 + x) - 1)</div><div>R> # then you'd do this for each symbol</div><div>R> </div><div>R> ### 2</div><div>R> </div><div>R> # Or, you can use log returns so that you can use the runSum function, then "unlog" them at the end</div>
<div>R> DD.m <- monthlyReturn(Ad(DD), type='log', leading=FALSE)[-1,]</div><div>R> DD.m1 <- exp(runSum(DD.m, n=1)) - 1</div><div>R> DD.m2 <- exp(runSum(DD.m, n=2)) - 1</div><div>R> DD.m3 <- exp(runSum(DD.m, n=3)) - 1</div>
<div>R> </div><div>R> ### 3</div><div>R> </div><div>R> # Alternatively, you could convert the prices to monthly first, and then calculate the returns</div><div>R> mDD <- to.monthly(DD)</div><div>R> # For univariate series, you can use Delt or ROC</div>
<div>R> DD.m1 <- Delt(Ad(mDD))</div><div>R> DD.m2 <- ROC(Ad(mDD), n=2, type='discrete')</div><div>R> DD.m3 <- Delt(Ad(mDD), k=3) </div><div>R> # then you'd do this for each symbol</div><div>
R> </div><div>R> ### 4</div><div>R> </div><div>R> # Since you're only dealing with the Adjusted price of each Symbol, it's probably better</div><div>R> # to merge all the relevant prices into a data.frame</div>
<div>R> p <- cbind(Ad(DD), Ad(DIS), Ad(PFE))</div><div>R> # convert to monthly prices</div><div>R> mp <- to.monthly(p, OHLC=FALSE)</div><div>R> # calculate 1 month, 2 month, and 3 month returns with ROC</div>
<div>R> ret1 <- ROC(mp, n=1, type='discrete')</div><div>R> ret2 <- ROC(mp, n=2, type='discrete')</div><div>R> ret3 <- ROC(mp, n=3, type='discrete')</div><div><br></div><div>R> head(ret1)</div>
<div>          DD.Adjusted DIS.Adjusted PFE.Adjusted</div><div>Jan 2007           NA           NA           NA</div><div>Feb 2007  0.031112200 -0.025954654  -0.03851709</div><div>Mar 2007 -0.025659301  0.005206738   0.01201803</div>
<div>Apr 2007 -0.005120702  0.015843998   0.04750124</div><div>May 2007  0.071813725  0.013197361   0.05007085</div><div>Jun 2007 -0.028355820 -0.036708111  -0.06972560</div><div><br></div><div>R> head(ret2)</div><div>
          DD.Adjusted DIS.Adjusted PFE.Adjusted</div><div>Jan 2007           NA           NA           NA</div><div>Feb 2007           NA           NA           NA</div><div>Mar 2007  0.004654581  -0.02088305  -0.02696196</div>
<div>Apr 2007 -0.030648610   0.02113323   0.06009014</div><div>May 2007  0.066325287   0.02925046   0.09995052</div><div>Jun 2007  0.041421569  -0.02399520  -0.02314596</div><div><br></div><div>R> # for many different return periods, you can use lapply (or a for loop if you prefer)</div>
<div>R> rets <- lapply(1:5, function(n) ROC(mp, n=n, type='discrete'))</div><div><br></div><div>R> head(rets[[1]]) # 1 month returns</div><div>          DD.Adjusted DIS.Adjusted PFE.Adjusted</div><div>Jan 2007           NA           NA           NA</div>
<div>Feb 2007  0.031112200 -0.025954654  -0.03851709</div><div>Mar 2007 -0.025659301  0.005206738   0.01201803</div><div>Apr 2007 -0.005120702  0.015843998   0.04750124</div><div>May 2007  0.071813725  0.013197361   0.05007085</div>
<div>Jun 2007 -0.028355820 -0.036708111  -0.06972560</div><div><br></div><div>R> head(rets[[2]]) # 2 month returns</div><div>          DD.Adjusted DIS.Adjusted PFE.Adjusted</div><div>Jan 2007           NA           NA           NA</div>
<div>Feb 2007           NA           NA           NA</div><div>Mar 2007  0.004654581  -0.02088305  -0.02696196</div><div>Apr 2007 -0.030648610   0.02113323   0.06009014</div><div>May 2007  0.066325287   0.02925046   0.09995052</div>
<div>Jun 2007  0.041421569  -0.02399520  -0.02314596</div><div><br></div><div>R> # etc. </div><div>R> </div><div>R> # If you have a lot of symbols, you may be interested in the "PF" function from</div><div>
R> # my "qmao" package (which is on R-Forge) which will do the "cbind"ing for you.</div><div>R> # install.packages("qmao", repos='<a href="http://r-forge.r-project.org">http://r-forge.r-project.org</a>')</div>
<div>R> # Just to have a little fun, here is how you could quickly calculate 6 month returns for all Dow components</div><div>R> </div><div>R> library(qmao)</div><div>R> library(XML)</div><div>R> #First get the symbols</div>
<div>R> DJIsymbols <- function() {</div><div>R+     djicomp <- readHTMLTable('<a href="http://finance.yahoo.com/q/cp?s=">http://finance.yahoo.com/q/cp?s=</a>^DJI+Components', as.data.frame=FALSE)</div><div>
R+     as.data.frame(djicomp[[10]], stringsAsFactors=FALSE)[,1]</div><div>R+ }</div><div>R> syms <- DJIsymbols()</div><div>R> getSymbols(syms)</div><div> </div><div><snip></div><div><br></div><div> [1] "AA"   "AXP"  "BA"   "BAC"  "CAT"  "CSCO" "CVX"  "DD"  </div>
<div> [9] "DIS"  "GE"   "HD"   "HPQ"  "IBM"  "INTC" "JNJ"  "JPM" </div><div>[17] "KFT"  "KO"   "MCD"  "MMM"  "MRK"  "MSFT" "PFE"  "PG"  </div>
<div>[25] "T"    "TRV"  "UTX"  "VZ"   "WMT"  "XOM" </div><div><br></div><div>R> P <- PF(syms, prefer='Adjusted', silent=TRUE)</div><div>R> mP <- to.monthly(P, OHLC=FALSE)</div>
<div>R> # 6 month simple return of each Dow component</div><div>R> tail(ROC(mP, n=6, type='discrete'))</div><div>                  AA          AXP          BA        BAC</div><div>May 2011  0.28407351 0.2035731077  0.23722861  0.0751604</div>
<div>Jun 2011  0.03464052 0.2186982249  0.14537514 -0.1766917</div><div>Jul 2011 -0.10800971 0.1623512946  0.02564103 -0.2914536</div><div>Aug 2011 -0.23718713 0.1495587552 -0.06029515 -0.4266667</div><div>Sep 2011 -0.45594088 0.0008954556 -0.17166324 -0.5398496</div>
<div>Oct 2011 -0.31659776 0.0698151951 -0.13522771 -0.4000000</div><div>                CAT        CSCO         CVX          DD</div><div>May 2011  0.2616811 -0.12031662  0.31547017  0.15129714</div><div>Jun 2011  0.1467261 -0.22550000  0.14423508  0.10002054</div>
<div>Jul 2011  0.0269677 -0.23912004  0.11244071  0.02991712</div><div>Aug 2011 -0.1084622 -0.14931880 -0.03230860 -0.10561423</div><div>Sep 2011 -0.3312995 -0.09283196 -0.12502363 -0.26077307</div><div>Oct 2011 -0.1525943  0.06789413  0.01763505 -0.11636233</div>
<div>                  DIS          GE          HD        HPQ</div><div>May 2011  0.152866242  0.25913838  0.20906631 -0.1050471</div><div>Jun 2011  0.040789123  0.04708520  0.04779948 -0.1306122</div><div>Jul 2011 -0.006431695 -0.09720102 -0.03640912 -0.2262774</div>
<div>Aug 2011 -0.221307727 -0.21362530 -0.08971912 -0.4002780</div><div>Sep 2011 -0.300069622 -0.22741117 -0.09969871 -0.4471805</div><div>Oct 2011 -0.159860789 -0.14136386 -0.01311475 -0.3018491</div><div>                IBM        INTC        JNJ         JPM</div>
<div>May 2011 0.20426720  0.08098933 0.11277945  0.16361650</div><div>Jun 2011 0.17881297  0.07125427 0.09463722 -0.02866089</div><div>Jul 2011 0.13197874  0.05738881 0.10324686 -0.08946412</div><div>Aug 2011 0.07135735 -0.04597156 0.08994534 -0.18622951</div>
<div>Sep 2011 0.08184855  0.07614725 0.09395397 -0.33894178</div><div>Oct 2011 0.10864679  0.09802198 0.01579436 -0.18394128</div><div>                KFT         KO        MCD          MMM</div><div>May 2011 0.17773973 0.06532988 0.05772496  0.137328642</div>
<div>Jun 2011 0.13811075 0.03789408 0.11562542  0.112147326</div><div>Jul 2011 0.14472801 0.09783845 0.19239311  0.003012746</div><div>Aug 2011 0.11960026 0.11808885 0.21160547 -0.088932806</div><div>Sep 2011 0.08884565 0.03271171 0.17062117 -0.222462905</div>
<div>Oct 2011 0.07175295 0.03622971 0.20826318 -0.156162100</div><div>                   MRK          MSFT          PFE</div><div>May 2011  0.0901365706  0.0024203308  0.342405063</div><div>Jun 2011  0.0014359563 -0.0565899963  0.200353565</div>
<div>Jul 2011  0.0521060842  0.0003673769  0.078142695</div><div>Aug 2011  0.0394026057  0.0137195122  0.006896552</div><div>Sep 2011  0.0133250697 -0.0071798963 -0.111111111</div><div>Oct 2011 -0.0008537279  0.0543180930 -0.035053554</div>
<div>                   PG           T         TRV         UTX</div><div>May 2011  0.114454776  0.16844512  0.16457143  0.17840249</div><div>Jun 2011  0.003691221  0.09985580  0.06161746  0.13621091</div><div>Jul 2011 -0.009902597  0.09332322 -0.00744507  0.02965465</div>
<div>Aug 2011  0.026820546  0.03200883 -0.14726436 -0.10032715</div><div>Sep 2011  0.042588588 -0.04195089 -0.16814612 -0.15867512</div><div>Oct 2011  0.013782302 -0.01620906 -0.04572437 -0.10158192</div><div>                   VZ          WMT           XOM</div>
<div>May 2011  0.183733948  0.040425128  0.2131675201</div><div>Jun 2011  0.067766647 -0.001135933  0.1252609603</div><div>Jul 2011  0.016647196 -0.047159505 -0.0001261352</div><div>Aug 2011  0.005071851  0.044784915 -0.1239199905</div>
<div>Sep 2011 -0.020248380  0.010907674 -0.1260979425</div><div>Oct 2011  0.023110386  0.053844735 -0.0624784260</div></div><div><br></div><div><br></div><div><br><br><div class="gmail_quote">On Sat, Oct 29, 2011 at 3:42 PM, Martin Bauer <span dir="ltr"><<a href="mailto:Bauermartin@gmx.at">Bauermartin@gmx.at</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">-------- Original-Nachricht --------<br>
Datum: Fri, 28 Oct 2011 18:13:14 +0200<br>
Von: "Martina Bauer" <<a href="mailto:Bauermartin@gmx.at">Bauermartin@gmx.at</a>><br>
An: r-sig-finance@r-project.or<br>
Betreff: runMult instead of runSum<br>
<br>
Hello,<br>
<br>
Maybe my approach is wrong from the beginning but I would like to calculate from monthly data - one months and two months and lastly three months return<br>
I know that I have to multiply the monthly performance instead to sum up - but I dont know how to do in R. Finally I should also rank the result according to n period performance (no clue how to archive this in R)<br>
<br>
<br>
here my code<br>
<br>
require(quantmod)<br>
stock=c("DD","DIS","PFE")<br>
DD.sp=Ad(DD)<br>
dd.m=monthlyReturn(DD.sp)<br>
dd1=runSum(dd.m,n=1)<br>
dd2=runSum(dd.m,n=2)<br>
dd3=runSum(dd.m,n=3)<br>
<br>
DIS.sp=Ad(DIS)<br>
dis.m=monthlyReturn(DIS.sp)<br>
dis1=runSum(DIS.m,n=1)<br>
dis2=runSum(DIS.m,n=2)<br>
dis3=runSum(DIS.m,n=3)<br>
<br>
PFE.sp=Ad(PFE)<br>
pfe.m=monthlyReturn(PFE.sp)<br>
pfe1=runSum(pfe.m,n=1)<br>
pfe2=runSum(pfe.m,n=2)<br>
pfe3=runSum(pfe.m,n=3)<br>
<br>
<br>
I know that my R code is very poor any idea how to improve ?<br>
<br>
THanks in advance<br>
Best Regards<br>
<br>
martin<br>
<br>
--<br>
<font color="#888888"><br>
<br>
<br>
<br>
--<br>
<br>
_______________________________________________<br>
<a href="mailto:R-SIG-Finance@r-project.org">R-SIG-Finance@r-project.org</a> mailing list<br>
<a href="https://stat.ethz.ch/mailman/listinfo/r-sig-finance" target="_blank">https://stat.ethz.ch/mailman/listinfo/r-sig-finance</a><br>
-- Subscriber-posting only. If you want to post, subscribe first.<br>
-- Also note that this is not the r-help list where general R questions should go.<br>
</font></blockquote></div><br></div>