xts: eXtensible Time Series

時系列データ操作

> library(xts)
> data("sample_matrix")

バージョン: 0.9.7


関数名 概略
.dimnames.xts Internal Documentation
.parseISO8601 Internal ISO 8601:2004(e) Time Parser CLASS Extract and Set .CLASS Attribute
[.xts Extract Subsets of xts Objects
align.time Align seconds, minutes, and hours to beginning of next period.
apply.monthly Apply Function over Calendar Periods
as.environment.xts Coerce an 'xts' Object to an Environment by Column
as.xts Convert Object To And From Class xts
as.xts.methods Convert Object To And From Class xts
axTicksByTime Compute x-Axis Tickmark Locations by Time
coredata.xts Extract/Replace Core Data of an xts Object
diff.xts Lags and Differences of xts Objects
dimnames.xts Dimnames of an xts Object
endpoints Locate Endpoints by Time
first Return First or Last n Elements of A Data Object
firstof Create a POSIXct Object
indexClass Extracting/Replacing the Class of an xts Index
indexTZ Query the TimeZone of an xts object
isOrdered Check If A Vector Is Ordered
make.index.unique Force Time Values To Be Unique
merge.xts Merge xts Objects
na.locf.xts Last Observation Carried Forward
ndays Number of Periods in Data
period.apply Apply Function Over Specified Interval
period.max Calculate Max By Period
period.min Calculate Min By Period
period.prod Calculate Product By Period
period.sum Calculate Sum By Period
periodicity Approximate Series Periodicity
plot.xts Plotting xts Objects
rbind.xts Concatenate Two or More xts Objects by Row
sample_matrix Sample Data Matrix For xts Example and Unit Testing
split.xts Divide into Groups by Time
timeBased Check if Class is Time-Based
timeBasedSeq Create a Sequence or Range of Times
to.period Convert time series data to an OHLC series
xts Create Or Test For An xts Time-Series Object
xts-package xts: extensible time-series
xtsAPI xts C API Documentation
xtsAttributes Extract and Replace xts Attributes

.parseISO8601

日付と時刻の表記に関する国際規格であるISO 8601

> .parseISO8601(x = "2015")
$first.time
[1] "2015-01-01 JST"

$last.time
[1] "2015-12-31 23:59:59 JST"

align.time

日付・時間クラスのオブジェクトを指定の時間単位で区切る

Arguments

  • x
  • n
  • ...
> x <- Sys.time() + 1:1000
> x %>% head()
[1] "2016-11-02 02:22:09 JST" "2016-11-02 02:22:10 JST"
[3] "2016-11-02 02:22:11 JST" "2016-11-02 02:22:12 JST"
[5] "2016-11-02 02:22:13 JST" "2016-11-02 02:22:14 JST"
> # 10秒ごと
> align.time(x, 10) %>% head()
[1] "2016-11-02 02:22:10 JST" "2016-11-02 02:22:20 JST"
[3] "2016-11-02 02:22:20 JST" "2016-11-02 02:22:20 JST"
[5] "2016-11-02 02:22:20 JST" "2016-11-02 02:22:20 JST"
> # 1分間
> align.time(x, 60) %>% sample(10)
 [1] "2016-11-02 02:25:00 JST" "2016-11-02 02:34:00 JST"
 [3] "2016-11-02 02:35:00 JST" "2016-11-02 02:23:00 JST"
 [5] "2016-11-02 02:36:00 JST" "2016-11-02 02:32:00 JST"
 [7] "2016-11-02 02:36:00 JST" "2016-11-02 02:26:00 JST"
 [9] "2016-11-02 02:24:00 JST" "2016-11-02 02:23:00 JST"
> align.time(x, 10 * 60) %>% sample(10)
 [1] "2016-11-02 02:30:00 JST" "2016-11-02 02:40:00 JST"
 [3] "2016-11-02 02:40:00 JST" "2016-11-02 02:40:00 JST"
 [5] "2016-11-02 02:30:00 JST" "2016-11-02 02:30:00 JST"
 [7] "2016-11-02 02:40:00 JST" "2016-11-02 02:40:00 JST"
 [9] "2016-11-02 02:40:00 JST" "2016-11-02 02:40:00 JST"

apply.monthly / apply.weekly / apply.monthly / apply.quarterly / apply.yearly

関数の適用

Arguments

  • x
  • FUN
  • ...
> xts.ts <- xts(rnorm(21), as.Date((Sys.Date() %>% as.numeric() - 20):Sys.Date() %>% as.numeric(), origin = "1970-01-01"))
> 
> apply.monthly(xts.ts, mean)
                [,1]
2016-10-31 0.2121055
2016-11-02 0.4925435
> apply.monthly(xts.ts, function(x) var(x))
                 [,1]
2016-10-31 1.06397958
2016-11-02 0.03207111

as.environment.xts

> x <- xts(1:10, Sys.Date() + 1:10)
> colnames(x) <- "X"
> y <- xts(1:10, Sys.Date() + 1:10)
> colnames(x) <- "Y"
> xy <- cbind(x,y)
> 
> e <- as.environment(xy)
> ls(xy)
[1] "Y"
> ls.str(xy)
Y : An 'xts' object on 2016-11-03/2016-11-12 containing:
  Data: int [1:10, 1] 1 2 3 4 5 6 7 8 9 10
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr "Y"
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
 NULL

as.xts / xtsible / Reclass / try.xts / reclass

xtsクラスオブジェクトへの変換を試みる

Arguments

  • x
  • match.to
  • error
  • ...
> as.xts(sample_matrix) %>% {
+   head(.) %>% print()
+   class(.)
+ }
               Open     High      Low    Close
2007-01-02 50.03978 50.11778 49.95041 50.11778
2007-01-03 50.23050 50.42188 50.23050 50.39767
2007-01-04 50.42096 50.42096 50.26414 50.33236
2007-01-05 50.37347 50.37347 50.22103 50.33459
2007-01-06 50.24433 50.24433 50.11121 50.18112
2007-01-07 50.13211 50.21561 49.99185 49.99185
[1] "xts" "zoo"

diff.xts

> x <- xts(1:10, Sys.Date() + 1:10)
> lag(x)
           [,1]
2016-11-03   NA
2016-11-04    1
2016-11-05    2
2016-11-06    3
2016-11-07    4
2016-11-08    5
2016-11-09    6
2016-11-10    7
2016-11-11    8
2016-11-12    9
> diff.xts(x)
           [,1]
2016-11-03   NA
2016-11-04    1
2016-11-05    1
2016-11-06    1
2016-11-07    1
2016-11-08    1
2016-11-09    1
2016-11-10    1
2016-11-11    1
2016-11-12    1
> diff.xts(x, lag = 1)
           [,1]
2016-11-03   NA
2016-11-04    1
2016-11-05    1
2016-11-06    1
2016-11-07    1
2016-11-08    1
2016-11-09    1
2016-11-10    1
2016-11-11    1
2016-11-12    1
> diff.xts(x, diff = 2)
           [,1]
2016-11-03   NA
2016-11-04   NA
2016-11-05    0
2016-11-06    0
2016-11-07    0
2016-11-08    0
2016-11-09    0
2016-11-10    0
2016-11-11    0
2016-11-12    0
> diff(diff(x))
           [,1]
2016-11-03   NA
2016-11-04   NA
2016-11-05    0
2016-11-06    0
2016-11-07    0
2016-11-08    0
2016-11-09    0
2016-11-10    0
2016-11-11    0
2016-11-12    0

endpoints

Arguments

  • x
  • on
  • k
> endpoints(sample_matrix, on = "weeks")
 [1]   0   7  14  21  28  35  42  49  56  63  70  77  84  91  98 105 112
[18] 119 126 133 140 147 154 161 168 175 180

first / last

要素の先頭・末尾から値を抽出する

Arguments

  • x
  • n
  • keep
  • ...
> x <- xts(1:100, Sys.Date() + 1:100)
> 
> first(x, n = 10)
           [,1]
2016-11-03    1
2016-11-04    2
2016-11-05    3
2016-11-06    4
2016-11-07    5
2016-11-08    6
2016-11-09    7
2016-11-10    8
2016-11-11    9
2016-11-12   10
> last(x, n = "4 days")
           [,1]
2017-02-07   97
2017-02-08   98
2017-02-09   99
2017-02-10  100

firstof / lastof

POSIXctクラスオブジェクトを生成する ref) ISOdatetime()

