1

I have two data files with stock returns. I'm trying to apply the same function to both but I get an error for one of them. I wanted to find out what's causing the error, so I compared the output of str for both xts objects and the only line that differs is:

Indexed by objects of class: [POSIXct,POSIXt] TZ: # this object errors
Indexed by objects of class: [Date] TZ: GMT       # this object works

Is there a way to change the indexing of the dates in an xts object so that the output of str returns: Indexed by objects of class: [Date] TZ: GMT?

I generated the dates using: seq(as.Date("1963/07/01"), as.Date("2004/12/01"), by = "1 month",tzone="GMT").

A reproducible example:

library(xts)
library("PerformanceAnalytics")
load("https://dl.dropboxusercontent.com/u/22681355/data.Rdata")
data(edhec)
data2 <- as.xts(french1)

The function I want to call is Return.portfolio() with the argument rebalance_on="months"

Return.portfolio(edhec["1997",1:10],rebalance_on="months") #this works
Return.portfolio(data2["1976",1:10],rebalance_on="months") #this does not work
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
user1984076
  • 777
  • 1
  • 8
  • 16
  • Can you provide a [reproducible example](http://stackoverflow.com/q/5963269/271616) and the function that is throwing the error? If you can't provide the function throwing the error, can you at least tell us what the error is? Your current question assumes you know the cause of the error, which may be an incorrect assumption. – Joshua Ulrich Jul 09 '15 at 19:39
  • Please see my edit. Now there is a complete reproducible example. – user1984076 Jul 09 '15 at 20:22
  • Generally `is` functions return logical, whereas `as` functions act to coerce objects. – IRTFM Jul 09 '15 at 20:41
  • Your example is reproducible only in the sense that one can run the code. At the top of your question are two fragments of `str()` output but it's not clear what objects were being examined. You probably wrote `is.xts` when you meant to write `as.xts` but it still remains mysterious what further processing you might have done with that `seq.Date()` result. – IRTFM Jul 09 '15 at 21:13

1 Answers1

0

xts:::as.xts.data.frame by default assumes that the rownames of your data.frame should be coerced to a POSIXct object/index. If you want to use a different class, specify it via the dateFormat= argument to as.xts.

> data2 <- as.xts(french1, dateFormat="Date")
> str(data2)
An ‘xts’ object on 1963-06-30/2004-11-30 containing:
  Data: num [1:498, 1:10] -0.47 4.87 -1.68 2.66 -1.13 2.83 0.79 1.85 3.08 -0.45 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:10] "NoDur" "Durbl" "Manuf" "Enrgy" ...
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
 NULL

Though I'm not convinced this is the cause of whatever error you encounter, because I do not get an error without specifying dateFormat="Date".

> data2 <- as.xts(french1)
> Return.portfolio(data2["1976",1:10],rebalance_on="months")
           portfolio.returns
1976-01-31         0.3980000
1976-02-29         0.1017811
1976-03-31         1.3408273
1976-04-30       -11.7395151
1976-05-31         8.0197492
1976-06-30        -0.2550812
1976-07-31         2.5732207
1976-08-31         1.3784635
1976-09-30        -1.6859705
1976-10-31       -21.4958124
1976-11-30         5.6863828
1976-12-31        -7.8071966
Warning message:
In Return.portfolio(data2["1976", 1:10], rebalance_on = "months") :
  weighting vector is null, calulating an equal weighted portfolio
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • strange because without the date argument in as.xts() I get an error but after adding dateformat="Date" it runs without an error. – user1984076 Jul 10 '15 at 08:16