2013-06-16 6 views
6
> x <- data.frame(a = rnorm(10), b = rnorm(10), c = rnorm(10)) 
> x 
      a   b   c 
1 -1.09651022 -0.7416278 0.209405373 
2 1.53644398 -0.9463432 0.374955227 
3 1.71132675 -0.3828052 2.024143398 
4 -1.10622882 -0.3599187 -0.808780103 
5 -0.49616562 0.7061180 0.644142118 
6 -1.75452442 0.3890812 -0.623815889 
7 0.06315648 0.5103820 -1.501873998 
8 0.64856129 -1.0973679 1.432024595 
9 -0.62828873 -0.3159317 0.183674189 
10 -0.82657934 1.6376569 -0.003601196 
> rownames(x) <- as.Date(Sys.Date() - 9:0) 
> x 
        a   b   c 
2013-06-07 -1.09651022 -0.7416278 0.209405373 
2013-06-08 1.53644398 -0.9463432 0.374955227 
2013-06-09 1.71132675 -0.3828052 2.024143398 
2013-06-10 -1.10622882 -0.3599187 -0.808780103 
2013-06-11 -0.49616562 0.7061180 0.644142118 
2013-06-12 -1.75452442 0.3890812 -0.623815889 
2013-06-13 0.06315648 0.5103820 -1.501873998 
2013-06-14 0.64856129 -1.0973679 1.432024595 
2013-06-15 -0.62828873 -0.3159317 0.183674189 
2013-06-16 -0.82657934 1.6376569 -0.003601196 
> class(x) 
[1] "data.frame" 
> boxplot(x) 

enter image description hereWarum kann ich nicht direkt ein xts boxplot?

> xx <- as.xts(x, order.by = as.Date(rownames(x))) 
> xx 
        a   b   c 
2013-06-07 -1.09651022 -0.7416278 0.209405373 
2013-06-08 1.53644398 -0.9463432 0.374955227 
2013-06-09 1.71132675 -0.3828052 2.024143398 
2013-06-10 -1.10622882 -0.3599187 -0.808780103 
2013-06-11 -0.49616562 0.7061180 0.644142118 
2013-06-12 -1.75452442 0.3890812 -0.623815889 
2013-06-13 0.06315648 0.5103820 -1.501873998 
2013-06-14 0.64856129 -1.0973679 1.432024595 
2013-06-15 -0.62828873 -0.3159317 0.183674189 
2013-06-16 -0.82657934 1.6376569 -0.003601196 
> class(xx) 
[1] "xts" "zoo" 
> boxplot(xx) 
Error in try.xts(c(2.12199579096527e-314, 2.12199579096527e-314, 0, 2.12199579096527e-314, : 
    Error in xts(coredata(x), order.by = index(x), .CLASS = "xts", ...) : order.by requires an appropriate time-based object 
> 

natürlich ...

> boxplot(as.matrix(xx), col = "red") 

enter image description here

kein großes Problem, aber ich will nicht meine xts ist haben, jedes Mal zu zwingen, oder Im Allgemeinen würde ich gerne wissen, was hier passiert, falls es andere Probleme gibt, die dies aufdecken könnte.

Antwort

5

Ein xts Objekt ist eine Matrix plus ein Index, so dass Sie das Objekt nicht einfach an boxplot() übergeben können. Wenn Sie bei str(xx) aussehen:

# An ‘xts’ object from 2013-06-07 to 2013-06-16 containing: 
# Data: num [1:10, 1:3] 0.321 -0.462 1.715 0.575 0.83 ... 
# - attr(*, "dimnames")=List of 2 
# ..$ : NULL 
# ..$ : chr [1:3] "a" "b" "c" 
# Indexed by objects of class: [Date] TZ: 
# xts Attributes: 
# NULL 

Sie können auf der Matrix durch coredata():

coredata(xx) 
#    a   b   c 
# [1,] 0.32120813 -0.07370657 -0.601288169 
# [2,] -0.46154742 -1.09475940 3.028664653 
# [3,] 1.71515544 -0.31000454 -0.009281175 
# [4,] 0.57482616 -0.06260991 1.198034802 
# [5,] 0.83015688 -2.49614565 -1.689812377 
# [6,] 0.01748081 -0.55332675 2.391426111 
# [7,] 0.69852800 -0.10337251 -0.267939285 
# [8,] 0.75139113 -0.17427970 -0.561434122 
# [9,] -0.68942598 0.18685817 -1.508917140 
# [10,] -0.76381007 -2.44387628 0.290524821 

die Sie direkt verwenden können, jedoch würde man eine Matrix verwenden:

boxplot(coredata(xx)) 

enter image description here

Wenn Sie den Index der 0 verwenden möchtenObjekt, die Sie gerade index() verwenden:

index(xx) 
# [1] "2013-06-07" "2013-06-08" "2013-06-09" "2013-06-10" "2013-06-11" "2013-06-12" "2013-06-13" "2013-06-14" "2013-06-15" 
# [10] "2013-06-16" 
+0

Gut:

library(PerformanceAnalytics) chart.Boxplot(xx) 

enter image description here

Intern wird das xts Objekt zu einem data.frame dazu gezwungen wird, dann kann boxplot genannt werden Vielen Dank. Dies erklärt das Problem gut. Ich denke, im täglichen Gebrauch verhalten sich xts-Objekte so ähnlich wie Matrizen, dass ich annahm, dass sie mit Boxplots "einfach funktionieren" würden. Solange ich verstehe, warum sie das nicht tun, bekomme ich auch einen Einblick in andere Probleme, denen ich in der Zukunft begegnen kann und die beim Debuggen helfen. –

3

Da Theres keine boxplot.xts Funktion. Schreibe es, und magisch du kannst Boxplot machen (Anxtsobject). Wenn der Autor von xts es mag, könnte es sogar in das Paket kommen. Oh, das ist vielleicht nur wahr wenn xts S3 Klassen sind ...

Kannst du hist (anxtsobject)?

+0

+1! für das "Oh, das könnte nur wahr sein, wenn xts S3-Klassen sind ..." Für das 'hist' kann man so etwas wie' chart.Histogram (anxtsobject) '... – agstudy

3

Sie PerformanceAnalytics Paket wie dies zum Beispiel verwenden können:

library(PerformanceAnalytics) 
R <- checkData(xx, method = "data.frame") 
boxplot(R)