原文鏈接:http://tecdat.cn/?p=21757
時間序列模型根據研究對象是否隨機分為確定性模型和隨機性模型兩大類。
隨機時間序列模型即是指僅用它的過去值及隨機擾動項所建立起來的模型,建立具體的模型,需解決如下三個問題模型的具體形式、時序變量的滯后期以及隨機擾動項的結構。
μ是yt的均值;ψ是系數,決定了時間序列的線性動態結構,也被稱為權重,其中ψ0=1;{εt}為高斯白噪聲序列,它表示時間序列{yt}在t時刻出現了新的信息,所以εt稱為時刻t的innovation(新信息)或shock(擾動)。
單位根測試是平穩性檢驗的特殊方法。單位根檢驗是對時間序列建立ARMA模型、ARIMA模型、變量間的協整分析、因果關系檢驗等的基礎。
對於單位根測試,為了說明這些測試的實現,考慮以下系列
-
-
> plot(X,type="l")
- Dickey Fuller(標准)
這里,對於Dickey-Fuller測試的簡單版本,我們假設
我們想測試是否(或不是)。我們可以將以前的表示寫為
所以我們只需測試線性回歸中的回歸系數是否為空。這可以通過學生t檢驗來完成。如果我們考慮前面的模型沒有線性漂移,我們必須考慮下面的回歸
-
-
Call:
-
lm(formula = z.diff ~ 0 + z.lag.1)
-
-
Residuals:
-
Min 1Q Median 3Q Max
-
-2.84466 -0.55723 -0.00494 0.63816 2.54352
-
-
Coefficients:
-
Estimate Std. Error t value Pr(>|t|)
-
z.lag.1 -0.005609 0.007319 -0.766 0.444
-
-
Residual standard error: 0.963 on 238 degrees of freedom
-
Multiple R-squared: 0.002461, Adjusted R-squared: -0.00173
-
F-statistic: 0.5873 on 1 and 238 DF, p-value: 0.4442
我們的測試程序將基於學生t檢驗的值,
-
> summary(lm(z.diff~0+z.lag.1 ))$coefficients[1,3]
-
[1] -0.7663308
這正是計算使用的值
-
ur.df(X,type="none",lags=0)
-
-
-
###############################################################
-
# Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
-
###############################################################
-
-
The value of the test statistic is: -0.7663
可以使用臨界值(99%、95%、90%)來解釋該值
-
> qnorm(c(.01,.05,.1)/2)
-
[1] -2.575829 -1.959964 -1.644854
如果統計量超過這些值,那么序列就不是平穩的,因為我們不能拒絕這樣的假設。所以我們可以得出結論,有一個單位根。實際上,這些臨界值是通過
-
-
###############################################
-
# Augmented Dickey-Fuller Test Unit Root Test #
-
###############################################
-
-
Test regression none
-
-
Call:
-
lm(formula = z.diff ~ z.lag.1 - 1)
-
-
Residuals:
-
Min 1Q Median 3Q Max
-
-2.84466 -0.55723 -0.00494 0.63816 2.54352
-
-
Coefficients:
-
Estimate Std. Error t value Pr(>|t|)
-
z.lag.1 -0.005609 0.007319 -0.766 0.444
-
-
Residual standard error: 0.963 on 238 degrees of freedom
-
Multiple R-squared: 0.002461, Adjusted R-squared: -0.00173
-
F-statistic: 0.5873 on 1 and 238 DF, p-value: 0.4442
-
-
Value of test-statistic is: -0.7663
-
-
Critical values for test statistics:
-
1pct 5pct 10pct
-
tau1 -2.58 -1.95 -1.62
R有幾個包可以用於單位根測試。
-
-
Augmented Dickey-Fuller Test
-
-
data: X
-
Dickey-Fuller = -2.0433, Lag order = 0, p-value = 0.5576
-
alternative hypothesis: stationary
這里還有一個檢驗零假設是存在單位根。但是p值是完全不同的。
-
p.value
-
[1] 0.4423705
-
testreg$coefficients[4]
-
[1] 0.4442389
- 增廣Dickey-Fuller檢驗
回歸中可能有一些滯后現象。例如,我們可以考慮
同樣,我們需要檢查一個系數是否為零。這可以用學生t檢驗來做。
-
-
> summary(lm(z.diff~0+z.lag.1+z.diff.lag ))
-
-
Call:
-
lm(formula = z.diff ~ 0 + z.lag.1 + z.diff.lag)
-
-
Residuals:
-
Min 1Q Median 3Q Max
-
-2.87492 -0.53977 -0.00688 0.64481 2.47556
-
-
Coefficients:
-
Estimate Std. Error t value Pr(>|t|)
-
z.lag.1 -0.005394 0.007361 -0.733 0.464
-
z.diff.lag -0.028972 0.065113 -0.445 0.657
-
-
Residual standard error: 0.9666 on 236 degrees of freedom
-
Multiple R-squared: 0.003292, Adjusted R-squared: -0.005155
-
F-statistic: 0.3898 on 2 and 236 DF, p-value: 0.6777
-
-
coefficients[1,3]
-
[1] -0.7328138
該值是使用
-
> df=ur.df(X,type="none",lags=1)
-
-
###############################################
-
# Augmented Dickey-Fuller Test Unit Root Test #
-
###############################################
-
-
Test regression none
-
-
Call:
-
lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)
-
-
Residuals:
-
Min 1Q Median 3Q Max
-
-2.87492 -0.53977 -0.00688 0.64481 2.47556
-
-
Coefficients:
-
Estimate Std. Error t value Pr(>|t|)
-
z.lag.1 -0.005394 0.007361 -0.733 0.464
-
z.diff.lag -0.028972 0.065113 -0.445 0.657
-
-
Residual standard error: 0.9666 on 236 degrees of freedom
-
Multiple R-squared: 0.003292, Adjusted R-squared: -0.005155
-
F-statistic: 0.3898 on 2 and 236 DF, p-value: 0.6777
-
-
Value of test-statistic is: -0.7328
-
-
Critical values for test statistics:
-
1pct 5pct 10pct
-
tau1 -2.58 -1.95 -1.62
同樣,也可以使用其他包:
-
-
-
Augmented Dickey-Fuller Test
-
-
data: X
-
Dickey-Fuller = -1.9828, Lag order = 1, p-value = 0.5831
-
alternative hypothesis: stationary
結論是一樣的(我們應該拒絕序列是平穩的假設)。
- 帶趨勢和漂移的增廣Dickey-Fuller檢驗
到目前為止,我們的模型中還沒有包括漂移。但很簡單(這將被稱為前一過程的擴充版本):我們只需要在回歸中包含一個常數,
-
> summary(lm)
-
-
-
Residuals:
-
Min 1Q Median 3Q Max
-
-2.91930 -0.56731 -0.00548 0.62932 2.45178
-
-
Coefficients:
-
Estimate Std. Error t value Pr(>|t|)
-
(Intercept) 0.29175 0.13153 2.218 0.0275 *
-
z.lag.1 -0.03559 0.01545 -2.304 0.0221 *
-
z.diff.lag -0.01976 0.06471 -0.305 0.7603
-
---
-
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
-
-
Residual standard error: 0.9586 on 235 degrees of freedom
-
Multiple R-squared: 0.02313, Adjusted R-squared: 0.01482
-
F-statistic: 2.782 on 2 and 235 DF, p-value: 0.06393
考慮到方差輸出的一些分析,這里獲得了感興趣的統計數據,其中該模型與沒有集成部分的模型進行了比較,以及漂移,
-
> summary(lmcoefficients[2,3]
-
[1] -2.303948
-
> anova(lm$F[2]
-
[1] 2.732912
這兩個值也是通過
-
ur.df(X,type="drift",lags=1)
-
-
###############################################
-
# Augmented Dickey-Fuller Test Unit Root Test #
-
###############################################
-
-
Test regression drift
-
-
Residuals:
-
Min 1Q Median 3Q Max
-
-2.91930 -0.56731 -0.00548 0.62932 2.45178
-
-
Coefficients:
-
Estimate Std. Error t value Pr(>|t|)
-
(Intercept) 0.29175 0.13153 2.218 0.0275 *
-
z.lag.1 -0.03559 0.01545 -2.304 0.0221 *
-
z.diff.lag -0.01976 0.06471 -0.305 0.7603
-
---
-
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
-
-
Residual standard error: 0.9586 on 235 degrees of freedom
-
Multiple R-squared: 0.02313, Adjusted R-squared: 0.01482
-
F-statistic: 2.782 on 2 and 235 DF, p-value: 0.06393
-
-
Value of test-statistic is: -2.3039 2.7329
-
-
Critical values for test statistics:
-
1pct 5pct 10pct
-
tau2 -3.46 -2.88 -2.57
-
phi1 6.52 4.63 3.81
我們還可以包括一個線性趨勢,
-
> temps=(lags+1):n
-
lm(z.diff~1+temps+z.lag.1+z.diff.lag )
-
-
Residuals:
-
Min 1Q Median 3Q Max
-
-2.87727 -0.58802 -0.00175 0.60359 2.47789
-
-
Coefficients:
-
Estimate Std. Error t value Pr(>|t|)
-
(Intercept) 0.3227245 0.1502083 2.149 0.0327 *
-
temps -0.0004194 0.0009767 -0.429 0.6680
-
z.lag.1 -0.0329780 0.0166319 -1.983 0.0486 *
-
z.diff.lag -0.0230547 0.0652767 -0.353 0.7243
-
---
-
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
-
-
Residual standard error: 0.9603 on 234 degrees of freedom
-
Multiple R-squared: 0.0239, Adjusted R-squared: 0.01139
-
F-statistic: 1.91 on 3 and 234 DF, p-value: 0.1287
-
-
> summary(lmcoefficients[3,3]
-
[1] -1.98282
-
> anova(lm$F[2]
-
[1] 2.737086
而R函數返回
-
ur.df(X,type="trend",lags=1)
-
-
-
###############################################
-
# Augmented Dickey-Fuller Test Unit Root Test #
-
###############################################
-
-
Test regression trend
-
-
-
-
Residuals:
-
Min 1Q Median 3Q Max
-
-2.87727 -0.58802 -0.00175 0.60359 2.47789
-
-
Coefficients:
-
Estimate Std. Error t value Pr(>|t|)
-
(Intercept) 0.3227245 0.1502083 2.149 0.0327 *
-
z.lag.1 -0.0329780 0.0166319 -1.983 0.0486 *
-
tt -0.0004194 0.0009767 -0.429 0.6680
-
z.diff.lag -0.0230547 0.0652767 -0.353 0.7243
-
---
-
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
-
-
Residual standard error: 0.9603 on 234 degrees of freedom
-
Multiple R-squared: 0.0239, Adjusted R-squared: 0.01139
-
F-statistic: 1.91 on 3 and 234 DF, p-value: 0.1287
-
-
Value of test-statistic is: -1.9828 1.8771 2.7371
-
-
Critical values for test statistics:
-
1pct 5pct 10pct
-
tau3 -3.99 -3.43 -3.13
-
phi2 6.22 4.75 4.07
-
phi3 8.43 6.49 5.47
- KPSS 檢驗
在這里,在KPSS過程中,可以考慮兩種模型:漂移模型或線性趨勢模型。在這里,零假設是序列是平穩的。
代碼是
-
ur.kpss(X,type="mu")
-
-
#######################
-
# KPSS Unit Root Test #
-
#######################
-
-
Test is of type: mu with 4 lags.
-
-
Value of test-statistic is: 0.972
-
-
Critical value for a significance level of:
-
10pct 5pct 2.5pct 1pct
-
critical values 0.347 0.463 0.574 0.73
在這種情況下,有一種趨勢
-
ur.kpss(X,type="tau")
-
-
#######################
-
# KPSS Unit Root Test #
-
#######################
-
-
Test is of type: tau with 4 lags.
-
-
Value of test-statistic is: 0.5057
-
-
Critical value for a significance level of:
-
10pct 5pct 2.5pct 1pct
-
critical values 0.119 0.146 0.176 0.216
再一次,可以使用另一個包來獲得相同的檢驗(但同樣,不同的輸出)
-
-
KPSS Test for Level Stationarity
-
-
data: X
-
KPSS Level = 1.1997, Truncation lag parameter = 3, p-value = 0.01
-
-
> kpss.test(X,"Trend")
-
-
KPSS Test for Trend Stationarity
-
-
data: X
-
KPSS Trend = 0.6234, Truncation lag parameter = 3, p-value = 0.01
至少有一致性,因為我們一直拒絕假設。
- Philipps-Perron 檢驗
Philipps-Perron檢驗基於ADF過程。代碼
-
> PP.test(X)
-
-
Phillips-Perron Unit Root Test
-
-
data: X
-
Dickey-Fuller = -2.0116, Truncation lag parameter = 4, p-value = 0.571
另一種可能的替代方案是
-
> pp.test(X)
-
-
Phillips-Perron Unit Root Test
-
-
data: X
-
Dickey-Fuller Z(alpha) = -7.7345, Truncation lag parameter = 4, p-value
-
= 0.6757
-
alternative hypothesis: stationary
- 比較
我不會花更多的時間比較不同的代碼,在R中,運行這些測試。我們再花點時間快速比較一下這三種方法。讓我們生成一些或多或少具有自相關的自回歸過程,以及一些隨機游走,讓我們看看這些檢驗是如何執行的:
-
-
> for(i in 1:(length(AR)+1)
-
+ for(s in 1:1000){
-
+ if(i!=1) X=arima.sim
-
+ M2[s,i]=(pp.testp.value)
-
+ M1[s,i]=(kpss.testp.value)
-
+ M3[s,i]=(adf.testp.value)
-
+ }
這里,我們要計算檢驗的p值超過5%的次數,
-
-
> plot(AR,P[1,],type="l",col="red",ylim=c(0,1)
-
> lines(AR,P[2,],type="l",col="blue")
-
> lines(AR,P[3,],type="l",col="green")
我們可以在這里看到Dickey-Fuller測試的表現有多不穩定,因為我們的自回歸過程中有50%(至少)被認為是非平穩的。
最受歡迎的見解
1.Matlab馬爾可夫鏈蒙特卡羅法(MCMC)估計隨機波動率(SV,Stochastic Volatility) 模型
3.WinBUGS對多元隨機波動率模型:貝葉斯估計與模型比較
4.R語言回歸中的hosmer-lemeshow擬合優度檢驗
5.matlab實現MCMC的馬爾可夫切換ARMA – GARCH模型估計
9.R語言如何在生存分析與Cox回歸中計算IDI,NRI指標請選中你要保存的內容,粘貼到此文本框