This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(ggplot2)
library(tidyr)
library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
setwd('/Users/yeshimonipede/Desktop/BC_Spring2024')
USD_TRY<-read.csv('USD_TRY Historical Data.csv')


dates <- as.Date(USD_TRY[, 1])  # Convert date column to Date class
exchange_rates <- USD_TRY[, 2]  # Extract exchange rate column

#Converting to a time series
USD_TRY_ts<-ts(exchange_rates, start = c(1990,2), frequency = 12)
USD_TRY_ts <- ts(rev(USD_TRY_ts), start = start(USD_TRY_ts), frequency = frequency(USD_TRY_ts))

Splitting the time series data into train and test data. The first 80% of the data will be for training.

# Last 5 years of data 
start_train <- c(1990, 2) # Start of training data
end_train <- c(2021,12)   # End of training data

start_val <- c(2022, 1)   # Start of validation data
end_val <- c(2022, 12)     # End of validation data

start_test <- c(2023, 1)  # Start of test data
end_test <- c(2024, 2)    # End of test data

# Create training, validation, and test sets
USD_TRY_ts_train <- window(USD_TRY_ts, start = start_train, end = end_train)
USD_TRY_ts_val <- window(USD_TRY_ts, start = start_val, end = end_val)
USD_TRY_ts_test <- window(USD_TRY_ts, start = start_test, end = end_test)
USD_TRY_ts_testval<-window(USD_TRY_ts, start = start_val, end = end_test)
# Build a neural network model
neural_net_model <- nnetar(USD_TRY_ts_train)

# Forecast for the validation set
forecast_neural_net <- forecast(neural_net_model, h = length(USD_TRY_ts_testval))
autoplot(USD_TRY_ts_testval) +
  autolayer(forecast_neural_net, series = "Neural Net") +
  xlab("Date") + ylab("Exchange Rate") +
  ggtitle("Forecast vs Actual Exchange Rate (Validation Set)") +
  theme_minimal()

autoplot(USD_TRY_ts) +
  autolayer(forecast_neural_net, series = "Neural Net") +
  xlab("Date") + ylab("Exchange Rate") +
  ggtitle("Forecast vs Actual Exchange Rate") +
  theme_minimal()

accuracy_metrics <- accuracy(forecast_neural_net, USD_TRY_ts_testval)
print(accuracy_metrics)
##                         ME       RMSE        MAE       MPE     MAPE       MASE
## Training set -0.0004424833  0.1125469 0.05883891 -26.38628 29.18319  0.1698994
## Test set      4.7992508569 10.3250345 8.64872502  16.59770 39.95657 24.9734945
##                    ACF1 Theil's U
## Training set -0.0655668        NA
## Test set     -0.1661163  7.185952

The model uses 6 lags which means it uses the values of the series at 6 previous time points as predictors for the current value.

The model performed better on the training set compared to the test set but the performance on the test set definitely was not too poor. The negative mean errors suggests that generally, the forecasts tended to be lower than the actual values.The MASE measures tests the relative accuracy of the forecast compared to the naive forecast. The training model performed better than a naive forecast while the test set did not as demonstrated by its MASE of 9. The Theil’s U forecast is high, also suggesting that it performs worse than a naive model.