Skip to content

Instantly share code, notes, and snippets.

@Protonk
Last active April 6, 2020 15:00
Show Gist options
  • Select an option

  • Save Protonk/819426 to your computer and use it in GitHub Desktop.

Select an option

Save Protonk/819426 to your computer and use it in GitHub Desktop.
Simple, non-technical stationary/non-stationary process examples
#ADF tests for both come from the function afd.test() in tseries. The means are just simple means over the ranges I chose.
####Stationary Process####
set.seed(42)
y<- w<- rnorm(1000)
for (t in 2:1000) {
y[t]<- 0.9*y[t-1]+w[t]
}
#Vector for line segments.
xy.mat<- cbind(c(0,450,225,750,600,1000),c(6.5,6.5,7.5,7.5,8.5,8.5))
##Plot the series, with annotations for means and the Dicky-Fuller Test##
plot(1:length(y),y,xlab="t",ylab=expression(y[t]),type="l",ylim=c(-8,10),main="Stationary Time Series")
points(xy.mat,pch=20,col="blue",cex=0.75)
for(i in c(1,3,5)) {segments(x0=xy.mat[i,1],y0=xy.mat[i,2],x1=xy.mat[i+1],lty=3,col="blue",lwd=2)}
text(125,7,labels=expression(E(y[t])==-0.220))
text(350,8,labels=expression(E(y[t])==-0.376))
text(725,9,labels=expression(E(y[t])==-0.341))
text(125,-6.5,labels=expression(ADF == -6.128));
####Non-stationary Process####
set.seed(42)
yns<- wns<- rnorm(1000)
for (t in 3:1000) {
yns[t]<-1.5*yns[t-1]-0.5*yns[t-2]+wns[t]
}
plot(1:length(yns),yns,xlab="t",ylab=expression(y[t]),type="l",ylim=c(-100,20),main="Non-stationary Time Series")
xyns.mat<- cbind(c(0,450,225,750,600,1000),c(10.5,10.5,0.5,0.5,-9.5,-9.5))
points(xyns.mat,pch=20,col="blue",cex=0.75)
for(i in c(1,3,5)) {segments(x0=xyns.mat[i,1],y0=xyns.mat[i,2],x1=xyns.mat[i+1],lty=3,col="blue",lwd=2)}
text(125,14,labels=expression(E(y[t])==-13.27))
text(350,4,labels=expression(E(y[t])==-27.56))
text(725,-6,labels=expression(E(y[t])==-64.51))
text(125,-60,labels=expression(ADF == -2.0251));
@robintux
Copy link
Copy Markdown

robintux commented Apr 6, 2020

Hay un problema con la linea 27 :

points(xyns.mat,pch=20,col="blue",cex=0.75)

Error in points(xyns.mat, pch = 20, col = "blue", cex = 0.75) :
object 'xyns.mat' not found

@Protonk
Copy link
Copy Markdown
Author

Protonk commented Apr 6, 2020

Been a little while since I even thought about this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment