# Seasoanl Unit root Test--- Tutorial for Time Series Analysis 1 @ FaraDars
#----------------------------------------------
# Call Corresponding Packages : Namely "uroot" and "forecast"
# Note That: The abave Packages must be installed already in R before calling them
# If Not, Type install.packages("uroot") and install.packages("forecast") in R-console.
library(uroot)
library(forecast)

# Use Example Data 
data("ch-data")
hours <- diff(log(ch.data$hours))

# Visual Inspection
plot(hours)
acf(hours)

# You Can Use monthplot for Ploting a Seasonal or other Subseries from a Time Series. 
require(graphics)
monthplot(hours, choice = "seasonal", cex.axis = 0.8)

# Or Using the Following Function You can Use "Boxplot" for
# To see The Seasonal Pattern.
require(ggplot2)

ts_plot_season <- function(x = x) {
  season <- cycle(x)
  season.factor <- factor(season)
  ggplot() + 
    geom_boxplot(mapping = aes(x = season.factor,
                               y = x)) +
    labs(x = "Periodo", y =  "Serie")
}

ts_plot_season(hours)


##############################
# Canova and Hansen (1995) Test for Seasonal Stability
help(ch.test)
res1 <- ch.test(x = hours, type = "dummy", lag1 = TRUE, NW.order = 4)
res1
# the auxiliary regression is stored in the element "fitted.model"
summary(res1$fit)
# For Trigonometric Type
res2 <- ch.test(x = hours, type = "trigonometric", lag1 = TRUE, NW.order = 4)
res2
summary(res2$fit)

# Hylleberg, Engle, Granger and Yoo (HEGY) test statistics for the null hypothesis seasonal unit roots.
help(hegy.test)

res3<-hegy.test(hours, deterministic = c(1,1,1), lag.method = "fixed", maxlag = 1)
res3
################################################################
## Another Procedure for testing seasonal Unit root Using Package "forecast" 
# Osborn, Chui, Smith and Birchenhall [1988] test is the modification of 
# Dickey, Hasza and Fuller [1984].
# ouput: "1??? means that there is a seasonal unit root
# and "0??? that there is no seasonal unit root.
help(nsdiffs)
nsdiffs(hours, test="ch")
nsdiffs(hours, test="ocsb")

## End 