Arguments

  • year, month, day
  • hour, min, sec
  • tz
> firstof(2016)
[1] "2016-01-01 JST"
> lastof(year = 2010, month = 12)
[1] "2010-12-31 23:59:59 JST"
> firstof(2016, 4, 20, 22, 00, 1, tz = "Asia/Tokyo")
[1] "2016-04-20 22:00:01 JST"

indexTZ / tzone

タイムゾーンの特定と代入

Arguments

  • x
  • value
  • ...
> x <- xts(1:10, Sys.Date() + 1:10)
> indexTZ(x)
[1] "UTC"
> tzone(x)
[1] "UTC"
> str(x)
An 'xts' object on 2016-11-03/2016-11-12 containing:
  Data: int [1:10, 1] 1 2 3 4 5 6 7 8 9 10
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
 NULL

na.fill

Arguments

  • object
  • fill
  • ix
  • ...
> z <- zoo(c(NA, 2, NA, 1, 4, 5, 2, NA))
> na.fill(z, fill = "extend")
  1   2   3   4   5   6   7   8 
2.0 2.0 1.5 1.0 4.0 5.0 2.0 2.0

period.sum

> period.sum(c(1, 1, 4, 2, 2, 6, 7, 8, -1, 20), c(0, 3, 5, 8, 10))
 3  5  8 10 
 6  4 21 19

