気温の変動をプロット(ARモデルにあてはめ)
AR(自己回帰)モデルにあてはめてみる。
使用データは下記よりダウンロード
データ出典:モニタリングサイト1000
データが大量なので、とりあえず1ファイルのみ読み込む。 備考は要らないので削除。
library("readxl") library("timeSeries") pat_name <- "大雪山/赤岳コマクサ平" tmp <- read_excel( path="files/KOZ01/mot-az_1c_air-temp_20100709-20111012.xls", sheet=2, col_names=TRUE)temp.dataframe <- tmp[,-3] temp.ts <- as.timeSeries(temp.dataframe)
データが定常性を持っているか確認
PP.test(temp.ts)
Phillips-Perron Unit Root Test data: temp.ts Dickey-Fuller = -4.2116, Truncation lag parameter = 12, p-value = 0.01
adf.test(temp.ts)
Augmented Dickey-Fuller Test data: temp.ts Dickey-Fuller = -3.025, Lag order = 22, p-value = 0.144 alternative hypothesis: stationary
- adf.testのp-valueが大きいため、「単位根が存在する(非定常である)は」という帰無仮説は棄却できなさそうである。が、とりあえずそのまま進める。
ARモデルにあてはめる。
temp.ar <- ar(temp.ts) temp.ar
Call: ar(x = temp.ts) Coefficients: 1 2 3 4 5 6 7 8 9 10 1.0269 0.0479 -0.0216 -0.0543 -0.0197 -0.0002 -0.0006 -0.0023 0.0113 -0.0176 11 12 13 14 15 16 17 18 19 20 0.0184 -0.0191 0.0031 0.0081 -0.0067 -0.0101 -0.0110 0.0043 0.0001 0.0659 21 22 23 24 25 26 27 28 29 30 -0.0224 0.0365 0.0203 0.0097 -0.0105 -0.0465 -0.0226 0.0024 -0.0084 -0.0389 31 32 33 34 35 36 37 38 39 40 0.0223 -0.0042 0.0011 0.0144 0.0153 -0.0181 0.0083 -0.0115 -0.0015 0.0297 Order selected 40 sigma^2 estimated as 0.5486
Order selectedが40となっているので、過去40時点のデータを用いてモデルの計算をしているのが分かる。
plot(temp.ts, type="l", main=pat_name) points(temp.ts-temp.ar$resid, type="l", col="red")
分かり辛いけど、サンプルデータとモデルによる推定値との当てはまりが良いことが分かった。(直感的には)
※追記
当てはまりの良さを確認する。
Jarque-Bera検定
jarque.bera.test(temp.ar$resid[41:11037])
Jarque Bera Test data: temp.ar$resid[41:11037] X-squared = 15826.67, df = 2, p-value < 2.2e-16
Jarque-Bera検定の帰無仮説は「分布は正規分布である」。
p-valueが2.2e-16と小さので、帰無仮説は棄却される。
Ljung-Box検定
Box.test(temp.ar$resid[41:11037], lag=30, type="Ljung")
Box-Ljung test data: temp.ar$resid[41:11037] X-squared = 29.4678, df = 30, p-value = 0.4931
Ljung-Box検定の帰無仮説は「残差に自己相関がない」。
lag=30を指定することで30次にわたって「自己相関がない」という帰無仮説に対する検定をしてみた。
p-valueが0.4931と大きいので、「残差に自己相関がない」といえる。