00引言
在畢業實用統計模型(一)——時間序列1中,介紹了一般的時間序列的建模思路,主要介紹ARMA模型以及R語言的實現。然而在實際還會遇到一些其他的模型ARIMA、SARIMA。而模型參數也會也會變的複雜,從pq到pdq到pdqPDQS。本文主要通過R語言生成符合三種模型的隨機數,進行建模預測。給大家看一下如何操作。
在寫模型之前先給大家介紹一個時間序列總結的不錯的鏈接2(見參考文獻[2])。
1、三種模型的區別
下面表格中給出常見的時間序列模型。
模型 | 適用數據 | 參數 |
---|---|---|
ar | 自迴歸模型(平穩數據) | p |
ma | 滑動平均模型(平穩數據) | q |
arma | 自迴歸滑動平均模型(平穩數據) | (p,q) |
arima | 加入了差分(帶有趨勢的不平穩數據) | (p,d,q) |
sarima | 加入了季節因素(帶有趨勢週期的不平穩數據) | (p,d,q)(P,D,Q)[s] |
2、數據模擬
此部分使用數據模擬的方式分別建立上述三種模型。下面是運行代碼所需要的包,自行安裝載入。
# 載入所需包
library(tseries)
library(zoo)
library(forecast)
2.1arma
# 構造數據
> ts1 <- arima.sim(n = 100,list(ar = c(0.8, -0.5), ma = c(0.2, 0.5)))
# 建模
> (fit <- auto.arima(ts1, ic = "aic"))
Series: ts1
ARIMA(2,0,2) with zero mean
Coefficients:
ar1 ar2 ma1 ma2
0.7477 -0.4414 0.1682 0.5319
s.e. 0.1611 0.1354 0.1471 0.1227
sigma^2 estimated as 0.9337: log likelihood=-137.19
AIC=284.37 AICc=285.01 BIC=297.4
> x.fore <- forecast(fit, h = 10)
> plot(x.fore)
看一下模型預測效果:
2.2arima
先看固定的趨勢:
> (fit <- auto.arima((1:100)^2, ic = "aic"))
Series: (1:100)^2
ARIMA(0,2,0)
sigma^2 estimated as 4: log likelihood=-206.98
AIC=415.97 AICc=416.01 BIC=418.55
> x.fore <- forecast(fit, h = 10)
> plot(x.fore)
效果:
再看一下函數filter
和數據presidents
構造的時間序列。
> ts2 <- filter(presidents, rep(1, 3))
> plot.ts(ts2)
> (fit <- auto.arima(ts2, ic = "aic"))
Series: ts2
ARIMA(3,0,2) with non-zero mean
Coefficients:
ar1 ar2 ar3 ma1 ma2 mean
0.6180 0.4213 -0.2307 1.1205 0.9496 169.2646
s.e. 0.1051 0.1156 0.1039 0.0700 0.0672 13.2637
sigma^2 estimated as 91.77: log likelihood=-400.08
AIC=814.15 AICc=815.18 BIC=833.49
> x.fore <- forecast(fit, h = 10)
> plot(x.fore)
下面看效果:
2.3sarima
這裏在加入季節因素,注意數據的構造,如果不用ts函數包裝則不能建立此模型。
先構造數據
> ts3 <- rep(1:4, time = 25) + rnorm(4*25, 0, 0.1)
> ts3 <- ts(ts3, frequency = 4)
> plot(ts3, type = "l")
下面建模:
> (fit <- auto.arima(ts3, ic = "aic", max.P = 5, max.Q = 5))
Series: ts3
ARIMA(0,0,0)(4,1,0)[4]
Coefficients:
sar1 sar2 sar3 sar4
-0.6396 -0.5746 -0.4950 -0.3314
s.e. 0.1026 0.1256 0.1219 0.1086
sigma^2 estimated as 0.01478: log likelihood=65.96
AIC=-121.91 AICc=-121.25 BIC=-109.09
> x.fore <- forecast(fit, h = 10)
> plot(x.fore)
3、總結
在使用過程中希望大家仔細識別數據,選擇合適的模型。