Question: R code question # Set repository options: local({r r[CRAN] options(repos=r)}) # Set a vector of strings: package names to use (and install, if necessary) pkg_list
R code question
# Set repository options:
local({r
r["CRAN"]
Question 1: This problem uses CRSP daily returns. First, get the data and plot the ACF in two ways library(Ecdat) data(CRSPday) crsp CRSPday7 acf(crsp) acf(as.numeric(crsp)) a. At what values of lag are there significant autocorrelations in the CRSP returns? For which of these values do you think the statistical significance might be due to chance? b. Use the augmented Dickey-Fuller tests to decide whether the crsp series is nonstationary. Do the test corroborate the conclusions of the time series and ACF plots? c. Fit AR(1) and AR(2) models to the CRSP returns: arima ersp.order (1,0,0) arima(crsp,order-(2,0,0)) Would you prefer an AR(1) or an AR(2) model for this time series? Explain your answer d. Find a 95 % confidence interval for the AR parameter of the AR (1) model. e. Next use autoarima to determine the best-fittng nonseasonal A and compare the results options(repos=r)})
# Set a vector of strings: package names to use (and install, if necessary)
pkg_list = c('zoo', 'tseries', 'MASS','stats','car','moments','fGarch', 'readxl','Ecdat',
'evir','faraway','xts','forecast','nor1mix','bootstrap')
# ensure existing required packages are up to date:
update.packages(ask=FALSE, oldPkgs=pkg_list)
# Install packages if needed
for (pkg in pkg_list)
{
# Try loading the library.
if ( ! library(pkg, logical.return=TRUE, character.only=TRUE) )
{
# If the library cannot be loaded, install it; then load.
install.packages(pkg)
library(pkg, character.only=TRUE)
}
}
library(Ecdat)
data(CRSPday) # get the data
crsp=CRSPday[,7] #CRSP daily returns
pdf("18_March15_crsp_acf.pdf", width=8, height=4)
par(mfrow=c(1,2))
acf(crsp,cex.axis=1.5,cex.lab=1.5,cex.main=1.2,main="(a)")
acf(as.numeric(crsp),cex.axis=1.5,cex.lab=1.5,cex.main=1.2,main="(b)")
graphics.off()
#acf(crsp) #plot the sacf method 1
#acf(as.numeric(crsp)) #plot the data method 2
############################################
# The Box test that the first lag autocorrelations
#are zero
# was performed by using Rs Box.test() function.
Box.test(crsp, lag = 5, type = "Ljung-Box")
#The parameter lag, which specifies the number of
# autocorrelation coefficients to test,was set equalto 5.
###### Model Fitting#################################
fitAR1 = arima(crsp, order=c(1,0,0)) # fit an AR1 model
###### Print the estimates of the model parameters####
print(fitAR1)
##### Model Adequacy Test ###########################
acf(as.numeric(residuals(fitAR1))) # plot the sacf of the residuals
######################################################
# The Box test that the first 5 residual autocorrelations
# are zero was performed by using Rs Box.test() function.
Box.test(residuals(fitAR1), lag = 5, type="Ljung", fitdf=1)
##########################################################
######### Fitting an AR(2) model #####################
fitAR2 = arima(crsp, order=c(2,0,0))
print(fitAR2) ## Estimates of the model parameters
############### Model adequacy test ##########
acf(as.numeric(residuals(fitAR2)))
######################################################
pdf("18_March15_residual_acf.pdf", width=8, height=4)
par(mfrow=c(1,2))
acf(as.numeric(residuals(fitAR1)),cex.axis=1.5,cex.lab=1.5,cex.main=1.2,main="(c)")
acf(as.numeric(residuals(fitAR2)),cex.axis=1.5,cex.lab=1.5,cex.main=1.2,main="(d)")
graphics.off()
#######################################################
# The Box test that the first 5 residual autocorrelations
# are zero was performed using Rs Box.test() function.
Box.test(residuals(fitAR2), lag =5, type="Ljung", fitdf=2)
#########################################################
######### Automated search ##############################
# R package has functions to automate the search
#for the AR model that optimizes AIC criteria.
# The auto.arima function in Rs forecast package found
# that p =2 is the first local minimum of AIC.
library(forecast)
auto.arima(crsp, max.p = 20, max.q = 0, d = 0, ic = "aic")
# R package has functions to automate the search for the AR model
#that optimizes BIC criteria.
# The auto.arima function in Rs forecast package found
# that p =1 is the first local minimum of BIC.
library(forecast)
auto.arima(crsp, max.p = 20, max.q = 5, d = 0, ic = "bic")
############################################################
### Automatic selection of an ARMA model #################
# Now auto.arima() is applied to the crsp data.
#The ARIMA (1,0,0) model is selected by auto.arima() using
# BIC to select p and q after setting d = 0.
auto.arima(crsp, max.p = 5, max.q = 5,d=0, ic = "bic", trace = FALSE)
##### Testing for stationary ################
# If we want to check whether or not the crsp time series is stationary.
# The augmented DickeyFuller and PhillipsPerron tests suggest that the crsp
# series is stationary (since the #hypothesis of a unit root is rejected).
# Both the augmented DickeyFuller and PhillipsPerron tests,
# which were implemented in R with the functions
# adf.test() and pp.test(),have small/large? P-value; see the output below.
# The functions adf.test(), pp.test() (used below) are in Rs tseries package.
#Therefore, at level 0.05 the null hypothesis of a unit root is rejected
# by both tests in favor of the alternative of stationarity,
#the default alternative hypothesis for both adf.test() and pp.test().
library(tseries)
adf.test(crsp)
pp.test(crsp)
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
