David Kartashyan
David Kartashyan

Reputation: 95

xts error - help explain behavior, please

My R version is 2.15.1 I am following a case study on "Predicting Stock Market Returns" by Luis Torgo. When I try to import dataset from the csv file or from the web both methods fail with

Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format

I go on using the available .RData file. However, the same error I get when I try to run the T.ind function

> T.ind <- function(quotes, tgt.margin = 0.025, n.days = 10) {
+ v <- apply(HLC(quotes), 1, mean)
+ r <- matrix(NA, ncol = n.days, nrow = NROW(quotes))
+ for (x in 1:n.days) r[, x] <- Next(Delt(v, k = x), x)
+ x <- apply(r, 1, function(x) sum(x[x > tgt.margin | x <
+ -tgt.margin]))
+ if (is.xts(quotes))
+ xts(x, time(quotes))
+ else x
+ }

So, I manually executed each command, line by line replacing parameters with respective values and traced down the error to the line:

 >xts(x,time(GSPC)) #'quote' replaced with 'GSPC'
 Error in as.POSIXlt.character(x, tz, ...) : 
   character string is not in a standard unambiguous format

Surprisingly, after many trials and errors, I found that removing 2 rows makes it work!:

 > GSPC_new<-GSPC[-c(1073,1325),]
 > x_new<-x[-c(1073,1325)]
 > xts_obj<-xts(x_new,time(GSPC_new))

Did anyone previously encountered this phenomenon? What might be the explanation for this? Thanks for your time reading and possibly answering this!

Upvotes: 2

Views: 1063

Answers (1)

Gavin Simpson
Gavin Simpson

Reputation: 174813

There error comes from as.POSIXlt.character which is the method called when character data is coerced to a POSIXlt object (i.e. to something that R can understand as a date time and not just a string).

What it means is that your datetime data vector is not recorded in a format that is recognised as a standard datetime format:

else if (all(!is.na(strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", 
    tz = tz))) || all(!is.na(strptime(xx, f <- "%Y/%m/%d %H:%M:%OS", 
    tz = tz))) || all(!is.na(strptime(xx, f <- "%Y-%m-%d %H:%M", 
    tz = tz))) || all(!is.na(strptime(xx, f <- "%Y/%m/%d %H:%M", 
    tz = tz))) || all(!is.na(strptime(xx, f <- "%Y-%m-%d", 
    tz = tz))) || all(!is.na(strptime(xx, f <- "%Y/%m/%d", 
    tz = tz)))) {

However, the only test that applies is to check for NA and IIRC there are other ways in which NAs can crop up in POSIXlt data. The most likely culprit is a datetime that does not exist in the time zone that your or your computer are operating. Think daylight saving times etc or dates that never existed for various vagaries of the system that is calendars and times.

You've identified the rows that are wrong or causing the problem. Go and see what those data look like. Are they in the correct format? If they are, do the date/times make sense for your timezone? You may need to do some checking down of this yourself for wherever your compute happens to think it is running.

Upvotes: 3

Related Questions