Purpose
Confirmatory Analysis

Perform Unit Root with H0 - Unit Root HA - No Unit Root

Perform KPSS with H0- Stationary HA - Non Stationary

Then check for how many pairs do both the test give the same result.

> sector.tests <- samesector
> sector.tests$uroot <- 0
> sector.tests$kpss <- 0
> npairs <- dim(samesector)[1]
> pair <- 1
> x <- c(0, 0, 0, 0)
> for (pair in 1:npairs) {
+     print(pair)
+     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.1) {
+         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
+             x[1] <- x[1] + 1
+         }
+         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)
+             x[2] <- x[2] + 1
+         }
+         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)
+             x[3] <- x[3] + 1
+         }
+         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
+             x[4] <- x[4] + 1
+         }
+         t1 <- unitrootTest(error.transf, lags = 1, type = "ct")@test$p.value[1]
+         kpfit <- urkpssTest(error.transf, type.2, "short", doplot = F)
+         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.1 & temp2 < 0.1) {
+         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
+             x[1] <- x[1] + 1
+         }
+         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)
+             x[2] <- x[2] + 1
+         }
+         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)
+             x[3] <- x[3] + 1
+         }
+         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
+             x[4] <- x[4] + 1
+         }
+         t1 <- unitrootTest(error.transf, lags = 1, type = type.1)@test$p.value[1]
+         kpfit <- urkpssTest(error.transf, type.2, "short", doplot = F)
+         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.1 & temp2 > 0.1) {
+         sector.tests$uroot[pair] <- 999
+         sector.tests$kpss[pair] <- 999
+     }
+ }
> categories <- x
> categories
[1]  12 155   0   3

Take away :
Most of the series which show cointegration seem to be having a trend component as 155 pairs have a significant coefficient for time

> test <- sector.tests[sector.tests$uroot != 999, ]
> x <- as.data.frame(cbind(test$uroot, test$kpss))
> x$ustat <- ifelse(x[, 1] < 0.05, 0, 1)
> colnames(x) <- c("uroot", "kpss.status", "uroot.status")
> table(x$uroot.status, x$kpss.status)
      0   1
  0  10  41
  1   6 113

Takeaway
The above result is pretty depressing as it shows that 113 pairs for which Unit Root H0 holds good , KPSS Ha holds good. Basically they confirm the same thing. They are non stationary

10 pairs for which Unit Root Ha holds good , KPSS Ho holds good. Basically they confirm the same thing. They are stationary

Now comes the troublesome part 41 pairs for which Unit Root Ha holds good , KPSS Ha holds good. Basically do not concur

6 pairs for which Unit Root Ho holds good , KPSS Ho holds good. Basically do not concur.

Out of the 313 pairs , only 170 showed granger causality. Out of 170 pairs, only 10 concur with stationarity and dickey fuller test

  1. Does that mean there are only 10 pairs that can be tradable ?