r:`[。xts`(x,seq_len(xlen - n))出错:下标超出范围

时间:2018-03-10 21:01:18

标签: r time-series xts performanceanalytics

我将以下数据集(prices)构建为xts中数百种股票的投资组合:

head(prices)

      time      A      B     ...
     <date>   <dbl>  <dbl>   ...
1   2014-07-17  NA    NA        
2   2014-07-18  NA    NA        
3   2014-07-19  NA    NA        
4   2014-07-20  NA    NA        
5   2014-07-21  NA    NA    

tail(prices)

         time          A      B     ...
        <date>       <dbl>  <dbl>   ...
1261    2017-12-28  1.1200  1.0000      
1262    2017-12-29  1.0200  0.9850  
1263    2017-12-30  0.8345  0.8889  
1264    2017-12-31  0.9346  0.9850      
1265    2018-01-01  0.9579  1.0500  

dput(prices)太大而无法放入网站(220 k +数据)。然而,这是一个子集:

structure(c("2017-04-11", "2017-04-12", "2017-04-13", "2017-04-14", 
"2017-04-15", "2017-04-16", "2017-04-17", "2017-04-18", "2017-04-19", 
"2017-04-20", "2017-04-21", "2017-04-22", "2017-04-23", "2017-04-24", 
"2017-04-25", "2017-04-26", "2017-04-27", "2017-04-28", "2017-04-29", 
"2017-04-30", "2017-05-01", "2017-05-02", "2017-05-03", "2017-05-04", 
"2017-05-05", "2017-05-06", "2017-05-07", "2017-05-08", "2017-05-09", 
"2017-05-10", "2017-05-11", "2017-05-12", "2017-05-13", "2017-05-14", 
"2017-05-15", "2017-05-16", "2017-05-17", "2017-05-18", "2017-05-19", 
"2017-05-20", "2017-05-21", "2017-05-22", "2017-05-23", "2017-05-24", 
"2017-05-25", "2017-05-26", "2017-05-27", "2017-05-28", "2017-05-29", 
"2017-05-30", "2017-05-31", "2017-06-01", "2017-06-02", "2017-06-03", 
"2017-06-04", "2017-06-05", "2017-06-06", "2017-06-07", "2017-06-08", 
"2017-06-09", "2017-06-10", "2017-06-11", "2017-06-12", "2017-06-13", 
"2017-06-14", "2017-06-15", "2017-06-16", "2017-06-17", "2017-06-18", 
"2017-06-19", "2017-06-20", "2017-06-21", "2017-06-22", "2017-06-23", 
"2017-06-24", "2017-06-25", "2017-06-26", "2017-06-27", "2017-06-28", 
"2017-06-29", "2017-06-30", "2017-07-01", "2017-07-02", "2017-07-03", 
"2017-07-04", "2017-07-05", "2017-07-06", "2017-07-07", "2017-07-08", 
"2017-07-09", "2017-07-10", "2017-07-11", "2017-07-12", "2017-07-13", 
"2017-07-14", "2017-07-15", "2017-07-16", "2017-07-17", "2017-07-18", 
"2017-07-19", "2017-07-20", "0.29370", "0.32450", "0.33450", 
"0.32930", "0.32430", "0.31880", "0.30160", "0.28740", "0.30220", 
"0.31030", "0.28980", "0.26570", "0.28430", "0.25760", "0.29860", 
"0.27970", "0.32600", "0.31230", "0.33540", "0.35660", "0.35340", 
"0.33310", "0.35210", "0.35540", "0.40920", "0.41170", "0.42420", 
"0.43330", "0.36840", "0.39870", "0.38930", "0.42800", "0.40500", 
"0.41710", "0.38930", "0.37460", "0.40510", "0.43880", "0.52420", 
"0.51000", "0.56770", "0.62710", "0.71720", "0.85850", "0.71340", 
"0.63400", "0.53670", "0.58580", "0.62310", "0.65400", "0.66640", 
"0.71560", "0.78220", "0.88150", "0.85040", "0.89780", "1.15000", 
"1.06000", "1.07000", "1.11000", "1.13000", "1.04000", "0.94000", 
"0.98210", "0.86820", "0.88100", "0.97830", "1.03000", "0.94500", 
"2.45000", "1.82000", "1.90000", "1.85000", "1.95000", "1.86000", 
"1.58000", "1.39000", "1.63000", "1.84000", "1.76000", "1.54000", 
"1.38000", "1.42000", "1.46000", "1.34000", "1.25000", "1.21000", 
"0.96130", "1.14000", "1.09000", "0.85110", "0.78770", "1.04000", 
"1.07000", "1.08000", "0.87970", "0.77960", "0.86230", "0.92230", 
"0.81440", "0.93830", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, " 0.7000", " 1.9800", " 1.0000", " 0.7001", " 1.2800", 
" 1.1000", " 0.7001", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, "  5.80", "  2.91", "  6.38", "  4.34", "  7.40", "  4.64", 
"  7.02", "  5.18", "  5.08", "  2.91", "  2.75", "  2.68", "  1.63", 
"  4.33", "  4.14", "  4.06", "  3.89", "  4.11", "  4.10", "  2.56", 
"  2.48", "  3.13", "  3.27", "  3.32", "  3.35", "  3.21", "  3.14", 
"  2.49", "  1.80", "  3.99", "  4.21", "  3.95", "  8.65", " 13.13", 
" 12.29", " 10.86", " 10.53", " 12.24", " 12.69", " 11.41", " 11.46", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, "0.07637", "0.05784", "0.04340", "0.02816", "0.02891", 
"0.02657", "0.03563", "0.02815", "0.03372", "0.03149", "0.02978", 
"0.02317", "0.02209", "0.02618", "0.03381", "0.02967", "0.03726", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, "0.2181", "0.2365", "0.2432", "0.2612", "0.2991", "0.3009", 
"0.2262", "0.2180", "0.2128", "0.1500", "0.1931", "0.2583", "0.2043", 
"0.1630", "0.1243", "0.1104", "0.1261", "0.1462", "0.1148", "0.1301", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, "0.4166", "0.6680", "0.6997", "0.7478", "0.8654", "0.7370", 
"1.0800", "1.4300", "1.3300", "1.4000", "1.4100", "1.3100", "2.3800", 
"3.1900", "3.2600", "2.9600", "2.6900", "2.8800", "3.2400", "3.8000", 
"2.5000", "2.4800", "3.0800", "2.2900", "1.9200", "1.8700", "1.8700", 
"0.9784", "1.5900", "0.4640", "0.5884", "0.7690", "0.4995", "0.8320", 
"0.9364", "0.6674", "0.7752", "0.6666", "0.5078", "0.5848", "0.5425", 
"0.4993", "0.4951", "0.5046", "0.3284", "0.3351", "0.3713", "0.3713", 
"0.4578", "0.5360", "0.4908", "0.5761"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", class = c("xts", 
"zoo"), index = structure(c(1491868800, 1491955200, 1492041600, 
1492128000, 1492214400, 1492300800, 1492387200, 1492473600, 1492560000, 
1492646400, 1492732800, 1492819200, 1492905600, 1492992000, 1493078400, 
1493164800, 1493251200, 1493337600, 1493424000, 1493510400, 1493596800, 
1493683200, 1493769600, 1493856000, 1493942400, 1494028800, 1494115200, 
1494201600, 1494288000, 1494374400, 1494460800, 1494547200, 1494633600, 
1494720000, 1494806400, 1494892800, 1494979200, 1495065600, 1495152000, 
1495238400, 1495324800, 1495411200, 1495497600, 1495584000, 1495670400, 
1495756800, 1495843200, 1495929600, 1496016000, 1496102400, 1496188800, 
1496275200, 1496361600, 1496448000, 1496534400, 1496620800, 1496707200, 
1496793600, 1496880000, 1496966400, 1497052800, 1497139200, 1497225600, 
1497312000, 1497398400, 1497484800, 1497571200, 1497657600, 1497744000, 
1497830400, 1497916800, 1498003200, 1498089600, 1498176000, 1498262400, 
1498348800, 1498435200, 1498521600, 1498608000, 1498694400, 1498780800, 
1498867200, 1498953600, 1499040000, 1499126400, 1499212800, 1499299200, 
1499385600, 1499472000, 1499558400, 1499644800, 1499731200, 1499817600, 
1499904000, 1499990400, 1500076800, 1500163200, 1500249600, 1500336000, 
1500422400, 1500508800), tzone = "UTC", tclass = "Date"), .Dim = c(101L, 
10L), .Dimnames = list(NULL, c("time", "1ST", "8BT", "ABC", "ACE", 
"ADL", "ADST", "ADT", "ADX", "AE")))

