# Install and Load Required Libraries
library(readxl)
## Warning: package 'readxl' was built under R version 4.4.2
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.2
library(scales)
library(forecast)
## Warning: package 'forecast' was built under R version 4.4.2
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(lubridate)
## Warning: package 'lubridate' was built under R version 4.4.2
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
# Load Dataset (Adjust File Path as Needed)
data <- read_excel("C:/Users/asus/Downloads/bananas - values.xlsx")

# Convert 'LOADING WEEK' to Date Format
data$Year <- as.integer(substr(data$`LOADING WEEK`, 1, 4))
data$Week <- as.integer(substr(data$`LOADING WEEK`, 5, 6))
data$Date <- as.Date(paste(data$Year, data$Week, 1, sep = "-"), format = "%Y-%U-%u")

# Aggregate Weekly Data
data_grouped <- data %>%
  group_by(Date) %>%
  summarise(TOTAL_REQ = sum(TOTAL_REQ, na.rm = TRUE),
            TOTAL_EXP = sum(TOTAL_EXP, na.rm = TRUE))

# Extract Quarters for Grouping
data_grouped$Quarter <- paste0("Q", quarter(data_grouped$Date))
data_grouped$Quarter_Label <- paste0(year(data_grouped$Date), "-", data_grouped$Quarter)
data_grouped$Week_Num <- week(data_grouped$Date) 

# Plot Time Series with Dual X-Axis Labels (Weeks & Quarters)
ggplot(data_grouped, aes(x = Date, y = TOTAL_EXP)) +
  geom_line(color = "blue", size = 1) +
  scale_x_date(
    breaks = pretty_breaks(n = 15), 
    labels = function(x) format(x, "%W") 
  ) +
  facet_grid(~Quarter_Label, scales = "free_x", space = "free") +  
  ggtitle("Banana Export Trends (Weeks & Quarters)") +
  xlab("Week Number (Grouped by Quarters)") +
  ylab("Total Exports") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 10))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

# Time Series Forecasting


# Create Time Series Object (Start Year is Dynamic)
start_year <- min(year(data_grouped$Date))
start_week <- min(week(data_grouped$Date))
ts_data <- ts(data_grouped$TOTAL_EXP, start = c(start_year, start_week), frequency = 52)  

# ARIMA Model
fit_arima <- auto.arima(ts_data)
summary(fit_arima)
## Series: ts_data 
## ARIMA(2,0,0) with non-zero mean 
## 
## Coefficients:
##          ar1     ar2       mean
##       0.3165  0.3239  955921.39
## s.e.  0.1391  0.1397   17749.05
## 
## sigma^2 = 2.302e+09:  log likelihood = -584.16
## AIC=1176.33   AICc=1177.26   BIC=1183.81
## 
## Training set error measures:
##                     ME     RMSE      MAE        MPE     MAPE MASE        ACF1
## Training set -465.7354 46454.88 35293.56 -0.2898999 3.723201  NaN -0.02975814
# Forecast for Next 13 Weeks
forecast_arima <- forecast(fit_arima, h = 13)

# Add Quarter_Label to Forecast Data
forecast_arima_df <- data.frame(
  Date = seq.Date(from = max(data_grouped$Date) + 7, by = "week", length.out = 13),
  Forecast = forecast_arima$mean
)
forecast_arima_df$Quarter_Label <- paste0(year(forecast_arima_df$Date), "-Q", quarter(forecast_arima_df$Date))

# Plot ARIMA Forecast
ggplot() +
  geom_line(data = data_grouped, aes(x = Date, y = TOTAL_EXP), color = "blue", size = 1) +
  geom_line(data = forecast_arima_df, aes(x = Date, y = Forecast), color = "red", size = 1) +
  ggtitle("ARIMA Forecast for Banana Exports (Next 13 Weeks)") +
  xlab("Time (Weeks)") +
  ylab("Exports") +
  scale_y_continuous(labels = label_comma()) +
  theme_minimal() +
  facet_wrap(~Quarter_Label, scales = "free_x")

# Exponential Smoothing (ETS) Model
fit_ets <- ets(ts_data)
summary(fit_ets)
## ETS(M,N,N) 
## 
## Call:
## ets(y = ts_data)
## 
##   Smoothing parameters:
##     alpha = 0.4642 
## 
##   Initial states:
##     l = 973777.6674 
## 
##   sigma:  0.051
## 
##      AIC     AICc      BIC 
## 1226.604 1227.149 1232.218 
## 
## Training set error measures:
##                     ME     RMSE      MAE        MPE     MAPE MASE        ACF1
## Training set -3749.968 48101.97 37775.87 -0.5999448 3.991511  NaN -0.09451448
# Forecast Using ETS
forecast_ets <- forecast(fit_ets, h = 13)

# Add Quarter_Label to Forecast Data
forecast_ets_df <- data.frame(
  Date = seq.Date(from = max(data_grouped$Date) + 7, by = "week", length.out = 13),
  Forecast = forecast_ets$mean
)
forecast_ets_df$Quarter_Label <- paste0(year(forecast_ets_df$Date), "-Q", quarter(forecast_ets_df$Date))

# Plot ETS Forecast
ggplot() +
  geom_line(data = data_grouped, aes(x = Date, y = TOTAL_EXP), color = "blue", size = 1) +
  geom_line(data = forecast_ets_df, aes(x = Date, y = Forecast), color = "red", size = 1) +
  ggtitle("ETS Forecast for Banana Exports (Next 13 Weeks)") +
  xlab("Time (Weeks)") +
  ylab("Exports") +
  scale_y_continuous(labels = label_comma()) +
  theme_minimal() +
  facet_wrap(~Quarter_Label, scales = "free_x")