6.-Case-Study-1

pdf

School

University of Toronto *

*We aren’t endorsed by this school

Course

374

Subject

Economics

Date

Jan 9, 2024

Type

pdf

Pages

6

Uploaded by SargentSquidPerson1009

Report
Case Study 1 ECO374 Install and load required R packages if (!require( "quantmod" )) install.packages( "quantmod" ) if (!require( "ggplot2" )) install.packages( "ggplot2" ) if (!require( "stats" )) install.packages( "stats" ) if (!require( "tsDyn" )) install.packages( "tsDyn" ) if (!require( "forecast" )) install.packages( "forecast" ) if (!require( "urca" )) install.packages( "urca" ) library(quantmod) # functions: getSymbols library(ggplot2) # functions: ggplot library(stats) # functions: arima library(tsDyn) # functions: SETAR library(forecast) # functions: auto.arima, nnetar library(urca) # functions: ur.kpss 1. Data Data: iShares Core S&P Total U.S. Stock Market ETF ( Source ) ITOT <- getSymbols( "ITOT" , src= "yahoo" , return.class= "xts" , auto.assign= F) ITOT <- window(ITOT, start= as.Date( "2022-11-01" ), end= as.Date( "2023-09-01" )) ITOT.c <- ITOT$ITOT.Close # extract close price seed <- 2345 Plot data ggplot(ITOT.c, aes( x= index(ITOT.c), y= ITOT.Close)) + geom_line( color= "springgreen4" ) + labs( x= "" , y= "" , title= "Core S&P Total U.S. Stock Market, Close" ) + theme_minimal() + theme( plot.title = element_text( size= 10 )) + scale_x_date( date_breaks= "1 months" , date_labels = "%Y-%m" ) 85 90 95 100 2022-11 2022-12 2023-01 2023-02 2023-03 2023-04 2023-05 2023-06 2023-07 2023-08 2023-09 Core S&P Total U.S. Stock Market, Close 1
Difference data and plot D_ITOT.c <- na.omit(diff(ITOT.c, lag= 1 , differences= 1 )) ggplot(D_ITOT.c, aes( x= index(D_ITOT.c), y= ITOT.Close)) + geom_line( color= "darkblue" ) + labs( x= "" , y= "" , title= "Differenced Core S&P Total U.S. Stock Market, Close" ) + theme_minimal() + theme( plot.title = element_text( size= 10 )) + scale_x_date( date_breaks= "3 months" , date_labels = "%Y-%m" ) -2.5 0.0 2.5 5.0 2023-01 2023-04 2023-07 Differenced Core S&P Total U.S. Stock Market, Close 2. Stationarity (unit root) test ur.test <- ur.kpss(ITOT.c) summary(ur.test) ## ## ####################### ## # KPSS Unit Root Test # ## ####################### ## ## Test is of type: mu with 4 lags. ## ## Value of test-statistic is: 3.4622 ## ## Critical value for a significance level of: ## 10pct 5pct 2.5pct 1pct ## critical values 0.347 0.463 0.574 0.739 We reject at 5% level the null hypothesis that the data is stationary. Let’s test the differenced data. ur.test.d <- ur.kpss(D_ITOT.c) summary(ur.test.d) ## ## ####################### ## # KPSS Unit Root Test # ## ####################### ## 2
## Test is of type: mu with 4 lags. ## ## Value of test-statistic is: 0.05 ## ## Critical value for a significance level of: ## 10pct 5pct 2.5pct 1pct ## critical values 0.347 0.463 0.574 0.739 We do not reject at 5% the null of stationarity and conclude that the differenced data is stationary. 3. ACF and PACF Plot ACF of differenced data par( mar= c( 4 , 4 , 0.5 , 0 )) # set margin sizes ACF <- acf(D_ITOT.c, lag.max= 20 , plot= FALSE, demean= TRUE) plot(ACF[ 1 : 20 ], main= "" , cex.lab= 0.75 , cex.axis= 0.75 , xaxt= "n" ) axis( 1 , at= ACF$lag, cex.axis= 0.75 ) # put a label at each lag value -0.10 0.00 0.05 0.10 Lag ACF 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Plot PACF of differenced data par( mar= c( 4 , 4 , 0.5 , 0 )) # set margin sizes PACF <- pacf(D_ITOT.c, lag.max= 20 , plot= FALSE, demean= TRUE) plot(PACF[ 1 : 20 ], main= "" , cex.lab= 0.75 , cex.axis= 0.75 , xaxt= "n" ) axis( 1 , at= PACF$lag, cex.axis= 0.75 ) # put a label at each lag value 3
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
-0.15 -0.05 0.05 Lag Partial ACF 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 4. Model Selection Best-fitting ARMA model on the full data set auto.arima(ITOT.c) ## Series: ITOT.c ## ARIMA(3,1,1) ## ## Coefficients: ## ar1 ar2 ar3 ma1 ## 0.9245 -0.1008 0.0900 -0.9403 ## s.e. 0.1163 0.0944 0.0735 0.0926 ## ## sigma^2 = 0.8584: log likelihood = -278.6 ## AIC=567.19 AICc=567.49 BIC=583.91 Best-fitting ARMA model on the differenced full data set auto.arima(D_ITOT.c) ## Series: D_ITOT.c ## ARIMA(3,0,1) with zero mean ## ## Coefficients: ## ar1 ar2 ar3 ma1 ## 0.9245 -0.1008 0.0900 -0.9403 ## s.e. 0.1162 0.0944 0.0735 0.0926 ## ## sigma^2 = 0.8583: log likelihood = -278.6 ## AIC=567.19 AICc=567.49 BIC=583.91 4
Time-series validation MSE for Specified Models data <- ITOT.c D_data <- D_ITOT.c # Loop over different model specifications for (m in 1 : 6 ) { TT <- length(data) T1 <- floor( 0.2 *TT) # start at 20% of the sample size step <- 20 # forecast data horizon for MSE MSE.t <- matrix( 0 , nrow= TT-T1+ 1 , ncol= 1 ) # initialize MAE.t <- MSE.t MAPE.t <- MSE.t tseq <- seq( from= T1, to= TT, by= step) tseq <- tseq[-length(tseq)] for (j in tseq) { # auto.arima model if (m== 1 ) {fcst <- forecast(arima(data[ 1 :j -1 ], order= c( 3 , 1 , 1 )), h= step) yhat <- as.numeric(fcst[[ 4 ]])} # ARMA(2,2) on differenced data (based on ACF and PACF) if (m== 2 ) {fcst <- forecast(arima(data[ 1 :j -1 ], order= c( 2 , 1 , 2 )), h= step) # the fcst$mean forecast is stored in the 4th element of the list fcst yhat <- as.numeric(fcst[[ 4 ]])} # ARMA(2,2) on differenced data with one seasonal AR lag if (m== 3 ) {fcst <- forecast(arima(data[ 1 :j -1 ], order= c( 2 , 1 , 2 ), seasonal= c( 1 , 1 , 0 )), h= step) yhat <- as.numeric(fcst[[ 4 ]])} # SETAR model with a threshold of 0 if (m== 4 ) {fcst <- predict(setar(D_data[ 1 :j -1 ], mL= 1 , mH= 1 , th= 0 ), n.ahead= step) yhat <- as.numeric(fcst) yhat <- cumsum(yhat) + as.numeric(last(data[ 1 :j -1 ]))} # cumulate forecast differences # LSTAR model if (m== 5 ) {fcst <- predict(lstar(D_data[ 1 :j -1 ], m= 1 , d= 1 , mL= 1 , mH= 1 , trace= FALSE), n.ahead= step) yhat <- as.numeric(fcst) yhat <- cumsum(yhat) + as.numeric(last(data[ 1 :j -1 ]))} # cumulate forecast differences # NNAR with parameters selected in file 5. NNAR Application if (m== 6 ) {set.seed(seed) fcst <- forecast(nnetar(data[ 1 :j -1 ], p= 5 , P= 0 , size= 4 ), h= step) # the fcst$mean forecast is stored in the 16th element of the list fcst yhat <- as.numeric(fcst[[ 16 ]][ 1 :step])} js <- j+step -1 MSE.t[(j-T1+ 1 ):(js-T1+ 1 )] <- (as.numeric(data[j:js])-yhat)ˆ 2 MAE.t[(j-T1+ 1 ):(js-T1+ 1 )] <- abs(as.numeric(data[j:js])-yhat) MAPE.t[(j-T1+ 1 ):(js-T1+ 1 )] <- 100 *abs((as.numeric(data[j:js])-yhat)/yhat) } if (m<= 3 ) print(fcst$method) if (m== 4 ) print( "SETAR" ) if (m== 5 ) print( "LSTAR" ) 5
if (m== 6 ) print( "NNAR" ) print(paste( "MSE =" , mean(MSE.t))) print(paste( "MAE =" , mean(MAE.t))) print(paste( "MAPE =" , mean(MAPE.t))) print( " " ) } ## [1] "ARIMA(3,1,1)" ## [1] "MSE = 9.30286307210739" ## [1] "MAE = 2.51961481429868" ## [1] "MAPE = 2.8010027273953" ## [1] " " ## [1] "ARIMA(2,1,2)" ## [1] "MSE = 8.36164709716682" ## [1] "MAE = 2.42263968235052" ## [1] "MAPE = 2.69305751667465" ## [1] " " ## [1] "ARIMA(2,1,2)" ## [1] "MSE = 9.15660851518732" ## [1] "MAE = 2.42659126336873" ## [1] "MAPE = 2.72411098772006" ## [1] " " ## [1] "SETAR" ## [1] "MSE = 8.61494167780873" ## [1] "MAE = 2.3372839122757" ## [1] "MAPE = 2.59116449005166" ## [1] " " ## [1] "LSTAR" ## [1] "MSE = 12.2418165645851" ## [1] "MAE = 2.93647578326473" ## [1] "MAPE = 3.29722689167999" ## [1] " " ## [1] "NNAR" ## [1] "MSE = 6.34241410121144" ## [1] "MAE = 2.0609470484656" ## [1] "MAPE = 2.27308621310925" ## [1] " " The NNAR model has the smallest validation MSE and would therefore be our model of choice. 6
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