Más contenido relacionado La actualidad más candente (20) Similar a Rによるデータサイエンス:12章「時系列」 (20) Rによるデータサイエンス:12章「時系列」6. データの形式と属性
> class(lh)
[1] "ts"
> lh
Time Series:
Start = 1
End = 48
Frequency = 1
[1] 2.4 2.4 2.4 2.2 2.1 1.5 2.3 2.3 2.5 2.0 1.9 1.7 2.2 1.8 3.2
3.2 2.7 2.2(以下省略)
※女性の血液中の黄体ホルモンを10分間隔で測定したデータ・・・だそうです
6
7. データの形式と属性
> start(UKgas)
[1] 1960 1
> end(UKgas)
[1] 1986 4
> frequency(UKgas)
[1] 4
> UKgas
Qtr1 Qtr2 Qtr3 Qtr4
1960 160.1 129.7 84.8 120.1
1961 160.1 124.9 84.8 116.9
1962 169.7 140.9 89.7 123.3
1963 187.3 144.1 92.9 120.1
※1960年から1986年までのイギリスのガス消費量の時系列データ・・・だそうです
7
8. データの形式と属性
1年を単位として四
> start(UKgas)
[1] 1960 1
> end(UKgas)
[1] 1986 4
半期ごとにデータ
> frequency(UKgas)
[1] 4
> UKgas
Qtr1 Qtr2 Qtr3 Qtr4
があるので4
1960 160.1 129.7 84.8 120.1
1961 160.1 124.9 84.8 116.9
1962 169.7 140.9 89.7 123.3
1963 187.3 144.1 92.9 120.1
※1960年から1986年までのイギリスのガス消費量の時系列データ・・・だそうです
8
9. Window関数で部分抽出
> window(UKgas, c(1975, 2), c(1979, 3))
Qtr1 Qtr2 Qtr3 Qtr4
1975 321.8 177.7 409.8
1976 593.9 329.8 176.1 483.5
1977 584.3 395.4 187.3 485.1
1978 669.2 421.0 216.1 509.1
1979 827.7 467.5 209.7
9
11. データの作成方法
> x <- ts(1:24, start = c(2010, 1), frequency = 12)
> class(x)
[1] "ts"
>x
Jan Feb Mar Apr May Jun Jul Aug Sep Oct
Nov Dec
2010 1 2 3 4 5 6 7 8 9 10 11 12
2011 13 14 15 16 17 18 19 20 21 22 23 24
11
12. データの作成方法
> x <- ts(1:24, start = c(2010, 1), frequency = 4)
>x
Qtr1 Qtr2 Qtr3 Qtr4
2010 1 2 3 4
2011 5 6 7 8
2012 9 10 11 12
2013 13 14 15 16
2014 17 18 19 20
2015 21 22 23 24
12
14. 差分処理
plot(diff(UKgas), col = "blue", lwd = 3)
200
diff(UKgas)
0
-400
1960 1965 1970 1975 1980 1985
Time
14
18. y1 , y2 ,, yn
という時系列が
手元にあったとして・・・
18
19. (標本)平均
n
1
yt
ˆ
n t 1
19
20. (標本)自己共分散
n
ˆ
Ck
1
1 yt yt k
ˆ ˆ
n t k
20
22. 自己相関のPLOT
acf(UKgas, ci = 0.9)
Series UKgas
1.0
0.6
ACF
0.2
-0.2
0 1 2 3 4 5
Lag
22
23. 自己相関のPLOT
横軸=年 acf(UKgas, ci = 0.9)
Series UKgas
1年周期をもつの
1.0
0.6
ACF
で1、2等にピーク
0.2
-0.2
0 1 2 3 4 5
Lag
23
25. (標本)相互共分散
n
ˆ
Ck
1
1 yt ytk
ˆ ˆ
n t k
※y’・μ’はまた別な時系列とその標本平均だとする
25
27. イギリスで肺関連(気管支炎、肺気腫、喘息)
の病気による死者数推移
ts.plot(mdeaths, fdeaths, col = 2:3, lwd = 2)
legend("topleft", c("male", "female"), col = 2:3, lty = 1)
27
31. パワースペクトル密度関数
p f Ck e 2ikf
k
自己共分散Ckのフーリエ変換
31
32. ピリオドグラム
n 1
p j Ck
ˆ e 2ikf j
ˆ
k n 1
j n
f j , n 0,1,2
n 2
(標本)自己共分散Ck
のフーリエ変換
32
35. 波の単位
||
sin, cos, tan
35
36. 波の単位
||
sin, cos, tan
※tanは波ではないですよね?
36
37. 波の単位
||
sin, cos, tan
※例としてcosだけで・・・
37
47. ARモデル
p
yt ai yt i et
i 1
ai :自己回帰係数
p : 次数order
e
t
: 残差 N 0, 2
47
49. ar関数で推定
> (lh.ar <- ar(lh))
Call:
ar(x = lh)
Coefficients:
1 2 3
0.6534 -0.0636 -0.2269
Order selected 3 sigma^2 estimated as
0.1959
49
52. ar関数で推定
Box-Pierce, Ljung-Box検定
> Box.test(lh.ar$res, type="Ljung")
Box-Ljung test
data: lh.ar$res
X-squared = 0.0257, df = 1, p-value =
0.8728
残差が独立か否かチェック
52
53. 予測
> (lh.pr <- predict(lh.ar, n.ahead=10))
$pred
Time Series:
Start = 49
End = 58
Frequency = 1
[1] 2.461588 2.272267 2.199151 2.262914 2.352194 2.423066 2.449223 2.441544 2.418779 2.398456
$se
Time Series:
Start = 49
End = 58
Frequency = 1
[1] 0.4425687 0.5286675 0.5525786 0.5527502 0.5592254 0.5665903 0.5688786 0.5689385 0.5692396
[10] 0.5697534
53
54. 予測
> pred.upper <- lh.pr$pred + 2 * lh.pr$se
> pred.lower <- lh.pr$pred - 2 * lh.pr$se
> ts.plot(lh, lh.pr$pred, pred.upper, pred.lower,
+ gpars=list(col=c(1,2,3,3),lwd=3))
54
56. ARMA/ARIMAモデル
p q
yt ai yt i et b j et j
i 1 j 1
ai :自己回帰係数
p : 次数order
e
t
: 残差 N 0, 2
56
58. arima関数で推定
> p <- 1:4
> d <- 0:1
> q <- 0:4
> lh.ari.all <- apply(expand.grid(p, d, q), 1, function(x)arima(lh, order = x))
> Reduce(function(x,y)if(x$aic<y$aic){x}else{y}, lh.ari.all)
Call:
arima(x = lh, order = x)
Coefficients:
ar1 ar2 ar3 intercept
0.6448 -0.0634 -0.2198 2.3931
s.e. 0.1394 0.1668 0.1421 0.0963
sigma^2 estimated as 0.1787: log likelihood = -27.09, aic = 64.18
58
60. GARCHモデル
ai :自己回帰係数
p : 次数order
e
t
: 残差 N 0, 2
q r
ht e 2
i t i j ht j
i 1 j 1
60
69. サンプルデータ
・実質GNPの対数値(季節調整済み)
・実質M1の対数値(季節調整済み)
library(tseries)
data(USeconomic)
USe.d <- diff(USeconomic[,1:2])
ts.plot(USe.d, lty=1:2, col = 1:2)
legend("bottomleft", colnames(USe.d), lty = 1:2, col = 1:2)
69
71. ベクトル自己回帰(VAR)モデル
> (USe.ar <- ar(USe.d, order.max = 2, aic=FALSE))
Call:
ar(x = USe.d, aic = FALSE, order.max = 2)
$ar
,,1
[,1] [,2]
[1,] 0.5446 -0.1888
[2,] 0.1981 0.1295
,,2
[,1] [,2]
[1,] 0.1231 0.04513
[2,] 0.1371 0.07110
$var.pred
[,1] [,2]
[1,] 1.063e-04 1.506e-05
[2,] 1.506e-05 8.494e-05
71
72. ベクトル自己回帰(VAR)モデル
こんなモデルでました
M t 0.54M t 1 0.12M t 2 0.19Gt 1 0.05Gt 1 Et
Gt 0.20M t 1 0.14M t 2 0.13Gt 1 0.07Gt 2 Et
M : log(M 1)の1階差分値
G : log(GNP)の1階差分値
72
76. その他参考パッケージ
– ade, its:時系列の操作例、モデルさk製に関する
関数
– fracdiff:長期記憶時系列のモデル
– tsfa:時系列因子分析
– GeneTS:マイクロアレイ時系列解析パッケージ
– tsboot(boot),
dynlm(dynlm),tsd/decdif/deccensus(pastecs)
– 統計数理研究所のTIMSAC(TIMe Series Analysis
and Control program)
76