## Regression with time series error term. #Look at the example in the book. TInt1year=read.csv("1yearTreasury1962to2008.csv",header=T)[,2] ### These are weekly data given in percents TInt3year=read.csv("3yearTreasury1962to2008.csv",header=T) [,2] plot(TInt1year,type="l",col="green") lines(TInt3year,col="red") ## If you want some semblance of the years on the x axis plot(seq(1962,2008.6,by=1/52),TInt1year,type="l",col="green") lines(seq(1962,2008.6,by=1/52),TInt3year,col="red") plot(TInt1year,TInt3year,lty=15,pch=20) ## a linear relation seems very appropriate plot(diff(TInt1year),diff(TInt3year),lty=15,pch=20) ## changes in the rates (Figure 2.18 in the book) #So let us try regression: model1=lm(TInt3year~TInt1year) summary(model1) ## Seemingly very good model with very high R squared. plot(model1) ## regular diagnostic plots - do not exhibit much except for the normal plot ## Now let us plot residuals in order and test for white noise plot(model1$residuals,type="l") acf(model1$residuals) ## clearly not white noise library(Rmetrics) Box.test(model1$residuals,lag=10,type="Ljung") ## recall: null hypothesis= data is not correlated for the first x lags ## unit root testing: ar(TInt1year) adfTest(TInt1year,lag=31) ## seems there is a unit root #So consider differences instead c1=diff(TInt1year) c3=diff(TInt3year) model2= lm(c3~c1) summary(model2) plot(model2) ## actually these plots look worse than the previous ones plot(model2$residuals,type="l") acf(model2$residuals) ## this plot now looks like a tipical time series - clearly not white noise though which is why we get those types of plots before ## Now we can use the methods before for the residual series and build a time series model for them. library(tseries) eacf.tables(model2$residuals) [1] "EACF table" [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [1,] 0.20725106 -0.01466150 0.012055648 0.058064295 -0.027020632 -0.112183716 -0.07208801 -0.008516606 0.004243084 -0.049461130 -0.021972854 0.045572990 0.063085688 0.007052264 0.0201031609 [2,] 0.26991046 0.07760036 0.003807543 0.045080173 -0.015836923 -0.083043365 -0.06173470 -0.033679788 -0.001895037 -0.050692228 -0.020809012 0.032534402 0.060070599 -0.020248600 0.0036882025 [3,] 0.39707963 -0.05912534 0.057812474 0.005066825 -0.020660967 -0.054247475 -0.04816528 -0.009207476 0.008883503 -0.032232609 0.009485697 0.006250752 0.065085458 -0.034689448 0.0148670866 [4,] -0.43677507 0.46079873 0.060507323 0.033523790 -0.016071314 -0.035601214 0.01163453 -0.043626566 0.015142979 -0.038798775 0.016600557 0.001823417 0.066025991 0.010966393 0.0181060038 [5,] 0.50076516 0.41816280 -0.197480150 0.022985606 0.023561232 -0.061910919 -0.01576405 -0.057302779 -0.016295076 -0.043135500 0.008493218 0.014094497 0.065648352 -0.025273849 -0.0009959697 [6,] -0.41789645 0.35779394 -0.188828695 0.084779814 0.056527709 -0.008784663 0.01748631 -0.034251846 0.022707777 0.008237890 -0.029258792 -0.001522256 0.062339300 -0.003059410 -0.0019600549 [7,] -0.30783439 0.16078711 -0.010703655 -0.239147502 0.009283573 0.032408981 0.08030709 -0.021399882 0.021193350 0.008272329 -0.027415948 -0.001360737 0.007768792 -0.025392075 -0.0003958602 [8,] 0.12946804 0.15539932 -0.001025325 -0.220171953 0.007932727 0.347155822 0.15366425 0.030130596 0.021835363 -0.012364341 0.001203381 -0.019569556 -0.010183169 -0.026035968 -0.0001696920 [9,] -0.41983302 0.03457927 -0.091594852 -0.200576895 0.161472595 0.312895922 0.32169272 -0.078550578 0.034024199 -0.006800506 -0.006686691 -0.009407021 0.006184285 -0.035786172 -0.0193931845 [10,] 0.18095144 0.13169722 -0.420488558 -0.144667824 0.209760613 0.011112326 0.34388978 0.154055188 0.050869412 -0.006402273 -0.009064084 -0.007257026 -0.006501958 -0.029750438 0.0146730632 [11,] -0.04661440 0.48724755 0.320824371 0.173425608 0.214241118 -0.006641865 0.19947874 0.138899041 -0.090340693 0.023929135 0.010043943 0.005268960 -0.001996635 -0.025970854 0.0112097349 [12,] 0.05598433 0.32370748 0.112903269 -0.135343039 0.394632075 0.062844704 0.18052006 0.056692805 -0.143524829 0.121949633 -0.009155198 0.007451496 -0.001434966 -0.020570400 0.0087381065 [13,] -0.49853264 0.35370204 0.069480324 -0.246694298 0.377604080 -0.058165423 0.18142364 -0.066017057 -0.162440727 0.084803207 0.060202820 0.005344994 0.005948702 -0.025321927 0.0097228252 [14,] 0.21946437 -0.28370609 -0.324511362 0.093385415 0.086896654 0.205409620 0.17353565 -0.076467488 -0.135873874 0.117714755 -0.031924566 0.036419328 0.038159972 -0.048071605 -0.0015671801 [15,] 0.30780099 -0.42220020 -0.322695215 0.290446967 -0.007640309 0.132274788 0.10292990 0.245154007 -0.094054692 -0.089096715 -0.125711868 -0.100383206 -0.211028380 -0.038782795 -0.0119638562 [1] " " [1] "Simplified EACF: 2 denotes significance" [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [1,] 2 0 0 2 0 2 2 0 0 2 0 2 2 0 0 [2,] 2 2 0 2 0 2 2 0 0 2 0 0 2 0 0 [3,] 2 2 2 0 0 2 2 0 0 0 0 0 2 0 0 [4,] 2 2 2 0 0 0 0 2 0 0 0 0 2 0 0 [5,] 2 2 2 0 0 2 0 2 0 2 0 0 2 0 0 [6,] 2 2 2 2 2 0 0 0 0 0 0 0 2 0 0 [7,] 2 2 0 2 0 0 2 0 0 0 0 0 0 0 0 [8,] 2 2 0 2 0 2 2 0 0 0 0 0 0 0 0 [9,] 2 0 2 2 2 2 2 2 0 0 0 0 0 0 0 [10,] 2 2 2 2 2 0 2 2 2 0 0 0 0 0 0 [11,] 2 2 2 2 2 0 2 2 2 0 0 0 0 0 0 [12,] 2 2 2 2 2 2 2 2 2 2 0 0 0 0 0 [13,] 2 2 2 2 2 2 2 2 2 2 2 0 0 0 0 [14,] 2 2 2 2 2 2 2 2 2 2 0 0 0 2 0 [15,] 2 2 2 2 0 2 2 2 2 2 2 2 2 0 0 Warning message: In optim(coef, err, gr = NULL, hessian = TRUE, ...) : one-diml optimization by Nelder-Mead is unreliable: use optimize ## Perhaps MA(1) is appropriate modelresid=arima(model2$residuals,order=c(0,0,1),include.mean=FALSE) modelresid Call: arima(x = model2$residuals, order = c(0, 0, 1), include.mean = FALSE) Coefficients: ma1 0.2241 s.e. 0.0200 sigma^2 estimated as 0.004411: log likelihood = 3132.7, aic = -6261.41 arima(model2$residuals,order=c(0,0,1),include.mean=T) ## In fact the mean is zero as this shows ## So the model is: ## c3= 4.087*10^{-05}+0.792 c1 + e where e_t=a_t+0.2241 a_{t-1} and a_t a white noise ## rewriting the model in terms of r1 and r3 and using the fact that the intercept is zero we get: ## r3_t=r3_{t-1}+0.792*(r1_t-r3_{t-1}) + a_t+0.2241 a_{t-1}