[R] Fwd: how to calculate the return?

Patrick Burns pburns at pburns.seanet.com
Sat Nov 24 17:28:11 CET 2007


You don't need a loop.  If 'data' is a matrix of prices:

re <- diff(log(data))

creates a matrix of returns.


Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

Denver XU wrote:

>hi,scionforbai,
>Thank you very much.
>I made very stupid mistake for the number is not stock price.
>In fact, if the number is correct, data[i,1] is equivalent to data[i]
>
>
>
>2007/11/24, Scionforbai <scionforbai at gmail.com>:
>  
>
>>Hi Denver,
>>
>>    
>>
>>>I want  to calculate the return say AMR,so I use
>>>      
>>>
>>If data is a matrix  or a data.frame you need to use the correct
>>index: [row,col] instead of [row]. Try:
>>
>>re=numeric(10)
>>for (i in 2:nrow(data)) {
>>     re[1]=0
>>     re[i]=log(data[i,1]/data[i-1,1])
>>}
>>
>>This works, but of course you have negative values of the ratio, so
>>the log cannot be computed.
>>See also ?apply.
>>
>>    
>>
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
>
>
>  
>



More information about the R-help mailing list