首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PerformanceAnalytics的xts时间序列错误

PerformanceAnalytics的xts时间序列错误
EN

Stack Overflow用户
提问于 2014-03-28 23:56:48
回答 1查看 312关注 0票数 2

您好,我有一个名为NormalizedPnL的数据帧对象。这就是它:

代码语言:javascript
复制
  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对象:

代码语言:javascript
复制
  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"

您可以看到所有的列都是zoo

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

代码语言:javascript
复制
charts.PerformanceSummary(NormalizedPnL_xts,geometric= FALSE,cex.axis=1.5)

然后我得到了错误:

代码语言:javascript
复制
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列。

EN

回答 1

Stack Overflow用户

发布于 2014-03-29 00:14:32

适用于我:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22717651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档