0

Assume I have three xts objects a, m, s, indexed with the same time slots, I want to compute abs((a*20)-m)/s. This works in the following simple case:

bla <- data.frame(c("2016-09-03 13:00", "2016-09-03 13:10", "2016-09-03 13:20"),c(1,2,3), c(4,5,6), c(7,8,9))
names(bla) <- c('ts','lin','qua','cub')
a <- as.xts(x = bla[,c('lin','qua','cub')], order.by=as.POSIXct(bla$ts)
... similar for m and s...
abs((a*20)-m)/s

gives the correct results.

When I go to my real data, I see different behaviour:

> class(a)
[1] "xts" "zoo"
> class(m)
[1] "xts" "zoo"
> class(s)
[1] "xts" "zoo"
> dim(a)
[1]    1 4650
> dim(m)
[1]    1 4650
> dim(s)
[1]    1 4650

Also the column names are the same:

> setdiff(names(a),names(m))
character(0)
> setdiff(names(m),names(s))
character(0)

Now when I do n <- abs((a*20)-m)/s I get

> n[1,feature]
                     feature
2016-09-08 14:00:00  12687075516

but if I do the computation by hand:

> aa <- coredata((a*20)[1,feature])[1,1]
> mm <- coredata(m[1,feature])[1,1]
> ss <- coredata(s[1,feature])[1,1]
> abs(aa-mm)/ss
     feature 
0.0005893713 

Just to give the original values:

> a[1,feature]
                        feature
2016-09-08 14:00:00 27955015680
> m[1,feature]
                         feature
2016-09-08 14:00:00 559150430034
> s[1,feature]
                        feature
2016-09-08 14:00:00 85033719103

Can anyone explain this discrepancy?

Thanks a lot

Norbert

Norbert Preining
  • 237
  • 1
  • 11
  • It's very hard to explain the discrepancy accurately without a [reproducible example](http://stackoverflow.com/q/5963269/271616). The most likely cause is that the index values for the 3 objects do not align like you think they do. But there's no way to know for sure without a reproducible example. The output of `str` for each object might be informative. – Joshua Ulrich Mar 03 '17 at 02:24

1 Answers1

0

Self answering: the error was that I believed that xts is more intelligent in the sense that a/b considers column names, which it does not.

> a
                    lin qua cub
2016-09-03 13:00:00   1   4   7
2016-09-03 13:10:00   2   5   8
2016-09-03 13:20:00   3   6   9
> b
                    qua lin cub
2016-09-03 13:00:00   2   3   4
2016-09-03 13:10:00   2   3   4
2016-09-03 13:20:00   2   3   4
> a/b
                    lin      qua  cub
2016-09-03 13:00:00 0.5 1.333333 1.75
2016-09-03 13:10:00 1.0 1.666667 2.00
2016-09-03 13:20:00 1.5 2.000000 2.25

Division is done via the underlying matrix without taking care of column names. That is the reason while even if the set of column names coincide, the results are wrong.

Norbert Preining
  • 237
  • 1
  • 11
  • What classes in R behave the way you expected xts to? I'm not aware of any that align by row and column before performing arithmetic. – Joshua Ulrich Mar 06 '17 at 11:33