7
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rで時系列 xtsライブラリの使い方

Last updated at Posted at 2017-07-07

参考文献

xts型データの生成

csvファイルからxts型に読み込む

いったんzoo型として読み込んで、それからxts型に読み込む

library(zoo)
library(xts)
# csvを読み込む
dow <- read.csv("/tmp/dow.csv")
# いったんzoo型として読み込んで、それからxts型に読み込む
dow <- as.xts(read.zoo(dow))

xts型データを作る

data <- matrix(rnorm(15),nrow=3,ncol=5,dimnames=list(c(1,2,3),c("A","B","C","D","E")))
dates <- seq(as.Date("2017-05-01"),length=3,by="days")
xts1 <- xts(x=data, order.by=dates)
xts1
##                     A            B          C          D          E
## 2017-05-01 -2.1709488 -0.483357405 -0.8199499  1.2010761  0.3000584
## 2017-05-02  0.9469986 -0.204186288  1.2858337  0.8338472  0.5886110
## 2017-05-03 -2.1927447  0.006157829  1.0795456 -0.6527199 -0.9421177

xts型データの加工

行のデータ抽出 subset

日時で指定する

dow['::1990-01-04']
##               [,1]
## 1990-01-02 2810.15
## 1990-01-03 2809.73
## 1990-01-04 2796.08
dow['1990-01-05::1990-01-07']
##               [,1]
## 1990-01-05 2773.25

::のかわりに, /でもスライスできる

dow.subset <- dow["1990-01-03/1990-01-08"]
dow.subset
##               [,1]
## 1990-01-03 2809.73
## 1990-01-04 2796.08
## 1990-01-05 2773.25
## 1990-01-08 2794.37
dow["1990-01-11/"]
##               [,1]
## 1990-01-11 2760.67
## 1990-01-12 2689.21

列のデータ抽出

列名を直接指定

xts1[,"B"]
##                       B
## 2017-05-01 -0.483357405
## 2017-05-02 -0.204186288
## 2017-05-03  0.006157829

日付をずらす

lag を使う. lagは遅れるという意味
ただし、lagだと、あくまでデータ内で一日ずらすことになる
純粋に1日をずらしたい場合は, indexからずらす

# データ内で1個ずつずらす
dow.subset.lag <- lag(dow.subset)
cbind(dow.subset, dow.subset.lag)
##                ..1     ..2
## 1990-01-03 2809.73      NA
## 1990-01-04 2796.08 2809.73
## 1990-01-05 2773.25 2796.08
## 1990-01-08 2794.37 2773.25
# データの日付を1日だけずらす
dow.subset.1day <- dow.subset
index(dow.subset.1day) <- index(dow.subset.1day) + 1
cbind(dow.subset, dow.subset.1day)
##                ..1     ..2
## 1990-01-03 2809.73      NA
## 1990-01-04 2796.08 2809.73
## 1990-01-05 2773.25 2796.08
## 1990-01-06      NA 2773.25
## 1990-01-08 2794.37      NA
## 1990-01-09      NA 2794.37

2日ずらす

dow.subset.lag2 <- lag(dow.subset, 2)
dow.subset.lag2
##               [,1]
## 1990-01-03      NA
## 1990-01-04      NA
## 1990-01-05 2809.73
## 1990-01-08 2796.08
dow.subset
##               [,1]
## 1990-01-03 2809.73
## 1990-01-04 2796.08
## 1990-01-05 2773.25
## 1990-01-08 2794.37

前にずらしたいときは?

普通にマイナスつければいいだけだった

lag(dow.subset, -1)
##               [,1]
## 1990-01-03 2796.08
## 1990-01-04 2773.25
## 1990-01-05 2794.37
## 1990-01-08      NA

データの差分をとる diff

diff(x, lag = 1, differences = 1, arithmetic = TRUE, log = FALSE, na.pad = TRUE, ...)

  • lagは差分をとる区間幅。
  • differencesは差分を何回とるかということ。2としたら差分の差分をとる。
  • na.padはデフォルトはTRUEとなっているので注意。
  • デフォルトの時点tにおける値は時点t-1との差分である。diff.zoo, diff.tsとは異なるので注意。
dow.diff <- diff(dow)
dow.diff <- cbind(dow, dow.diff)
colnames(dow.diff) <- c("dow", "diff")
head(dow.diff,3)
##                dow   diff
## 1990-01-02 2810.15     NA
## 1990-01-03 2809.73  -0.42
## 1990-01-04 2796.08 -13.65

xts型をcsvで保存する

そのままwrite.csvだと, 日付がうまく保存されないので, write.zooを使う

write.zoo(dow, file="/tmp/demo.csv", sep=",")
7
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?