【问题标题】:xts time series error with PerformanceAnalyticsPerformanceAnalytics 的 xts 时间序列错误
【发布时间】:2014-05-08 04:47:08
【问题描述】:

您好,我有一个名为 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"

可以看到所有的列都是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。

【问题讨论】:

  • 对我来说很好。一定是您的语法错误。看起来你在某个时候设置了NormalizedPnL_xts &lt;- NormalizedPnL
  • 你能解释一下吗?我不确定你的意思。这行:NormalizedPnL_xts

标签: r xts performanceanalytics


【解决方案1】:

为我工作:

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)

【讨论】:

  • hmmm 这太奇怪了,我的代码不起作用。它似乎做同样的事情。我迷路了
  • 您可能在某处出现拼写错误或语法错误。在新的 R 会话中尝试。
  • 不走运。你认为这与我的行名有关吗?当我做 rownames(NormalizedPnL) 我得到 1,2,4.... 但是当我做 rownames(NormalizedPnL_xts) 我得到 NULL。我不确定如何解释这个答案stackoverflow.com/questions/20008212/…,但它与行名有关。
  • 该答案有效,因为他们将 data.frame 传递给函数。您可以这样做,但行名 必须 是要用于 xts 索引的值,并且它们必须采用合适的格式。 xts 对象不应该有非 NULL 行名,所以这不是问题。我不知道为什么它对你不起作用。它适用于 CRAN 上最新的 PerformanceAnalytics。
猜你喜欢
  • 1970-01-01
  • 2012-03-11
  • 2012-10-20
  • 2020-10-30
  • 2012-10-24
  • 2012-11-20
  • 1970-01-01
  • 1970-01-01
  • 2012-07-12
相关资源
最近更新 更多