Log Returns : Stationary?
returns_log <- Delt(Posco_Close_xts, k=1, type="log") # diff(log(Posco_Close_xts))
plot(returns_log)
test_ranges(returns_log, begin_dates, end_dates, "POSCO log returns for:")
그래프를 보면 4 구간에서 평균값들이 모두 비슷하고 (0), sd도 모두 비슷하고, distribution
모양들이 모두 Gaussian 비슷한 것이 “대충” (weakly) stationary 할 것 같아 보인다.
Formal Stationarity Test : (Widely used) Stationarity Test in R
- Augmented Dickey–Fuller (ADF) test
- Elliott–Rothenberg–Stock test
- KPSS unit root test
- Phillips–Perron test
* Issue: Trend Stationary, Seasonal Stationary 라는 개념이 말이 되나?
ADF Test using tseries adf.test()
The Augmented Dickey–Fuller (ADF) test: test for null hypothesis of non-stationarity
- the more negative Dickey-Fuller statistics is, the more likely to reject null (non-stationarity)
- small p-values suggest the data is stationary
- https://en.wikipedia.org/wiki/Augmented_Dickey%E2%80%93Fuller_test
# 앞에서 Posco_Close_xts 그래프를 보고 nonstationary 일 것이라 예상
library(tseries)
adf.test(Posco_Close_xts, alternative = "stationary") # test for null hypothesis of non-stationarity
##
## Augmented Dickey-Fuller Test
##
## data: Posco_Close_xts
## Dickey-Fuller = -3.6656, Lag order = 13, p-value = 0.02635
## alternative hypothesis: stationary
# p값이 생각보다 작다. 따라서 stationary 하다고 생각할 수 있는 여지가 있다.
# adf.test()는 trend/seasonal stationary 특성을 보이는 시계열을
# reject하려는 속성을 보여준다. 따라서, tseries package의 adf.test()는 trend/seasonal stationary 특성을
# 보이는 시계열에 대해 non-stationarity null hypothesis를 reject 하여 결과적으로 그 시계열을
# stationary 하다고 생각케 한다. adf.test()는 trend stationarity와 보통 constant stationarity를
# 구별하는 데에는 적절한 도구가 아니다
adf.test(na.remove(returns_log), alternative = "stationary")
## Warning in adf.test(na.remove(returns_log), alternative = "stationary"): p-
## value smaller than printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: na.remove(returns_log)
## Dickey-Fuller = -14.897, Lag order = 13, p-value = 0.01
## alternative hypothesis: stationary
ADF Test using adfTest() of fUnitRoots
Tests for null hypothesis of non-stationarity. More negative the “Dickey-Fuller” statistic and smaller P value,
the more confidence on rejecting the null hypothesis (of nonstationarity). And thus more confidence on stationarity.
library(fUnitRoots)
adfTest(Posco_Close_xts, lags = 0, type = "c") # null (nonstatinarity)를 reject하기 어려움
## Warning in if (class(x) == "timeSeries") x = series(x): the condition has
## length > 1 and only the first element will be used
##
## Title:
## Augmented Dickey-Fuller Test
##
## Test Results:
## PARAMETER:
## Lag Order: 0
## STATISTIC:
## Dickey-Fuller: -1.5288
## P VALUE:
## 0.4893
##
## Description:
## Thu Dec 31 16:19:17 2015 by user: Administrator
adfTest(returns_log, lags = 0, type = "c") # suggests stationarity
## Warning in if (class(x) == "timeSeries") x = series(x): the condition has
## length > 1 and only the first element will be used
## Warning in adfTest(returns_log, lags = 0, type = "c"): p-value smaller than
## printed p-value
##
## Title:
## Augmented Dickey-Fuller Test
##
## Test Results:
## PARAMETER:
## Lag Order: 0
## STATISTIC:
## Dickey-Fuller: -45.7847
## P VALUE:
## 0.01
##
## Description:
## Thu Dec 31 16:19:17 2015 by user: Administrator
# Dickey-Fuller: -45.7847; big negative, P value: 0.01 ; small -> null (nonstatinarity)를 reject.
Determine stationarity with ’urca" package
Performs the KPSS unit root test, where the Null hypothesis is stationarity
library(urca)
test = ur.kpss(as.numeric(Posco_Close_xts) ) # type="mu" for level/constant stationarity
test@teststat # 16.0658, S4 object # type="tau" for trend stationarity
## [1] 16.0658
test@cval # 0.347 0.463 0.574 0.739
## 10pct 5pct 2.5pct 1pct
## critical values 0.347 0.463 0.574 0.739
# For given 0.05 level, since 16.0658 > 0.463, the null hypothesis(level stationay) is
# rejected. So, likely non_stationary
test = ur.kpss(as.numeric(Posco_Close_xts), type="tau" )
test@teststat # 1.750902
## [1] 1.750902
test@cval
## 10pct 5pct 2.5pct 1pct
## critical values 0.119 0.146 0.176 0.216
# at 0.05 level, since 1.750902 > 0.146 the null hypothesis of trend stationarity is rejected
# So, trend nonstationary is likely
test = ur.kpss(as.numeric(returns_log) )
test@teststat # 0.3034485
## [1] 0.3034485
test@cval # 0.347 0.463 0.574 0.739
## 10pct 5pct 2.5pct 1pct
## critical values 0.347 0.463 0.574 0.739
# at 0.05 level, since 0.3034485 < 0.463, the (level stationarity) null hypothesis cannot be
# rejected (But barely). Thus could be said "level stationary" (with caution)
test = ur.kpss(as.numeric(returns_log), type="tau")
test@teststat # 0.03532884
## [1] 0.03532884
test@cval # 0.119 0.146 0.176 0.216
## 10pct 5pct 2.5pct 1pct
## critical values 0.119 0.146 0.176 0.216
# trend stationarity cannot be rejected. "Trend stationary"" likely
Determine stationarity with kpss.test in ’tseries" package
kpss.test(Posco_Close_xts, null="Level")
## Warning in kpss.test(Posco_Close_xts, null = "Level"): p-value smaller than
## printed p-value
##
## KPSS Test for Level Stationarity
##
## data: Posco_Close_xts
## KPSS Level = 12.103, Truncation lag parameter = 11, p-value = 0.01
# KPSS Level = 12.103, Truncation lag parameter = 11, p-value = 0.01
# since KPSS Level 12.103 > 0.01, the null hypothesis is rejected. Thus nonstationary
kpss.test(returns_log, null="Trend")
## Warning in kpss.test(returns_log, null = "Trend"): p-value greater than
## printed p-value
##
## KPSS Test for Trend Stationarity
##
## data: returns_log
## KPSS Trend = 0.037189, Truncation lag parameter = 11, p-value =
## 0.1
# KPSS Trend = 0.037189, Truncation lag parameter = 11, p-value = 0.1
# Since 0.037189 < 0.1, the null hypothesis is not rejected. Thus Trend stationary
kpss.test(returns_log, null="Level")
## Warning in kpss.test(returns_log, null = "Level"): p-value greater than
## printed p-value
##
## KPSS Test for Level Stationarity
##
## data: returns_log
## KPSS Level = 0.3178, Truncation lag parameter = 11, p-value = 0.1
# KPSS Level = 0.3178, Truncation lag parameter = 11, p-value = 0.1
# in this test since 0.3178 > 0.1, the process cannot be said "Level stationary"
'Learning & Reasoning > R ' 카테고리의 다른 글
Financial Time Series Import (0) | 2015.12.21 |
---|---|
R로 Lasso regression 연습 (0) | 2015.10.05 |
A simple time series clustering (0) | 2015.04.24 |
Signal and time series seen from eight miles high cloud - DFT & Simple digital filtering (0) | 2015.02.18 |
Signal and time series seen from eight miles high cloud (0) | 2015.02.15 |