w1=rnorm(600,0,1) w2=filter(w1,sides=2,rep(1,3)/3) w3=vector(length=600) for (i in 1:600)w3[i]=rnorm(1,0,i) par(mfrow=c(3,1)) plot.ts(w1) plot.ts(w2) plot.ts(w3) ################################################################## #Autoregresja x_n=x_(n-1)+0.9x_(n-2)+w_n par(mfrow=c(1,1)) w4=filter(rnorm(650,0,1),filter=c(1,-.8),method="recursive") plot.ts(w4[51:650]) ################################################################## #Błądzenie przypadkowe z dryfem i bez dryfu #xn=0.1+x(n-1)+wn;wn-IID N(0,1) set.seed(100) w=rnorm(300,0,1);x=cumsum(w) wd=w+.1;xd=cumsum(wd) plot.ts(xd,ylim=c(-10,40)) lines(x) lines(.1*(1:300),lty="dashed") ################################################### # AR1s set.seed(100) x1=arima.sim(list(order=c(1,0,0), ar=.8), n=16000) acf(x1,lag.max =25) plot(x1) acf(x1)$acf x2=arima.sim(list(order=c(1,0,0), ar=-.8), n=500) plot(x2) acf(x2,max.lag=25) par(mfrow=c(2,1)) plot(x1, main="AR1 phi = +.8") plot(x2, main="AR1 phi = -.8") par(mfcol=c(2,2)) acf(x1, 20) acf(x2, 20) pacf(x1, 20) pacf(x2, 20) ################################################### # AR2 x=arima.sim(list(order=c(2,0,0), ar=c(1,-0.5)), n=50000) par(mfcol=c(3,1)) plot(x) acf(x, 20) pacf(x, 20) par(mfrow=c(2,1)) acf(MA1,20,xlim=c(1,20)) pacf(MA1,20,ylim=c(-.2,1)) ################################################### #MA(2) set.seed(100) a=arima.sim(list(order = c(0,0,2),ma=c(.8,.8)),n=100) plot(a) par(mfcol=c(2,1)) pacf(a,lag.max =40) acf(a,lag.max =40) acf(a)$acf #********************************************************** # ARMA(1,1) x=arima.sim(list(order=c(1,0,1), ar=.8, ma=-.2), n=500) par(mfcol=c(3,1)) plot(x) acf(x, 30) pacf(x, 30) #********************************************************** x = arima.sim(list(order=c(1,0,1), ar=.8, ma=-.2), n=500) (x.fit = arima(x, order = c(1, 0, 1))) #Remember: There is mean and not intercept tsdiag(x.fit, gof.lag=20) x.f=predict(x.fit, n.ahead=10) U = x.f$pred + 2*x.f$se L = x.f$pred - 2*x.f$se minx=min(x,L) maxx=max(x,U) par(mfcol=c(1,1)) ts.plot(x,x.f$pred,col=1:2, ylim=c(minx,maxx)) lines(U, col="blue", lty="dashed") lines(L, col="blue", lty="dashed")