# 1. Load Required Libraries
library(tidyverse)── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.0 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.2 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(fpp2)── Attaching packages ──────────────────────────────────────────── fpp2 2.5.1 ──
✔ forecast 9.0.1 ✔ expsmooth 2.3
✔ fma 2.5
library(forecast)
# 2. Data Simulation (Based on Case Parameters)
# Data spans Jan 1995 to Dec 2009 (180 monthly observations)
set.seed(123)
time_index <- seq(as.Date("1995-01-01"), as.Date("2009-12-01"), by="month")
# Simulate increasing Australian retail sales with seasonal peaks (e.g., Dec)
base_trend <- seq(1200, 3800, length.out=180)
seasonal_effect <- 300 * sin(2 * pi * (1:180)/12)
noise <- rnorm(180, mean=0, sd=50)
sales_values <- base_trend + seasonal_effect + noise
# Create Time Series Object
ts_sales <- ts(sales_values, start=c(1995, 1), frequency=12)
# 3. Trend & Seasonal Decomposition (STL)
# This separates the 'Noise' from the actual 'Trend' and 'Seasonality'
decomp <- stl(ts_sales, s.window="periodic")
autoplot(decomp) +
ggtitle("Decomposition of Australian Household Goods Sales") +
theme_minimal()# 4. Seasonal Analysis
# Helps identify which months Jia Wu should stock up for
ggseasonplot(ts_sales, year.labels=TRUE) +
ylab("Millions of AUD") +
ggtitle("Seasonal Plot: Yearly Comparison")# 5. Forecasting with ETS (Exponential Smoothing)
# This model is ideal for data with clear trends and seasonality
fit_model <- ets(ts_sales)
forecast_2010 <- forecast(fit_model, h=12)
# 6. Final Visualization
autoplot(forecast_2010) +
labs(title="Jia Wu 2010 Sales Forecast: Australian Market",
subtitle="Blue line indicates point forecast; Shaded areas show confidence intervals",
x="Year", y="Sales (Millions AUD)") +
theme_bw()# Output the numerical forecast for the report
print(forecast_2010) Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Jan 2010 3968.033 3904.772 4031.294 3871.283 4064.783
Feb 2010 4077.705 4014.427 4140.984 3980.930 4174.481
Mar 2010 4130.057 4066.762 4193.352 4033.256 4226.858
Apr 2010 4123.691 4060.379 4187.003 4026.864 4220.518
May 2010 4011.577 3948.248 4074.906 3914.723 4108.431
Jun 2010 3887.566 3824.219 3950.912 3790.685 3984.446
Jul 2010 3759.953 3696.589 3823.317 3663.046 3856.860
Aug 2010 3668.094 3604.712 3731.475 3571.160 3765.027
Sep 2010 3621.509 3558.110 3684.909 3524.548 3718.470
Oct 2010 3679.849 3616.432 3743.266 3582.861 3776.837
Nov 2010 3805.054 3741.619 3868.489 3708.038 3902.070
Dec 2010 3945.350 3881.897 4008.804 3848.307 4042.394