#####Chapter 7: Errors and Residuals

###Section 7.2:  Heteroscedasticity
x=c(8,9,10,11,12,13,14,15,16,17,18,19)
y=c(11,10.5,11.2,9.8,9.2,9,8.6,8.3,7,10,10.5,6)
hetero=lm(y~x)
summary(hetero)
ms.res <-sum(resid(hetero)^2)/((length(x)-2)) 

#White's Test of Heteroscedasticity
res.sqr <-resid(hetero)^2
white.test <-lm(res.sqr~x+I(x^2))
white.chi <-summary(white.test)$r.squared*length(x)
white.prob <- 1 - pchisq(white.chi, 2)
white <-cbind(white.chi, white.prob)
white

#Breusch Pagan Test of Heteroscedasticity 
library(car)   #attach car package
BP <-ncvTest(hetero)
BP

#Weighted Least Squares
abs_e=abs(hetero$residuals) #Calculate absolute value of residuals
weights <-lm(abs_e~x)  #Regress absolute value of residuals on x
w.int =1/abs(fitted(weights)) #Create weighted variables 
w.x =x/abs(fitted(weights))
w.y =y/abs(fitted(weights))
WLS <-lm(w.y~w.int+w.x-1) #regress weighted variables (no intercept)
summary(WLS) 

#Shorter way to perform WLS using Weights commands
weight1=1/(weights$fitted^2)
WLS.1=lm(y~x,weights=weight1)
summary(WLS.1)

#Using Gamma as a Transformation Matrix for WLS
V <-cbind(1,x,y)
gamma <-diag(sqrt((ms.res/fitted(weights)^2)))
vv <-gamma%*%V
vv.reg <-lm(vv[,3]~vv[,1:2]-1)
summary(vv.reg)

#HC_3 Heteroscedasticity Consistent Covariance Matrix
library(lmSupport) #attach lmSupport package
lm.correctSE(hetero, digits=6)

###Section 7.3: Autocorrelations
x=c(84,83,81,83,87,83,87,87,84,86,81,86)
y=c(.5384,.5471,.5663,.5677,.6187,.6059,.6131,.5944,.5492,.5183,.5017,.5689)
autoreg=lm(y~x)
summary(autoreg)

#Durbin Watson and Breusch Godfrey
library(lmtest)              #attach lmtest package
dwtest(autoreg)
bgtest(autoreg)

#Estimate Rho 1 iteration
e=autoreg$residuals
n <- length(e)
rho.1 <-lm(e[2:n]~e[1:(n-1)]); rho.1$coef[2]

#Generalized Least Squares using Maximum Likelihood Estimation
library(nlme)                #attach nlme package
gls1 <- gls(y~x, correlation = corAR1(), method = "ML")
summary(gls1)
gls1$fitted
gls1$residuals
vcov(gls1)

#Newey-West to Match Lag=3 Example Output is Corrected Covariance Matrix
library(sandwich)            #attach sandwich package
NeweyWest(autoreg, lag = 3, prewhite = FALSE)
