#R example Section 3
data1<-read.table("E:L3eg1data.txt")
realgoldprice<-data1[,1]
realstockprice<-data1[,2]
a.lm<-lm(realgoldprice~realstockprice)
resid01<-a.lm$resid
plot(resid01)
length(resid01)
lines(seq(1:33), rep(0, 33))
length(resid01)
residl0<-resid01[2:33]
residl1<-resid01[1:32]
plot(residl1, residl0)
#R example Section 4
#need to load the tseries package
residsign<-1*(resid01>0)
residsign<-factor(residsign)
runs.test(residsign)
#durbin-watson example
#need to load the lmtest package
dwtest(a.lm)
#R example Section 5
arima(realgoldprice, xreg=realstockprice, order=c(1, 0, 0))
coeff<-c(0.5578,     3.9406,         -0.0487)
ese<-c(0.1545,     0.5675,          0.0247)
t<-abs(coeff)/ese
2*(1-pt(t, 30))
#tutorial exercise
#part 1
oil<-c(8.597, 8.572, 8.649, 8.688, 8.879, 8.971, 8.680, 8.349, 8.140, 7.613, 7.355, 7.417, 7.171, 6.847, 6.662, 6.560, 6.465, 6.452, 6.252, 5.881, 5.882)
gas<-c(1770, 1780, 1805, 1865, 1915, 1910, 1915, 1960, 1980, 1795, 1750, 1630, 1535, 1329, 1241, 1169, 1006, 967, 940, 907, 840)
#part 2
gas.lm<-lm(gas~oil)
summary(gas.lm)
#part 3
plot(gas.lm$resid)
length(gas.lm$resid)
lines(seq(1:21), rep(0, 21))
length(gas.lm$resid)
residl1<-gas.lm$resid[1:20]
residl0<-gas.lm$resid[2:21]
plot(residl1, residl0, xlab="Lag 1 residuals", ylab="Lag 0 residuals") 
#part 4
#need to make sure you have loaded the tseries package in order to get this to work
posres<-1*(gas.lm$resid>0)
posres<-factor(posres)
runs.test(posres)
#part 5
#need to make sure you have loaded the lmtest package in order to get this to work
dwtest(gas.lm)
#part 6
arima(gas, xreg=oil, order=c(1, 0, 0))
#part 7
#Look for the same interpretation about autocorrelation coming from the two graphical tests and the three numerical tests
#using the values generated from Part 6 above
0.8978/0.0989
#degrees of freedom=sample size-number of estimated parameters
21-3
2*(1-pt(9.077856, 18))
#part 8
#To see the ordinary least squares equation use either
gas.lm
#or better would be
summary(gas.lm)



