# Script for Class 2 # SS link is www.stat.pitt.edu/stoffer/tsa2 # set working directory for convenience to place on your system setwd("~/courses/stat910/") # --- Threshold model threshold.ar <- function(n,phi1,phi2,theta,n0=100) { y <- rnorm(n + n0); w <- rnorm(n + n0); for (t in 2:(n+n0)) { if(y[t-1] < theta) phi <- phi1 else phi <- phi2; y[t] <- phi * y[t-1] + w[t] } y[(n0+1):(n0+n)] } plot(threshold.ar(200,-.9,.8,1), type="l") plot(threshold.ar(200,-1.5,.8,1), type="l") plot(threshold.ar(200,1.5,.8,1), type="l") # --- Bilinear model bilinear <- function(n,a,b,n0=100) { y <- rnorm(n + n0); w <- rnorm(n + n0); for (t in 2:(n+n0)) { y[t] <- a * y[t-1] + b * w[t] * y[t-1] + w[t] } y[(n0+1):(n0+n)] } plot(bilinear(400,.8,.2), type="l") plot(bilinear(400,-.7,.5), type="l") plot(bilinear(400,-.8,.7), type="l") plot(bilinear(400,-.8,-.8), type="l")