###---------------------------------------------------------------------------- ### MA(1), MA(2) ###---------------------------------------------------------------------------- T <- 1000 ## Generate the epsilons epsilon <- rnorm(T, sd = 0.1) epsilon_t<- epsilon[1:(T-1)] epsilon_t1 <- epsilon[2:T] ## Generate Y y_t <- 40 + epsilon_t + 0.8*epsilon_t1 ## Plot the time series par(mfrow = c(1, 3)) plot(y_t, type = "b") ## Plot the sample ACF acf(y_t) ## Plot the sample PACF pacf(y_t) ## Question: Generate MA(3) model and check the ACF patterns. ## y_t = 40 + epsilon_t + 0.7*epsilon_t1 - 0.28*epsilon_t2 ###---------------------------------------------------------------------------- ### AR(1) ###---------------------------------------------------------------------------- T <- 100 ## Generate the epsilons epsilon <- rnorm(T) y <- 0 for(t in 2:T) { y[t] <- 8 + 0.8 * y[t-1]+ epsilon[t] } ## Plot the time series par(mfrow = c(1, 2)) plot(y, type = "l") ## Plot the sample ACF acf(y) ###---------------------------------------------------------------------------- ### AR(2) ###---------------------------------------------------------------------------- T <- 1000 ## Generate the epsilons epsilon <- rnorm(T, sd = 0.5) y <- NULL y[1] <- 0 y[2] <- 1 y[3] <- 3 for(t in 4:T) { y[t] <- 4 + 0.4 * y[t-1] +0.5 * y[t-2] - 0.7 * y[t-3] + epsilon[t] } ## Plot the time series par(mfrow = c(1, 3)) plot(y, type = "l") ## Plot the sample ACF acf(y) ## Plot the sample PACF pacf(y) ###---------------------------------------------------------------------------- ### Estimate ARIMA model (Bank Loan example) ###---------------------------------------------------------------------------- loan <- read.table("TimeSeriesTable5.5.csv", header = TRUE) y <- loan$Applications ## Plot the time series and ACF, PACF par(mfrow = c(2, 2)) plot(loan, type = "b") acf(y) pacf(y) ## We try to fit an AR(2) model. WHY? AR2 <- arima(y, order = c(1, 0, 1)) AR2 ## The residual plot epsilon <- as.numeric(AR2$residuals) par(mfrow = c(3, 2)) plot(loan, type = "b") plot(epsilon, type = "l") hist(epsilon) qqnorm(epsilon) qqline(epsilon) acf(epsilon) pacf(epsilon)