R дээр нэгжийн язгуурын тестийг хэд хэдэн янзаар хийж болно.
library(urca)
library(tseries)Хамгийн энгийн нэгж язгуурын процесс бол Random Walk юм.
set.seed(1)
e <- rnorm(240)
X <- cumsum(e)
plot(X, type="l")Dickey-Fuller test-ийн хамгийн энгийн хувилбар
\[Y_t=\varphi Y_{t-1}+\varepsilon_t\] модельд \(\varphi=1\) эсэхийг шалгана. \(y_{t-1}\)-г хасаад бичвэл \[\Delta Y_t=(\varphi-1)Y_{t-1}+\varepsilon_t.\]
Тэгэхээр дээрхи регрессийн загварт налалтын коэффициент тэг эсэхийг Стюдентийн \(t\)-тестээр шалгана
library(dplyr)##
## Attaching package: 'dplyr'
##
## The following objects are masked from 'package:stats':
##
## filter, lag
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
dat <- data.frame(x=X)
dat <- dat %>% mutate(x_lag = lag(x),
d_x = x-lag(x))
summary(lm(d_x ~ -1 + x_lag, data=dat))##
## Call:
## lm(formula = d_x ~ -1 + x_lag, data = dat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.84466 -0.55723 -0.00494 0.63816 2.54352
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## x_lag -0.005609 0.007319 -0.766 0.444
##
## Residual standard error: 0.963 on 238 degrees of freedom
## (1 observation deleted due to missingness)
## Multiple R-squared: 0.002461, Adjusted R-squared: -0.00173
## F-statistic: 0.5873 on 1 and 238 DF, p-value: 0.4442
library(urca)
dfuller <- ur.df(X, type="none", lags=0)
summary(dfuller)##
## ###############################################
## # 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
library(tseries)
adf.test(X, k=0)##
## Augmented Dickey-Fuller Test
##
## data: X
## Dickey-Fuller = -2.0433, Lag order = 0, p-value = 0.5576
## alternative hypothesis: stationary
It is possible to had some lags in the regression. For instance, we can consider
\[\Delta Y_t=\alpha+\beta t+[\varphi-1] Y_{t-1}+\gamma_1 \Delta Y_{t-1}+\varepsilon_t\]
Again, we have to check if one coefficient is null, or not. And this can be done using Student’s t test.
dat <- mutate(dat, d_xlag = lag(d_x))
lm1 <- lm(d_x ~ -1 + x_lag + d_xlag, data=dat)
summary(lm1)##
## Call:
## lm(formula = d_x ~ -1 + x_lag + d_xlag, data = dat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.87492 -0.53977 -0.00688 0.64481 2.47556
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## x_lag -0.005394 0.007361 -0.733 0.464
## d_xlag -0.028972 0.065113 -0.445 0.657
##
## Residual standard error: 0.9666 on 236 degrees of freedom
## (2 observations deleted due to missingness)
## Multiple R-squared: 0.003292, Adjusted R-squared: -0.005155
## F-statistic: 0.3898 on 2 and 236 DF, p-value: 0.6777
Доорхи аргаар олж болно
df=ur.df(X, type="none", lags=1)
summary(df)##
## ###############################################
## # 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
adf.test(X, k=1)##
## Augmented Dickey-Fuller Test
##
## data: X
## Dickey-Fuller = -1.9828, Lag order = 1, p-value = 0.5831
## alternative hypothesis: stationary
lm2 <- lm(d_x ~ x_lag + d_xlag, data=dat)
summary(lm2)##
## Call:
## lm(formula = d_x ~ x_lag + d_xlag, data = dat)
##
## 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 *
## x_lag -0.03559 0.01545 -2.304 0.0221 *
## d_xlag -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
## (2 observations deleted due to missingness)
## Multiple R-squared: 0.02313, Adjusted R-squared: 0.01482
## F-statistic: 2.782 on 2 and 235 DF, p-value: 0.06393
summary(lm2$coefficients[2])## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -0.03559 -0.03559 -0.03559 -0.03559 -0.03559 -0.03559
Those two values are the ones obtained also with
ur.df(X, type="drift", lags=1) %>% summary##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression drift
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)
##
## 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
And we can also include a linear trend,
dat %>%
mutate(trend = row_number()) %>%
lm(d_x ~ trend + x_lag + d_xlag, data=.) %>% summary##
## Call:
## lm(formula = d_x ~ trend + x_lag + d_xlag, data = .)
##
## 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.3231440 0.1506797 2.145 0.0330 *
## trend -0.0004194 0.0009767 -0.429 0.6680
## x_lag -0.0329780 0.0166319 -1.983 0.0486 *
## d_xlag -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
## (2 observations deleted due to missingness)
## Multiple R-squared: 0.0239, Adjusted R-squared: 0.01139
## F-statistic: 1.91 on 3 and 234 DF, p-value: 0.1287
df=ur.df(X, type="trend", lags=1)
summary(df)##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression trend
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + 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 *
## 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
summary(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.739
# while it will be, in the case there is a trend
summary(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
PP.test(X)##
## Phillips-Perron Unit Root Test
##
## data: X
## Dickey-Fuller = -2.0116, Truncation lag parameter = 4, p-value =
## 0.571