periodicity

周期性の予測

> zoo.ts <- zoo::zoo(rnorm(231), as.Date(13514:13744, origin = "1970-01-01"))
> periodicity(zoo.ts)
Daily periodicity from 2007-01-01 to 2007-08-19

rbind.xts

複数のtsクラスオブジェクトを結合する

Arguments

  • ...
  • deparse.level
> (x <- xts(rep(1,5), Sys.Date() + c(1, 2, 2, 2, 3)))
           [,1]
2016-11-03    1
2016-11-04    1
2016-11-04    1
2016-11-04    1
2016-11-05    1
> (y <- xts(rep(2,3), Sys.Date() + c(1, 2, 3)))
           [,1]
2016-11-03    2
2016-11-04    2
2016-11-05    2
> rbind(x, y)
           [,1]
2016-11-03    1
2016-11-03    2
2016-11-04    1
2016-11-04    1
2016-11-04    1
2016-11-04    2
2016-11-05    1
2016-11-05    2
> merge(y, x)
            y x
2016-11-03  2 1
2016-11-04  2 1
2016-11-04 NA 1
2016-11-04 NA 1
2016-11-05  2 1

sample_matrix

xtsクラスオブジェクトデモのための180行4列からなる行列データ。

> data("sample_matrix")
> 
> sample_matrix %>% {
+   head(.) %>% print()
+   str(.) %>% print()
+   class(.)
+ }
               Open     High      Low    Close
2007-01-02 50.03978 50.11778 49.95041 50.11778
2007-01-03 50.23050 50.42188 50.23050 50.39767
2007-01-04 50.42096 50.42096 50.26414 50.33236
2007-01-05 50.37347 50.37347 50.22103 50.33459
2007-01-06 50.24433 50.24433 50.11121 50.18112
2007-01-07 50.13211 50.21561 49.99185 49.99185
 num [1:180, 1:4] 50 50.2 50.4 50.4 50.2 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:180] "2007-01-02" "2007-01-03" "2007-01-04" "2007-01-05" ...
  ..$ : chr [1:4] "Open" "High" "Low" "Close"
NULL
[1] "matrix"

split.xts

時系列データの分割

Arguments

  • x
  • f
  • drop
  • k
  • ...
> as.xts(sample_matrix) %>% {
+   split(., f = "weeks", k = 4) %>% .[[1]] %>% print()
+   split(., f = "months") %>% .[[1]]
+ }
               Open     High      Low    Close
2007-01-02 50.03978 50.11778 49.95041 50.11778
2007-01-03 50.23050 50.42188 50.23050 50.39767
2007-01-04 50.42096 50.42096 50.26414 50.33236
2007-01-05 50.37347 50.37347 50.22103 50.33459
2007-01-06 50.24433 50.24433 50.11121 50.18112
2007-01-07 50.13211 50.21561 49.99185 49.99185
2007-01-08 50.03555 50.10363 49.96971 49.98806
               Open     High      Low    Close
