2

I have various financial data that I am trying to merge into an xts object so I can perform multiple statistical analyses. I am having difficulty, however, with dates when moving from the original data to a zoo object to an xts object.

For instance, I read in some hedge fund return data, change the report date variable using the ymd function from the lubridate package, create a zoo object, then just as a check create a timeSeries object. All seems to be OK, but I continue to get an error when I attempt to create the xts object, as shown below:

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

What do I need to do to ensure that I have the correct time-based object to create the desired xts object?

nrussell
  • 18,382
  • 4
  • 47
  • 60
fibrou
  • 313
  • 1
  • 5
  • 15
  • `"ReportDt"` is a character string, not a time-based object. – Joshua Ulrich Jan 23 '15 at 22:24
  • possible duplicate of [R: Converting a data frame to xts](http://stackoverflow.com/questions/4297231/r-converting-a-data-frame-to-xts) – kristang Jan 23 '15 at 23:43
  • even when adding the "order.by" argument, I get the same error: lsHoldingsXTS <-as.xts(lsHoldingsZoo, order.by = "ReportDt") Error in xts(coredata(x), order.by = order.by, frequency = frequency, : order.by requires an appropriate time-based object – fibrou Jan 26 '15 at 18:56
  • @JoshuaUlrich - I agree that "ReportDt" appears to not be a time-based object, but I thought the "ymd" function from package "lubridate" converted YYYYMMDD character data into PosixCT data, which is what shows to be in the data environment console. Also, I thought I needed a time-based object to formulate a timeSeries object, which I am doing (I think) when using the "as.timeSeries" function without error. I'm having difficulty trying to understand what I am doing incorrectly. – fibrou Jan 26 '15 at 19:04
  • You convert `hfIndexes$ReportDt` to `POSIXct`. Then you tell the `zoo` constructor to use the character string `"ReportDt"` as the index. Look at the `hfIndexesZoo` object you've created, and I'm sure it's not what you think it is. – Joshua Ulrich Jan 26 '15 at 19:08
  • @JoshuaUlrich - Thanks for being so patient with me, and helping me see where I am going wrong, but I still don't see how "ReportDt" is a string. When I look at its class it's a POSIXct. Even if I change the name to not confuse myself, I still get an error: lsHoldings$ReportDate <- ymd(lsHoldings$ReportDt) > lsHoldingsZoo <- zoo(lsHoldings,order.by = "ReportDate") > lsHoldingsXTS <-as.xts(lsHoldingsZoo, order.by = "ReportDate") Error in xts(coredata(x), order.by = order.by, frequency = frequency, : order.by requires an appropriate time-based object What am I missing? – fibrou Jan 26 '15 at 23:23
  • `str("ReportDate")` is not `str(hfIndexes$ReportDt)` – Joshua Ulrich Jan 26 '15 at 23:34
  • 1
    @JoshuaUlrich - Thanks again for your patience. I agree the two variables are not the same, but when I evaluate the structure of "ReportDate," it appears to be of POSIXct class > str(lsHoldings$ReportDate) POSIXct[1:2247], format: "2010-10-31" "2011-01-31" "2011-04-30" > str(lsHoldings$ReportDt) int [1:2247] 20101031 20110131 20110430 > lsHoldingsZoo <- zoo(lsHoldings,order.by = "ReportDate") > lsHoldingsXTS <-as.xts(lsHoldingsZoo, order.by = "ReportDate") Error in xts(coredata(x), order.by = order.by, frequency = frequency, : order.by requires an appropriate time-based object – fibrou Jan 27 '15 at 15:23
  • What I am having trouble with is formulating why "zoo" sees "ReportDate" as a time-based object, but "xts" does not. Sorry to be so naive and bothersome. I appreciate your attention and suggestions for improvement. – fibrou Jan 27 '15 at 15:25
  • As I said before, look at the `hfIndexesZoo` object you created. zoo most certainly *does not* see `"ReportDate"` as a time-based object. – Joshua Ulrich Jan 27 '15 at 15:43
  • 1
    @JoshuaUlrich - Well, you are correct. The Zoo object does not see "ReportDate" as a time-based object. ‘zoo’ series from ReportDate to ReportDate Data: 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" ... Index: chr "ReportDate" Now I'm at a loss. I thought I converted "ReportDt" to a time-based object, but I suppose not. How is it that the "ymd" function creates the POSIXct class, but zoo does not recognize it as such? – fibrou Jan 27 '15 at 16:09

1 Answers1

0

Consider this answer: https://stackoverflow.com/a/4297342/3253015

order.by is an argument needed in xts objects. As we are dealing with timeseries, you can consider it to be one, that creates a frame of sorts, into which the data is put. So you tell as.xts that the data you want inside is spaced out by the time-based object given in order.by.

Community
  • 1
  • 1
kristang
  • 557
  • 5
  • 17