hist(rnorm(2000)) #white noise WN<-ts(rnorm(1400), frequency=4, start=c(1980,1)) ts.plot(WN,ylab="White noise") MA_1<-WN+0.9*lag(WN,1) ts.plot(MA_1,ylab="Moving average") acf(MA_1,lag.max = 15) MA_2<-WN+0.9*lag(WN)+0.6*lag(WN,2) ts.plot(MA_2,ylab="Moving average") acf(MA_2,lag.max = 15) acf(WN,lag.max=20) AR_1<-0*WN AR_1[1]<-WN[1] for(i in 2:length(AR_1)) AR_1[i]<-0.99*AR_1[i-1]+WN[i] ts.plot(AR_1) AR_1<-arima.sim(model=list(ar=c(.9)),n=(50))#simule tout seul op <-par(no.readonly=TRUE) layout(matrix(c(1,1,2,3),2,2,byrow=TRUE) ) ts.plot(AR_1,ylab="Autoregressive") acf(AR_1, main='Autocorrelations') pacf(AR_1, main='Parital autocorrelations') par(op) #un ARMA(2,1) ARMA_12<-arima.sim(model=list(ar=c(.8),ma=c(.7,.6)),n=1000)#simule tout seul op <-par(no.readonly=TRUE) layout(matrix(c(1,1,2,3),2,2,byrow=TRUE) ) ts.plot(ARMA_12,ylab="Autoregressive") acf(ARMA_12, main='Autocorrelations') pacf(ARMA_12, main='Parital autocorrelations') par(op) tsdisplay(ARMA_12, lag.max=10) library(forecast) bestmodel<-auto.arima(ARMA_12, ic="bic") Box.test(bestmodel$residuals, type= "Ljung-Box") acf(bestmodel$residuals) par(mfrow=c(1,1)) pacf(residuals(bestmodel)) #un ARMA(2,1) ARMA_12<-arima.sim(model=list(ar=c(.8),ma=c(.7,.6)),n=1000)#simule tout seul ts.plot(ARMA_12) bestmodel<-auto.arima(ARMA_12,d=0,seasonal = F,ic="bic") forecast.Arima(bestmodel) par(mfrow=c(1,1)) plot(forecast.Arima(bestmodel,h=15),xlim=c(970,1015)) ----------------------- #Random walk shock=ts(rnorm(140), frequency=4, start=c(1970,1)) RW<-diffinv(shock) ts.plot(RW,ylab="Random walk") RW<-diffinv(shock+.2) ts.plot(RW,ylab="Random walk + trend") #install.packages("fpp") library(fpp) consumption<-diffinv(usconsumption[,1])/sqrt(usgdp) ts.plot((consumption)) acf(consumption, 20) pacf(consumption, 20) acf(diff(consumption), 20) pacf(diff(consumption), 20) ts.plot(diff(consumption)) income<-diffinv(usconsumption[,2])/sqrt(usgdp) ts.plot(income,consumption,col = c("red","blue")) legend("topleft", legend = c("income", "consumption"), fill = c("red","blue")) ts.plot(diff(income)) acf(diff(income), 20) pacf(diff(income), 20) install.packages("urca") library(urca) df=ur.df(income, type="trend") summary(df) #first value of test-stat (-2.7) is closer to 0 than levels (-3.13): #accept null hypothese -> cointegration. #check the trend (third value and levels line): 10.9 is larger than 8.4: #very significant, there is a trend. #The same for the constant. We accept the full model. df=ur.df(consumption, type="trend",lags=1) summary(df) #first value of test-stat (-1.97) is closer to 0 than 10% level (-3.13): #accept null hypothese -> cointegration. #check the trend (third value and levels line): 6.6 is larger than 6.5: #significant, there is a trend. #No constant. We accept the trend model without constant. coint=ca.jo(usconsumption) summary(coint) #test for cointegration between variables. #compare column test with the levels: #for r<=1 test is larger than 1% level so there is a very significant cointegration relation res= lm(consumption~time(consumption)) summary(res) ts.plot(consumption-1.4537*income) usconsumption