### Calculating log returns using R

• I am trying to calculate the log returns of a dataset in R using the usual log differencing method. However, the calculated data is simply a vector of zeroes. I can't see what I'm doing wrong.

Here is the snippet showing what I'm doing

``````> prices <- data\$cl
> head(prices)
[1] 1108.1 1095.4 1095.4 1102.2 1096.3 1096.7
>
>
> lrets <- log(lag(prices)) - log(prices)
> head(lrets)
[1] 0 0 0 0 0 0
> summary(lrets)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
0       0       0       0       0       0
``````

What am I doing wrong?

lrets <- diff(log(prices))

@VishalBelsare you should add that as an answer.

@Patrick Burns, vonjd, aajajim thanks to all of you for answering. I learned new information.

• Nemis Correct answer

9 years ago

You are simply doing \$log(S_t) - log(S_t) = 0\$ for all \$t\$. Instead, try

``````> n <- length(prices);
> lrest <- log(prices[-1]/prices[-n])
``````

Should do the trick.

Or the more traditional, fewer characters: diff(log(prices)) which also works when 'prices' is a matrix with times in the rows and assets in the columns. The other lesson is that 'lag' doesn't do what we naively expect it to do.

To be sure that `lag` works as you expect, it is much safer to store your time series as `zoo` or `xts` objects: if you use vectors (or even `ts` objects), many operations will discard or ignore the timestamps.

hadnt noticed the "diff" function yet. A handy one, indeed.

@PatrickBurns: +1 for your input. I preferred your more succinct syntax. Would have accepted that as an answer.

i have been using diff(log(prices)) for a while, but was starting to doubt as it seems almost noone use it. thanks :)

There was a slight (obvious?) error in my reply with taking the negative logreturn instead. Now corrected. Thanks to user6569 that notified me.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM
• {{ error }}