现在,我想接受每日报酬,所以我使用PerformanceAnalytics包只是如下:

returns <- Return.calculate(prices)

但是,这是输出:

  

错误[.xts&#39;(x,seq_len(xlen - n)):下标越界

     
      
  1. 停止(&#34;下标超出范围&#34;)
  2.   
  3. &#39; [.xts&#39;(x,seq_len(xlen - n))
  4.   
  5. x [seq_len(xlen - n)]
  6.   
  7. lag(pr)
  8.   
  9. Return.calculate(价格)
  10.   

我该如何解决?

2 个答案:

答案 0 :(得分:1)

您的问题中的示例有点令人困惑,因为head()tail()的输出表明prices是一个tibble,但dput()输出表明它已经是xts对象

如何复制您报告的错误也不明显,这使得提供帮助更加困难......

也就是说,假设prices是一个tibble,你需要像这样构造xts对象:

xprices <- xts(prices[,-1], prices$time)

然后您可以使用PerformanceAnalytics::Return.calculate()

returns <- PerformanceAnalytics::Return.calculate(xprices)

答案 1 :(得分:0)

文档并不像它可能那样清楚,但我相信你想要一个时间序列对象作为你的价格参数。根据文档中的用法,您有:

install.packages("xts")
library(xts)
returns <- Return.calculate(xts(prices))

有警告说,我所做的就是阅读文档。我没有使用该软件包的经验,而且我没有尝试使用您的数据。