Read in the data and convert it to a Tsibble
UST <- read.csv("C:/Users/bz3832zi/Downloads/USTRADE.csv")
UST <- UST %>%
mutate(observation_date = yearmonth(observation_date)) %>%
as_tsibble(index = observation_date)
glimpse(UST)
## Rows: 1,032
## Columns: 2
## $ observation_date <mth> 1939 Jan, 1939 Feb, 1939 Mar, 1939 Apr, 1939 May, 193…
## $ USTRADE <dbl> 3107.8, 3121.8, 3139.7, 3154.8, 3151.7, 3152.9, 3165.…
Get a test set
train <- UST %>% filter(observation_date <= yearmonth("2021-12"))
test <- UST %>% filter(observation_date > yearmonth("2021-12"))
Fit the models
models <- train %>%
model(
NAIVE = NAIVE(USTRADE),
SNAIVE = SNAIVE(USTRADE),
RW_Drift = RW(USTRADE ~ drift())
)
Forecasting each model
forecasts <- models %>%
forecast(h = "24 months") # 2022-01 to 2023-12
Compare the forecasts
accuracy(forecasts, test)
## # A tibble: 3 × 10
## .model .type ME RMSE MAE MPE MAPE MASE RMSSE ACF1
## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 NAIVE Test 169. 179. 169. 1.09 1.09 NaN NaN 0.538
## 2 RW_Drift Test 15.3 56.9 41.8 0.0983 0.269 NaN NaN 0.621
## 3 SNAIVE Test 282. 294. 282. 1.81 1.81 NaN NaN 0.619
Final forecast for 2024 will be using the RW_Drift model because it
has the lowest RMSE, and was overall the best model.
UST_ts <- UST %>%
as_tsibble(index = observation_date)
final_model <- UST %>%
model(RW_Drift = RW(USTRADE ~ drift()))
final_forecast <- final_model %>%
forecast(h = "12 months")
Graph the forecast with historical data and zoom the window in to
show the forecast up close.
# Subset data for forecast period (January 2022 to December 2024)
UST_subset <- UST %>%
filter(observation_date >= yearmonth("2022-01") & observation_date <= yearmonth("2024-12"))
# Create the final forecast
final_forecast <- final_model %>%
forecast(h = "12 months")
# Plot the full range forecast
final_forecast %>%
autoplot(UST) +
ggtitle("Full Forecast for U.S. Retail Trade Employment") +
theme_minimal() +
ylab("USTRADE")

# Plot the zoomed-in forecast (subsetted data)
final_forecast %>%
autoplot(UST_subset) +
ggtitle("2024 Forecast for U.S. Retail Trade Employment (Zoomed In)") +
theme_minimal() +
ylab("USTRADE")
