Reputation: 29
I have followed the instructions to calculate the log returns of multiple securities for multiple time period and it works fine for computing daily returns of my data set. Now my problem begins when I calculate the monthly returns as of the latest date. Using the formula to get the monthly return:
logs=data.frame(
cbind.data.frame(
prices$Date[-1],
na.locf(diff(as.matrix(log(prices[,-1])), lag = 20))
)
)
I'm getting:
Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 6790, 6771
Understandably, the difference in row number is coming from the 20-day lag I used to get the monthly return as of date. I also need to compute annual returns as of date and I think I am also going to get the same error when I do so. I tried using merge.data.frame
instead of cbind.data.frame
but only led to my computer crashing.
I took the first 10 rows and columns of my dataset:
Date `2GO` AAA AB ABA ABG ABS AC ACE ACR
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 28-Jun-17 23.25 1.61 14.98 0.37 28.25 42.85 841.5 1.61 1.50
2 27-Jun-17 23.90 1.61 14.98 0.37 27.95 42.90 842.5 1.61 1.53
3 23-Jun-17 24.60 1.61 14.98 0.38 27.00 42.90 840.5 1.70 1.57
4 22-Jun-17 24.40 1.61 14.98 0.37 28.05 43.20 855.0 1.67 1.57
5 21-Jun-17 24.80 1.61 15.00 0.37 28.05 43.10 841.5 1.67 1.57
6 20-Jun-17 25.10 1.61 14.68 0.37 28.85 43.45 858.0 1.70 1.58
7 19-Jun-17 24.85 1.61 14.68 0.37 29.05 43.40 860.0 1.75 1.55
8 16-Jun-17 25.70 1.61 14.68 0.38 29.60 43.45 850.0 1.77 1.52
9 15-Jun-17 26.20 1.61 14.48 0.38 29.55 43.30 867.0 1.69 1.53
10 14-Jun-17 26.85 1.61 16.00 0.37 29.50 43.35 867.5 1.69 1.52
Using the code Florian provided and used 3 as lag:
logs=data.frame(
cbind.data.frame(
p$Date[-1],
c(rep(NA,3), na.locf(diff(as.matrix(log(p[,-1])), lag = 3)))
)
)
still puts out an error:
Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 9, 66
Is there a way to remedy the error / fix the row number?
Upvotes: 1
Views: 10812
Reputation: 25385
Edited based on updated question.
Since the lag for 20 periods does exist not in the initial periods, you might pad with NA's. The problem is that prices$Date[-1]
has a different number of rows than na.locf(diff(as.matrix(log(prices[,-1])), lag = 20))
. You should make sure that they number of rows is equal. For example like this:
p = read.table(text="Date `2GO` AAA AB ABA ABG ABS AC ACE ACR
28-Jun-17 23.25 1.61 14.98 0.37 28.25 42.85 841.5 1.61 1.50
27-Jun-17 23.90 1.61 14.98 0.37 27.95 42.90 842.5 1.61 1.53
23-Jun-17 24.60 1.61 14.98 0.38 27.00 42.90 840.5 1.70 1.57
22-Jun-17 24.40 1.61 14.98 0.37 28.05 43.20 855.0 1.67 1.57
21-Jun-17 24.80 1.61 15.00 0.37 28.05 43.10 841.5 1.67 1.57
20-Jun-17 25.10 1.61 14.68 0.37 28.85 43.45 858.0 1.70 1.58
19-Jun-17 24.85 1.61 14.68 0.37 29.05 43.40 860.0 1.75 1.55
16-Jun-17 25.70 1.61 14.68 0.38 29.60 43.45 850.0 1.77 1.52
15-Jun-17 26.20 1.61 14.48 0.38 29.55 43.30 867.0 1.69 1.53
14-Jun-17 26.85 1.61 16.00 0.37 29.50 43.35 867.5 1.69 1.52",header=T)
p=p[order(p$Date),]
logs=data.frame(
cbind.data.frame(
Date = p$Date[4:nrow(p)],
na.locf(diff(as.matrix(log(p[,-1])), lag = 3))
)
)
Output:
Date X.2GO. AAA AB ABA ABG ABS
7 19-Jun-17 -0.07740807 0 -0.086102699 0.00000000 -0.015371780 0.001152738
6 20-Jun-17 -0.04289156 0 0.013717636 -0.02666825 -0.023973751 0.003458217
5 21-Jun-17 -0.03564734 0 0.021564178 -0.02666825 -0.053785729 -0.008087855
4 22-Jun-17 -0.01827462 0 0.020229955 0.00000000 -0.035029851 -0.004618946
3 23-Jun-17 -0.02012140 0 0.020229955 0.02666825 -0.066273127 -0.012739026
2 27-Jun-17 -0.03696519 0 -0.001334223 0.00000000 -0.003571432 -0.004651171
1 28-Jun-17 -0.04827800 0 0.000000000 0.00000000 0.007104826 -0.008134850
AC ACE ACR
7 -0.008683123 0.034887259 0.019544596
6 -0.010434877 0.005899722 0.032157112
5 -0.010050336 -0.058155920 0.032365285
4 -0.005830920 -0.046792162 0.012820688
3 -0.020607147 0.000000000 -0.006349228
2 0.001187649 -0.036589447 -0.025807884
1 -0.015915455 -0.036589447 -0.045610511
Don't forget to check if the output is as expected. I am just showing you why the code is not working and a way of matching the number of rows within the statement, I am not familiar with the operation you are performing. Hope this helps!
Upvotes: 1