【问题标题】:Moving from zoo to xts object从动物园移动到 xts 对象
【发布时间】:2015-03-23 01:10:52
【问题描述】:

我尝试将各种财务数据合并到xts 对象中,以便执行多项统计分析。但是,从原始数据到 zoo 对象再到 xts 对象时,我遇到了日期问题。

例如,我读入一些对冲基金回报数据,使用 lubridate 包中的 ymd 函数更改报告日期变量,创建一个 zoo 对象,然后像检查一样创建一个 timeSeries 对象。似乎一切正常,但是当我尝试创建xts 对象时,我仍然收到错误消息,如下所示:

hfIndexes$ReportDt <- ymd(hfIndexes$ReportDt)
hfIndexesZoo <- zoo(hfIndexes,order.by="ReportDt")
hfIndexesTimeSeries <- as.timeSeries(hfIndexesZoo)
hfIndexesXTS <- as.xts(hfIndexesZoo)
Error in xts(coredata(x), order.by = order.by, frequency = frequency,  : 
  order.by requires an appropriate time-based object

我需要做些什么来确保我有正确的基于时间的对象来创建所需的xts 对象?

【问题讨论】:

  • "ReportDt" 是字符串,不是基于时间的对象。
  • 即使添加“order.by”参数,我也会收到相同的错误: lsHoldingsXTS
  • @JoshuaUlrich - 再次感谢您的耐心等待。我同意这两个变量不一样,但是当我评估“ReportDate”的结构时,它似乎是 POSIXct 类 > str(lsHoldings$ReportDate) POSIXct[1:2247],格式:“2010-10-31 " "2011-01-31" "2011-04-30" > str(lsHoldings$ReportDt) int [1:2247] 20101031 20110131 20110430 > lsHoldingsZoo lsHoldingsXTS
  • @JoshuaUlrich - 好吧,你是对的。 Zoo 对象不会将“ReportDate”视为基于时间的对象。从 ReportDate 到 ReportDate 数据的“动物园”系列:chr [1, 1:11] “AANNX”“50103”“20101031”“EMN”“0.016000”“0.0425”“4”“68.14437”“3.185563e+01”.. . - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:11] "Symbol" "CrspNo" "ReportDt" "LipperCode" ...索引:chr "ReportDate"现在我不知所措了。我以为我将“ReportDt”转换为基于时间的对象,但我想不是。 “ymd”函数如何创建了 POSIXct 类,但 zoo 不识别它?

标签: r time-series xts


【解决方案1】:

考虑这个答案:https://stackoverflow.com/a/4297342/3253015

order.by 是 xts 对象中需要的参数。当我们处理时间序列时,您可以将其视为一个,它创建了一个框架,数据被放入其中。所以你告诉as.xts,你想要的数据被order.by中给出的基于时间的对象隔开。

【讨论】:

    猜你喜欢
    • 2016-06-11
    • 2016-06-14
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 2019-07-14
    • 2011-05-18
    相关资源
    最近更新 更多