xts 操作产生错误的结果
xts operations yield wrong result
假设我有三个 xts 对象 a
、m
、s
,索引相同的时隙,我想计算 abs((a*20)-m)/s
。这适用于以下简单情况:
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
给出了正确的结果。
当我查看真实数据时,我看到了不同的行为:
> 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
列名也相同:
> setdiff(names(a),names(m))
character(0)
> setdiff(names(m),names(s))
character(0)
现在当我 n <- abs((a*20)-m)/s
我得到
> n[1,feature]
feature
2016-09-08 14:00:00 12687075516
但如果我手动计算:
> 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
只给出原始值:
> 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
谁能解释这个差异?
非常感谢
诺伯特
自我回答:错误是我认为 xts 在 a/b
考虑列名的意义上更智能,它 而不是 .
> 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
除法是通过底层矩阵完成的,不考虑列名。这就是为什么即使列名集合一致,结果也是错误的。
假设我有三个 xts 对象 a
、m
、s
,索引相同的时隙,我想计算 abs((a*20)-m)/s
。这适用于以下简单情况:
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
给出了正确的结果。
当我查看真实数据时,我看到了不同的行为:
> 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
列名也相同:
> setdiff(names(a),names(m))
character(0)
> setdiff(names(m),names(s))
character(0)
现在当我 n <- abs((a*20)-m)/s
我得到
> n[1,feature]
feature
2016-09-08 14:00:00 12687075516
但如果我手动计算:
> 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
只给出原始值:
> 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
谁能解释这个差异?
非常感谢
诺伯特
自我回答:错误是我认为 xts 在 a/b
考虑列名的意义上更智能,它 而不是 .
> 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
除法是通过底层矩阵完成的,不考虑列名。这就是为什么即使列名集合一致,结果也是错误的。