ykjk - 1 year ago 75

R Question

I have a df of stock prices and I would like to calculate the return, hence I need to calculate today's value - yesterdays' for every lag.

Moreover I want this to be lagged by 1 day ´since I wan to know how yesterdays return influenced my decision today.

I tried

`sapply(importData, function(x) diff(x) / lag(x, k=-1))`

But this somehow gives me completely different values than I would expect.

Here is an example value:

Stock 1 has the value 16.49 for today and 16.32 yesterday, hence I would expect a value of 0.17 in the cell before yesterdays' value, since it is lagged by 1. But the formula gives me a -0.031.

I guess I must have understood the code wrong, can someone help me out here?

Edit: here is some testdata

Time |Stock 1

day 1|18.85

day 2|18.32

day3|18.49

so my outcome should be 18.49-18.32 =

`0.17`

`day1`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Try these. Except for the last one no packages are used.

**1) plain vector**

```
x <- c(2, 2.1, 2.2)
c(NA, diff(x) / x[-length(x)])
## [1] NA 0.05000000 0.04761905
```

Double check:

```
c(NA, (2.1 - 2)/2, (2.2 - 2.1)/2.1)
## [1] NA 0.05000000 0.04761905
```

**1a) log approximation**

```
diff(log(x))
## [1] 0.04879016 0.04652002
```

`diff(log(x))`

is based on the following where the last equals is really approximately equals and is based on the approximation log(x) = x-1 if x is close to 1. For example, log(1.01) = 0.009950331 which is close to 1.01 - 1 = .01.

```
diff(log(x))
= c(log(x[2]) - log(x[1]), log(x[3]) - log(x[2]))
= c(log(x[2]/x[1]), log(x[3]/x[2]))
= c(x[2]/x[1] - 1, x[3]/x[2] - 1) (approximately)
```

**2) ts time series**

```
tt <- ts(c(2, 2.1, 2.2)
diff(tt) / lag(tt, -1)
## Time Series:
## Start = 2
## End = 3
## Frequency = 1
## [1] 0.05000000 0.04761905
```

**3) zoo time series**

```
library(zoo)
z <- zoo(c(2, 2.1, 2.2))
diff(z, arithmetic = FALSE) - 1
## 2 3
## 0.05000000 0.04761905
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**