简体   繁体   中英

Difference between column of a xts zoo object

I have a matrix a, class (a): "xts" "zoo"

            EUSA.2 EUSA.3 EUSA.4 EUSA.5
2014-06-11 0.3140 0.4016 0.5230 0.6910
2014-06-12 0.3190 0.3965 0.5347 0.6950
2014-06-13 0.3180 0.3903 0.5320 0.6980
2014-06-16 0.3255 0.4129 0.5546 0.7267
2014-06-17 0.3180 0.4017 0.5280 0.6950
2014-06-18 0.3210 0.3922 0.5234 0.6921

This object has 4 columns and I am trying to calculate the difference between every two pairs in this object, expecting another object in "xts" "zoo" class, with 16 columns.I used the following:

df<-outer(colnames(a),colnames(a),paste,sep="_")
b<-outer(1:ncol(a),1:ncol(a),function(x,y) (a[,x]-a[,y]))
colnames(b)<-df

and got the error msg:

Error in NextMethod() : dims [product 121] do not match the length of object [54087] In addition: Warning message: In dim<-.zoo ( *tmp* , value = c(dX, dY)) : setting this dimension may lead to an invalid zoo object

any suggestion?

1) Assuming that differences of distinct pairs of columns is sufficient try combn :

library(xts)

a <- as.zoo(a)
a.combn <- combn(names(a), 2, function(nms) a[, nms[1]] - a[, nms[2]])
colnames(a.combn) <- combn(names(a), 2, paste, collapse = "-")
xts(a.combn, index(a))

giving (continued after output):

           EUSA.2-EUSA.3 EUSA.2-EUSA.4 EUSA.2-EUSA.5 EUSA.3-EUSA.4
2014-06-11       -0.0876       -0.2090       -0.3770       -0.1214
2014-06-12       -0.0775       -0.2157       -0.3760       -0.1382
2014-06-13       -0.0723       -0.2140       -0.3800       -0.1417
2014-06-16       -0.0874       -0.2291       -0.4012       -0.1417
2014-06-17       -0.0837       -0.2100       -0.3770       -0.1263
2014-06-18       -0.0712       -0.2024       -0.3711       -0.1312
           EUSA.3-EUSA.5 EUSA.4-EUSA.5
2014-06-11       -0.2894       -0.1680
2014-06-12       -0.2985       -0.1603
2014-06-13       -0.3077       -0.1660
2014-06-16       -0.3138       -0.1721
2014-06-17       -0.2933       -0.1670
2014-06-18       -0.2999       -0.1687

2) A subscript-free alternative is the following. It creates a 3d array of combinations which it reduces to a matrix. In this case we don't have to convert to "zoo" first:

a.combn <- apply(combn(as.data.frame(a), 2, as.matrix), 3, `%*%`, c(1, -1))
colnames(a.combn) <- combn(names(a), 2, paste, collapse = "-")
xts(a.combn, index(a))

Note: Here is a in reproducible form:

a <- structure(c(0.314, 0.319, 0.318, 0.3255, 0.318, 0.321, 0.4016, 
0.3965, 0.3903, 0.4129, 0.4017, 0.3922, 0.523, 0.5347, 0.532, 
0.5546, 0.528, 0.5234, 0.691, 0.695, 0.698, 0.7267, 0.695, 0.6921
), .Dim = c(6L, 4L), .Dimnames = list(NULL, c("EUSA.2", "EUSA.3", 
"EUSA.4", "EUSA.5")), index = structure(c(1402444800, 1402531200, 
1402617600, 1402876800, 1402963200, 1403049600), 
tzone = "UTC", tclass = "Date"), class = c("xts", "zoo"), 
.indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC")

We need to Vectorize the outer

b <- outer(1:ncol(a),1:ncol(a), FUN= Vectorize(function(i,j) 
              list(a[,i]-a[,j])))
res <- do.call(cbind, b)
colnames(res) <- df
res
#           EUSA.2_EUSA.2 EUSA.3_EUSA.2 EUSA.4_EUSA.2 EUSA.5_EUSA.2 EUSA.2_EUSA.3 EUSA.3_EUSA.3 EUSA.4_EUSA.3 EUSA.5_EUSA.3 EUSA.2_EUSA.4 EUSA.3_EUSA.4 EUSA.4_EUSA.4 EUSA.5_EUSA.4 EUSA.2_EUSA.5
#2014-06-11             0        0.0876        0.2090        0.3770       -0.0876             0        0.1214        0.2894       -0.2090       -0.1214             0        0.1680       -0.3770
#2014-06-12             0        0.0775        0.2157        0.3760       -0.0775             0        0.1382        0.2985       -0.2157       -0.1382             0        0.1603       -0.3760
#2014-06-13             0        0.0723        0.2140        0.3800       -0.0723             0        0.1417        0.3077       -0.2140       -0.1417             0        0.1660       -0.3800
#2014-06-16             0        0.0874        0.2291        0.4012       -0.0874             0        0.1417        0.3138       -0.2291       -0.1417             0        0.1721       -0.4012
#2014-06-17             0        0.0837        0.2100        0.3770       -0.0837             0        0.1263        0.2933       -0.2100       -0.1263             0        0.1670       -0.3770
#2014-06-18             0        0.0712        0.2024        0.3711       -0.0712             0        0.1312        0.2999       -0.2024       -0.1312             0        0.1687       -0.3711
#           EUSA.3_EUSA.5 EUSA.4_EUSA.5 EUSA.5_EUSA.5
#2014-06-11       -0.2894       -0.1680             0
#2014-06-12       -0.2985       -0.1603             0
#2014-06-13       -0.3077       -0.1660             0
#2014-06-16       -0.3138       -0.1721             0
#2014-06-17       -0.2933       -0.1670             0
#2014-06-18       -0.2999       -0.1687             0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM