2017-07-19 989 views
1

我按照說明計算了log returns of multiple securities for multiple time period,它對我的​​數據的每日返回組。現在,當我計算截至最新日期的每月回報時,我的問題就開始了。用公式來獲得每月返還:data.frame中的錯誤(...,check.names = FALSE):參數意味着行數不同:6790,6771

logs=data.frame(
    cbind.data.frame(
     prices$Date[-1], 
     na.locf(diff(as.matrix(log(prices[,-1])), lag = 20)) 
     ) 
    ) 

我越來越:

Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 6790, 6771

可以理解的是,在連續數之差從20天的滯後來我用來獲取每月返還截至日期。我還需要計算截至目前爲止的年度回報,並且我認爲在這樣做時我也會得到同樣的錯誤。我嘗試使用merge.data.frame而不是cbind.data.frame,但只導致我的電腦崩潰。

我拿了第10行和列我的數據集:

 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 

使用弗洛裏安提供的代碼,並使用3作爲滯後:

logs=data.frame(
    cbind.data.frame(
    p$Date[-1], 
    c(rep(NA,3), na.locf(diff(as.matrix(log(p[,-1])), lag = 3))) 
) 
) 

還推出了一個錯誤:

Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 9, 66

有沒有辦法糾正錯誤/修復行號?

回答

0

Edited based on updated question.

由於20個期間的滯後不存在於初始期間,因此您可能會使用NA進行填充。問題是prices$Date[-1]有不同於na.locf(diff(as.matrix(log(prices[,-1])), lag = 20))的行數。你應該確保它們的行數是相等的。例如像這樣:

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)) 
) 
) 

輸出:

 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 

不要忘記檢查是否如預期的輸出。我只是向你展示爲什麼代碼不能正常工作以及在語句中匹配行數的方式,我不熟悉你正在執行的操作。希望這可以幫助!

+0

嗨@Florian,謝謝你幫我解決這個問題。不幸的是,我仍然使用上面公佈的公式得到同樣的錯誤,只有6790,6771到6790,1855547中的不同數字。我測試了你的第一個代碼,它的工作原理,但我不知道爲什麼第二個代碼贏了「T。 – samael

+0

有沒有辦法做一個小例子,例如採取數據集的前10行,並使用3的滯後?然後粘貼在你的問題中使用的數據,請參閱https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example這使得它更容易幫助你。 – Florian

+0

嗨@Florian,不要發佈樣品道歉。我在原始文章中包含了我的數據集的前10行和列,並使用3作爲您提供的公式的滯後期。 – samael

相關問題