2007-01-02 50.03978 50.11778 49.95041 50.11778
2007-01-03 50.23050 50.42188 50.23050 50.39767
2007-01-04 50.42096 50.42096 50.26414 50.33236
2007-01-05 50.37347 50.37347 50.22103 50.33459
2007-01-06 50.24433 50.24433 50.11121 50.18112
2007-01-07 50.13211 50.21561 49.99185 49.99185
2007-01-08 50.03555 50.10363 49.96971 49.98806
2007-01-09 49.99489 49.99489 49.80454 49.91333
2007-01-10 49.91228 50.13053 49.91228 49.97246
2007-01-11 49.88529 50.23910 49.88529 50.23910
2007-01-12 50.21258 50.35980 50.17176 50.28519
2007-01-13 50.32385 50.48000 50.32385 50.41286
2007-01-14 50.46359 50.62395 50.46359 50.60145
2007-01-15 50.61724 50.68583 50.47359 50.48912
2007-01-16 50.62024 50.73731 50.56627 50.67835
2007-01-17 50.74150 50.77336 50.44932 50.48644
2007-01-18 50.48051 50.60712 50.40269 50.57632
2007-01-19 50.41381 50.55627 50.41278 50.41278
2007-01-20 50.35323 50.35323 50.02142 50.02142
2007-01-21 50.16188 50.42090 50.16044 50.42090
2007-01-22 50.36008 50.43875 50.21129 50.21129
2007-01-23 50.03966 50.16961 50.03670 50.16961
2007-01-24 50.10953 50.26942 50.06387 50.23145
2007-01-25 50.20738 50.28268 50.12913 50.24334
2007-01-26 50.16008 50.16008 49.94052 50.07024
2007-01-27 50.06041 50.09777 49.97267 50.01091
2007-01-28 49.96586 50.00217 49.87468 49.88096
2007-01-29 49.85624 49.93038 49.76308 49.91875
2007-01-30 49.85477 50.02180 49.77242 50.02180
2007-01-31 50.07049 50.22578 50.07049 50.22578

timeBased

時系列データを扱うためのクラスオブジェクトであるかを検証。対応するのはDate, POSIXct, chron, yearmon, yearqtrおよびtimeDate

> timeBased(Sys.time())
[1] TRUE
> timeBased(Sys.Date())
[1] TRUE
> lubridate::today() %>% timeBased()
[1] TRUE

to.period / to.minutes / to.minutes3 / to.minutes5 / to.minutes10 / to.minutes15 / to.minutes30 / to.hourly / to.daily / to.weekly / to.monthly / to.quarterly / to.yearly

日付・時間クラスオブジェクトをOHLCへ変換する

Arguments

  • x
  • period
  • indexAt
  • drop.time
  • k
  • name
  • OHLC
  • ...
> as.xts(sample_matrix) %>% {
+   to.monthly(.) %>% print()
+   to.quarterly(.)
+ }
          ..Open   ..High    ..Low  ..Close
 1 2007 50.03978 50.77336 49.76308 50.22578
 2 2007 50.22448 51.32342 50.19101 50.77091
 3 2007 50.81620 50.81620 48.23648 48.97490
 4 2007 48.94407 50.33781 48.80962 49.33974
 5 2007 49.34572 49.69097 47.51796 47.73780
 6 2007 47.74432 47.94127 47.09144 47.76719
          ..Open   ..High    ..Low  ..Close
2007 Q1 50.03978 51.32342 48.23648 48.97490
2007 Q2 48.94407 50.33781 47.09144 47.76719

xts

時系列オブジェクトの生成

Arguments

  • x
  • order.by
  • frequency
  • unique
  • tzone
> xts.ts <- xts(x = rnorm(21), 
+               order.by = as.Date((Sys.Date() %>% as.numeric() - 20):Sys.Date() %>% as.numeric(), origin = "1970-01-01"))
> xts.ts
                  [,1]
2016-10-13  0.74197765
2016-10-14  0.78598765
2016-10-15 -1.30252699
2016-10-16  0.51848262
2016-10-17 -0.15804097
2016-10-18 -2.24990441
2016-10-19  1.19197067
2016-10-20 -0.07112146
2016-10-21 -1.19009834
2016-10-22  1.47584784
2016-10-23  0.52914928
2016-10-24  0.73792712
2016-10-25 -0.09567862
2016-10-26 -1.46709248
2016-10-27  1.83147417
2016-10-28 -1.10034182
2016-10-29 -1.14820687
2016-10-30  0.81639389
2016-10-31 -0.51827925
2016-11-01 -1.29435673
2016-11-02 -0.38568130
> xts(1:5, Sys.Date() + 1:5)
           [,1]
2016-11-03    1
2016-11-04    2
2016-11-05    3
2016-11-06    4
2016-11-07    5