```# -------------------------------------------------------------------
# Lecture 2: Stationarity testing, PPP example from Enders, 2004

# Libraries: tseries, zoo
rm(list=ls())
setwd("~/Desktop/Cam/MFin Lectures Lent/Lecture2/Example")
ls()
# -------------------------------------------------------------------

names(ppp) <- c("Date","RER")
str(ppp)
library(zoo)
ppp\$Date <- as.yearmon(ppp\$Date, format="%YM%m")

# In other words if the PPP holds we should find out that the real exchange rate is stationary.
# Let’s test this theory employing the Augmented Dickey-Fuller test.
# Additionally note that the theory implies that our test can include only a constant (and not a trend).

# This is the real exchange rate for UK, defined as US prices in £ the numerator and
# UK prices in the denominator
plot(RER ~ Date, type="l", data=ppp)

# does not allow specification of constant/trend in the test!
library(tseries)

# Alternatives:
source("http://klein.uk/R/myfunctions.R")

# Augmented Dickey Fuller test (correct p-value)
# The arguments of the function are as in the original adf.test function, i.e.
# x    = a numeric vector or time series.
# k    = the lag order to calculate the test statistic
#        defaults to (n-1)^(1/3)
# int   = logical, a constant is included if int=T
# trend = logical, a trend variable is included if trend=T

# Augmented Dickey Fuller test (summary table)
# The arguments of the function are:
# x     = time series vector
# k     = number of lags to be included in the test
# int   = logical, a constant is included if int=T
# trend = logical, a trend variable is included if trend=T

# The null hypothesis that the RER series has a unit root cannot be rejected
# (p-value 0.46)

## --- A procedure to test for unit roots ---
# Walter Enders (2004) Applied Econometric time series, page 213 ff

# Step 2: If null is NOT rejected, check
# where to many deterministic regressors included in step 1?
# test for significance of the of the trend term by joint hypothesis

lm1 <- adf.test.2(x=ppp\$RER, int=T, trend=T, k=0)
summary(lm1)

# wrong p-value from F-distribution:
linearHypothesis(lm1, c("xD.lag(x, -1)", "xtime(x)"))

# correct p-value from DF-table
linearHypothesis.adf(lm1, c("xD.lag(x, -1)", "xtime(x)"), int=T, trend=T)
# We can not reject H0. -> Coeffient of time-trend is zero

# Step 3: If null is not rejected, estimate model without trend