#************************************************************ 
# Unit Root Test 
# Jin Man Lee
# install.packages("TTR")
# install.packages("forecast")
#------------------------------------------------------------

library("TTR")
library("forecast")
library(quantmod)

getSymbols("GDPC1", src = "FRED")

x0 <- ts(GDPC1,start=c(1947,1),frequency = 4)
plot.ts(x0)

x = window(x0,start=c(2000,1),end=c(2015,4))
class(x)
plot(x)

y <- x - lag(x)


# UniRoot Tests using urca

df1 <- ur.df(y, type = "drift",  selectlags ="AIC")
pp1 <- ur.pp(y, type = "Z-tau", model = "constant",lags ="short")
kp1 <- ur.kpss(y, type = "tau",  lags ="short")
summary(df1)
summary(pp1)
summary(kp1)

# UnitRoot Tests using tseries
df20 <- adf.test(y, alternative = "stationary", k = 0)
r0 <- c(df20$statistic,df20$p.value)
r0

df24 <- adf.test(y, alternative = "stationary", k = 4)
r1 <- c(df24$statistic,df24$p.value)

pp2 <- pp.test(y,lshort=TRUE,type="Z(t_alpha)")
r2 <- c(pp2$statistic,pp2$p.value)

kp2 <- kpss.test(y,lshort=TRUE,null = c("Level", "Trend"))
r3 <- c(kp2$statistic,kp2$p.value)

table1 <- cbind(r0,r1,r2,r3)
table1


