KPSS & Unit Root
Purpose
For all the 313 pairs see whether kpss and unit root test
> sector.tests <- samesector
> sector.tests$uroot <- 0
> sector.tests$kpss <- 0
> npairs <- dim(samesector)[1]
> pair <- 1
> for (pair in 1:npairs) {
+ a <- samesector[pair, "tickeri"]
+ b <- samesector[pair, "tickerj"]
+ y1 <- (security.db1[, a])
+ x1 <- (security.db1[, b])
+ temp1 <- (grangertest(y1 ~ x1, order = 1))[2, 4]
+ if (temp1 < 0.05) {
+ y1 <- (security.db1[, a])
+ x1 <- (security.db1[, b])
+ fit.1 <- lm(y1 ~ x1 + 0)
+ error <- residuals(fit.1)
+ time <- 1:length(error)
+ fit.2 <- lm(error ~ time)
+ if (coef(summary(fit.2))[1, 4] > 0.05 & coef(summary(fit.2))[2,
+ 4] > 0.05) {
+ type.1 <- "nc"
+ type.2 <- "mu"
+ error.transf <- error
+ }
+ if (coef(summary(fit.2))[1, 4] < 0.05 & coef(summary(fit.2))[2,
+ 4] < 0.05) {
+ type.1 <- "ct"
+ type.2 <- "tau"
+ error.transf <- resid(fit.2)
+ }
+ if (coef(summary(fit.2))[1, 4] < 0.05 & coef(summary(fit.2))[2,
+ 4] > 0.05) {
+ type.1 <- "c"
+ type.2 <- "mu"
+ error.transf <- error - mean(error)
+ }
+ if (coef(summary(fit.2))[1, 4] > 0.05 & coef(summary(fit.2))[2,
+ 4] < 0.05) {
+ type.1 <- "c"
+ type.2 <- "mu"
+ error.transf <- error
+ }
+ t1 <- unitrootTest(error.transf, lags = 1, type = "ct")@test$p.value[1]
+ kpfit <- urkpssTest(error.transf, type.2, "short")
+ if (kpfit@test$test@teststat > kpfit@test$test@cval[2]) {
+ t2 <- 1
+ }
+ else {
+ t2 <- 0
+ }
+ sector.tests$uroot[pair] <- t1
+ sector.tests$kpss[pair] <- t2
+ }
+ temp2 <- (grangertest(x1 ~ y1, order = 1))[2, 4]
+ if (temp1 > 0.05 & temp2 < 0.05) {
+ y1 <- (security.db1[, b])
+ x1 <- (security.db1[, a])
+ fit.1 <- lm(y1 ~ x1 + 0)
+ error <- residuals(fit.1)
+ time <- 1:length(error)
+ fit.2 <- lm(error ~ time)
+ if (coef(summary(fit.2))[1, 4] > 0.05 & coef(summary(fit.2))[2,
+ 4] > 0.05) {
+ type.1 <- "nc"
+ type.2 <- "mu"
+ error.transf <- error
+ }
+ if (coef(summary(fit.2))[1, 4] < 0.05 & coef(summary(fit.2))[2,
+ 4] < 0.05) {
+ type.1 <- "ct"
+ type.2 <- "tau"
+ error.transf <- resid(fit.2)
+ }
+ if (coef(summary(fit.2))[1, 4] < 0.05 & coef(summary(fit.2))[2,
+ 4] > 0.05) {
+ type.1 <- "c"
+ type.2 <- "mu"
+ error.transf <- error - mean(error)
+ }
+ if (coef(summary(fit.2))[1, 4] > 0.05 & coef(summary(fit.2))[2,
+ 4] < 0.05) {
+ type.1 <- "c"
+ type.2 <- "mu"
+ error.transf <- error
+ }
+ t1 <- unitrootTest(error.transf, lags = 1, type = type.1)@test$p.value[1]
+ kpfit <- urkpssTest(error.transf, type.2, "short")
+ if (kpfit@test$test@teststat > kpfit@test$test@cval[2]) {
+ t2 <- 1
+ }
+ else {
+ t2 <- 0
+ }
+ sector.tests$uroot[pair] <- t1
+ sector.tests$kpss[pair] <- t2
+ }
+ if (temp1 > 0.05 & temp2 > 0.05) {
+ sector.tests$uroot[pair] <- 999
+ sector.tests$kpss[pair] <- 999
+ }
+ } |
What the above script does is : . For the pairs which pass granger test, check for stationarity and unit root test simulataneously
> test <- sector.tests[sector.tests$uroot != 999, ] > dim(test) [1] 127 12 |
Out of 313, there are about 127 pairs for which there is no granger causality
> length(which(test$kpss == 1)) [1] 113 |
Out of 127 pairs, there are about , 113 pairs which pass stationarity tests
> length(which(test$uroot < 0.05)) [1] 44 |
Out of 127 pairs, there are about 44 pairs which fail unit root tests
> length(which(test$uroot < 0.05 & test$kpss == 1)) [1] 35 |
Out of 127 pairs, there are about 35 pairs ,
which fail unit root tests and pass stationarity tests.
However there is one thing which is problematic with the above approach. The final pairs are
> test <- sector.tests[sector.tests$uroot < 0.05 & sector.tests$kpss == + 1, ] > as.data.frame(paste(test$tickeri, test$tickerj, sep = "-")) if (stringsAsFactors) factor(x) else x 1 AMBUJACEM-ACC 2 DIVISLAB-CIPLA 3 GAIL-CAIRN 4 GESHIP-CONCOR 5 GSPL-BPCL 6 IBREALEST-DLF 7 ICICIBANK-AXISBANK 8 IDBI-BANKINDIA 9 IDFC-HDFC 10 JPASSOCIAT-IVRCLINFRA 11 LICHSGFIN-IDFC 12 NAGARCONST-JPASSOCIAT 13 NAGARCONST-LITL 14 NAGARCONST-HCC 15 RECLTD-HDFC 16 RECLTD-PFC 17 RELCAPITAL-INDIAINFO 18 RELCAPITAL-IFCI 19 RELIANCE-IOC 20 RNRL-MRPL 21 ROLTA-POLARIS 22 ROLTA-HCLTECH 23 SUNPHARMA-DIVISLAB 24 SUNPHARMA-LUPIN 25 TTML-MTNL 26 ULTRACEMCO-AMBUJACEM 27 UNIONBANK-PNB 28 UNITECH-IBREALEST 29 VIJAYABANK-IDBI 30 WIPRO-INFOSYSTCH 31 DRREDDY-CIPLA 32 DRREDDY-DIVISLAB 33 FINANTECH-ROLTA 34 PATNI-HCLTECH 35 PATNI-POLARIS |
- By using Chris Brooks procedure 9 pairs have been eliminated as they did not show stationarity . The following pairs have been removed
> test <- sector.tests[sector.tests$uroot < 0.05 & sector.tests$kpss == + 0, ] > as.data.frame(paste(test$tickeri, test$tickerj, sep = "-")) if (stringsAsFactors) factor(x) else x 1 BANKINDIA-ANDHRABANK 2 GTOFFSHORE-ABAN 3 INDHOTEL-HOTELEELA 4 IOC-CAIRN 5 SBIN-BANKINDIA 6 SCI-GESHIP 7 SCI-CONCOR 8 DRREDDY-RANBAXY 9 PATNI-ROLTA |