I like to find codes that can calculate different holding period return from a return column in a panel data set like this (where last two columns are the desired output)
Date Firms ret ret_2 ret_3
5/1/1988 A 0.5
6/1/1988 A 0.6 1.4
7/1/1988 A 0.4 1.24 2.36
8/1/1988 A 0.5 1.1 2.36
9/1/1988 A 0.6 1.4 2.36
11/1/1988 A 0.6 1.56 2.84
12/1/1988 A 0.4 1.24 2.584
13/01/1988 A 0.5 1.1 2.36
14/01/1988 A 0.6 1.4 2.36
15/01/1988 A 0.5 1.4 2.6
16/01/1988 A 0.6 1.4 2.84
18/01/1988 A 0.6 1.56 2.84
19/01/1988 A 0.4 1.24 2.584
20/01/1988 A 0.5 1.1 2.36
21/01/1988 A 0.6 1.4 2.36
22/01/1988 A 0.4 1.24 2.36
23/01/1988 A 0.5 1.1 2.36
25/01/1988 A 0.6 1.4 2.36
26/01/1988 A 0.5 1.4 2.6
27/01/1988 A 0.6 1.4 2.84
28/01/1988 A 0.5 1.4 2.6
29/01/1988 A 0.6 1.4 2.84
5/1/1988 B 0.5 1.4 2.6
6/1/1988 B 0.6 1.4 2.84
7/1/1988 B 0.6 1.56 2.84
8/1/1988 B 0.4 1.24 2.584
9/1/1988 B 0.5 1.1 2.36
11/1/1988 B 0.6 1.4 2.36
12/1/1988 B 0.6 1.56 2.84
13/01/1988 B 0.4 1.24 2.584
14/01/1988 B 0.5 1.1 2.36
15/01/1988 B 0.4 1.1 1.94
16/01/1988 B 0.5 1.1 2.15
18/01/1988 B 0.6 1.4 2.36
19/01/1988 B 0.4 1.24 2.36
20/01/1988 B 0.5 1.1 2.36
21/01/1988 B 0.5 1.25 2.15
22/01/1988 B 0.6 1.4 2.6
23/01/1988 B 0.6 1.56 2.84
25/01/1988 B 0.4 1.24 2.584
26/01/1988 B 0.5 1.1 2.36
27/01/1988 B 0.6 1.4 2.36
28/01/1988 B 0.4 1.24 2.36
29/01/1988 B 0.5 1.1 2.36
Here
ret_2= [(1 + r1) x (1 + r2)]-1, ret_3= [(1 + r1) x (1 + r2) x (1 + r3)]-1
r1 is the first-period return, r2 is the second-period return
I tried to use this kind of code
library(dplyr)
library(lubridate)
df %>%
group_by(date = format(dmy(Date), '%b-%y'), firms) %>%
summarise(ret2 = ))
But cannot find how I build code related to holding period of return function. could you please help me in this regard.
Using dplyr
we can use lag
to calculate results
library(dplyr)
df %>%
mutate(res2 = ((1 + ret) * (1 + lag(ret))) - 1,
res3 = ((1 + ret) * (1 + lag(ret)) * (1 + lag(ret, 2))) - 1)
# Date Firms ret ret_2 ret_3 res2 res3
#1 5/1/1988 A 0.5 NA NA NA NA
#2 6/1/1988 A 0.6 1.40 NA 1.40 NA
#3 7/1/1988 A 0.4 1.24 2.360 1.24 2.360
#4 8/1/1988 A 0.5 1.10 2.360 1.10 2.360
#5 9/1/1988 A 0.6 1.40 2.360 1.40 2.360
#6 11/1/1988 A 0.6 1.56 2.840 1.56 2.840
#7 12/1/1988 A 0.4 1.24 2.584 1.24 2.584
#8 13/01/1988 A 0.5 1.10 2.360 1.10 2.360
#....
Kept your original columns as it is for comparing the result.