#************************************************************ 
# 1. Spurious Regresson 
# Two random work series regression model
# Simulation based Granger and Newbold (1974)
# "Spurious Regressions in Econometrics 
#install.packages("nortest")
#------------------------------------------------------------
library(lmtest)
library(nortest)
# 1) Simple Example with trend

n = 50 
y = rep(0,n)
x = rep(0,n)
ey = rnorm(n)
ex = rnorm(n)
xrho = 1
yrho = 1
x[1] = 100
y[1] = 100
t = seq(1,n)

for (i in 2:n) {
  y[i] = yrho*y[i-1] + 0.050*t[i-1] + ey[i]
  x[i] = xrho*x[i-1] + 0.025*t[i-1] + ex[i]
}
ols <- lm( y ~ x)
summary(lm( y ~ x))
dwtest(lm( y ~ x))
y <- as.ts(y)
x <- as.ts(x)
ts.plot(y,x,col=c("red", "blue"))

# 2) Normality

e <- y - predict(ols)
m <- mean(e)
std <- sqrt(var(e))
hist(e,breaks=15,prob=TRUE)
curve(dnorm(x,mean=m,sd=std),col="darkblue",lwd=2, add=TRUE, yaxt="n")
qqnorm(e,col=4)
qqline(e,col=2)

# Shapiro-Wilk test of normality.
shapiro.test(e)
# Anderson-Darling Normality Test
ad.test(e)
