LAB 9
.docx
keyboard_arrow_up
School
George Mason University *
*We aren’t endorsed by this school
Course
MISC
Subject
Industrial Engineering
Date
Dec 6, 2023
Type
docx
Pages
3
Uploaded by mummadiashwini17
LAB 9:
Autocorrelation and Autoregressive Models
PART I:
Practice Problems with code and outputs given in the R
Demonstration Videos
.
Refer to the R Demonstration Videos for a walk-through of Part I.
You are expected to duplicate this demonstration as
Part I of the lab and submit it for credit and grading.
Save the
Amtrak.csv
file into a known folder.
The dataset file is found in the left-hand menu of Blackboard in a
link titled
Data Sets
Open RStudio.
Go to File > New File > R Script
Save your Untitled1.R file into the same directory that your
csv
is saved.
Give it the name
Autocorrelation.R
From the Menu Bar, go to Sessions > Set Working Directory > To Source File Location; This sets the working
directory as the place where both the R file and the csv file are saved
.
Duplicate the following code:
Autocorrelation and AR models
The dataset
Amtrak.csv
contains counts of riders on Amtrak trains in thousands
Install the
forecast
package
library(forecast)
amtrak.df<-read.csv(“Amtrak.csv, header=T)
amtrak.ts<-ts(amtrak.df$Ridership, start = c(1991, 1), end = c(2004, 3), freq = 12)
create time series object using ts()
ts() takes three arguments: start, end, and freq.
With monthly data, the frequency of periods per cycle is 12 (per year).
Arguments start and end are (cycle [=year], seasonal period [=month] number)
Here start is Jan 1991: start = c(1991, 1); end is Mar 2004: end = c(2004,3)
plot(amtrak.ts, xlab = "Time", ylab = "Ridership (in 000s)“)
plot the data
amtrak.full.qseason<-tslm(amtrak.ts~trend+I(trend^2)+season)
amtrak.full.qseason.pred<-forecast(amtrak.full.qseason, h=1, level=0)
amtrak.full.qseason.pred
combining the training and validation dataset to get an improved forecast from chosen “best” model
Acf(amtrak.ts, lag.max = 12, main = “ACF of Data”)
Compute and display autocorrelation for different lags
Acf(amtrak.full.qseason$residuals, lag.max = 12, main = “ACF of Residuals”)
Autocorrelation for the residuals
amtrak.res.ar1 <- Arima(amtrak.full.qseason$residuals, order = c(1,0,0))
summary(amtrak.res.ar1)
Fit an AR(1) model to residuals.
Use Arima() in the forecast package to fit an ARIMA model.
The ARIMA models include the AR models;
order c(1,0,0) gives an AR(1) model
amtrak.res.ar1.pred <-forecast(amtrak.res.ar1,h = 1, level=0)
amtrak.res.ar1.pred
predicted residual
amtrak.res.ar1.pred$mean+amtrak.full.qseason.pred$mean
improved forecast that combines the predicted residual and the previously predicted output
Acf(amtrak.res.ar1$residuals, lag.max=12,main=”ACF of residuals for AR(1))
ACF on the residuals of the AR(1) model
plot(amtrak.full.qseason$residuals, ylim=c(-250,250),ylab=”Residuals”)
lines(amtrak.res.ar1.pred$fitted,col=”blue”)
A plot of actual residuals vs. the forecasted residuals
PART 2:
Practice Problems with outputs given
.
Using R, answer the following questions and check your results with the given output provided.
1.
For the
Amtrak
dataset, fit a cubic plus seasonal model to the entire data set and generate a graph of the ACF
function on the residuals.
Graph the plot for two years (lag.max=24)
Solution:
2.
For the model generated in the previous question (cubic plus seasonal), fit an AR(1) model to the residuals and
predict the residual for April 2004.
Combine the predicted residual with the predicted output of April 2004 using
the cubic + seasonal model to get an improved forecast.
Report that improved forecast.
Solution:
Apr
2004 2149.518
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help