2

嗨,我有一个名为 NormalizedPnL 的数据框对象。这里是:

  print(head(NormalizedPnL))
  print(class(NormalizedPnL))
  print(class(NormalizedPnL[,1]))
  print(class(NormalizedPnL[,2]))
  print(class(NormalizedPnL[,3]))

  businessdate    start A            Strat B           Strat C
1   2014-01-01  0.000000000        0.00000000       0.000000000
2   2014-01-02  0.016764200        0.04218263       0.011912007
3   2014-01-03  0.001179697       -0.02683310      -0.000897083
4   2014-01-06 -0.033131903        0.01902207       0.021104512
5   2014-01-07 -0.033215587       -0.06347915      -0.018900792
6   2014-01-08  0.045181350       -0.00732205      -0.016600410
[1] "data.frame"
[1] "Date"   # you can see here the this column is a date
[1] "numeric"
[1] "numeric"

现在我把它变成一个 xts 对象:

  NormalizedPnL_xts<- xts(NormalizedPnL[,-1], order.by= as.Date(NormalizedPnL[,1]))
  print(head(NormalizedPnL_xts))
  print(class(head(NormalizedPnL_xts)))
  print(class(NormalizedPnL_xts[,1]))
  print(class(NormalizedPnL_xts[,2]))
  print(class(NormalizedPnL_xts[,3]))

               Strat A            Start B         Strat C 
2014-01-01  0.000000000        0.00000000       0.000000000
2014-01-02  0.016764200        0.04218263       0.011912007
2014-01-03  0.001179697       -0.02683310      -0.000897083
2014-01-06 -0.033131903        0.01902207       0.021104512
2014-01-07 -0.033215587       -0.06347915      -0.018900792
2014-01-08  0.045181350       -0.00732205      -0.016600410
[1] "xts" "zoo"
[1] "xts" "zoo"
[1] "xts" "zoo"
[1] "xts" "zoo"

你可以看到所有的列都是动物园

现在我尝试使用性能分析:

charts.PerformanceSummary(NormalizedPnL_xts,geometric= FALSE,cex.axis=1.5)

我得到了错误:

The data cannot be converted into a time series.  If you are trying to pass
 in        names     from a data object with one column, you should use the 
 form 'data[rows, columns,     drop = FALSE]'.  Rownames should have standard date formats, such as 

'1985-03-15'. 
Error in `[<-.data.frame`(`*tmp*`, i, 2, value = c(90600, 60400, 302000 : 
  replacement has 3 rows, data has 1
Error in `[<-.data.frame`(`*tmp*`, i, 8, value = c(-742200, -494800, -2474000 : 
  replacement has 3 rows, data has 1

谁能告诉我问题是什么?错误提到有 1 列,但我有 3。

4

1 回答 1

0

为我工作:

library(PerformanceAnalytics)

NormalizedPnL <-
structure(list(businessdate = c("2014-01-01", "2014-01-02", "2014-01-03", 
"2014-01-06", "2014-01-07", "2014-01-08"), StratA = c(0, 0.0167642, 
0.001179697, -0.033131903, -0.033215587, 0.04518135), StratB = c(0, 
0.04218263, -0.0268331, 0.01902207, -0.06347915, -0.00732205), 
    StratC = c(0, 0.011912007, -0.000897083, 0.021104512, -0.018900792, 
    -0.01660041)), .Names = c("businessdate", "StratA", "StratB", 
"StratC"), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5", "6"))

NormalizedPnL_xts <- xts(NormalizedPnL[,-1], order.by=as.Date(NormalizedPnL[,1]))
charts.PerformanceSummary(NormalizedPnL_xts,geometric=FALSE,cex.axis=1.5)
于 2014-03-28T16:14:32.250 回答