我按照說明計算了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
有沒有辦法糾正錯誤/修復行號?
嗨@Florian,謝謝你幫我解決這個問題。不幸的是,我仍然使用上面公佈的公式得到同樣的錯誤,只有6790,6771到6790,1855547中的不同數字。我測試了你的第一個代碼,它的工作原理,但我不知道爲什麼第二個代碼贏了「T。 – samael
有沒有辦法做一個小例子,例如採取數據集的前10行,並使用3的滯後?然後粘貼在你的問題中使用的數據,請參閱https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example這使得它更容易幫助你。 – Florian
嗨@Florian,不要發佈樣品道歉。我在原始文章中包含了我的數據集的前10行和列,並使用3作爲您提供的公式的滯後期。 – samael