data <- timetk::taylor_30_minLab 6 - Time Series Methods
SOLUTIONS
Introduction
In today’s lab, you’ll practice building workflows with recipes, parsnip models, rsample cross validations, and model comparison in the context of timeseries data.
Packages
The Data
Today we will be using electricity demand data, based on a paper by James W Taylor:
Taylor, J.W. (2003) Short-term electricity demand forecasting using double seasonal exponential smoothing. Journal of the Operational Research Society, 54, 799-805.
The data can be found in the timetk package as timetk::taylor_30_min, a tibble with demensions: 4,032 x 2
date: A date-time variable in 30-minute incrementsvalue: Electricity demand in Megawatts
Exercise 1: EDA
Plot the data using the functions timetk::plot_time_series, timetk::plot_acf_diagnostics (using 100 lags), and timetk::plot_seasonal_diagnostics.
Exercise 2: Time scaling
The raw data has 30 minutes intervals between data points. Downscale the data to 60 minute intervals, using timetk::summarise_by_time, revising the electricity demand (value) variable by adding the two 30-minute intervals in each 60-minute interval. Assign the downscaled data to the variable taylor_60_min.
Exercise 3: Training and test datasets
- Split the new (60 min) time series into training and test sets using
timetk::time_series_split- set the training period (‘initial’) to ‘2 months’ and the assessment period to ‘1 weeks’
- Prepare the data resample specification with
timetk::tk_time_series_cv_plan()and plot it withtimetk::plot_time_series_cv_plan - Separate the training and test data sets using
rsample.
Exercise 4: recipes
Create a base recipe (base_rec) using the formula
value ~ dateand the training data. This will be used for non-regression modelsCreate a recipe (lm_rec) using the formula
value ~ .and thetrainingdata. This will be used for regression models. For this recipe:- add time series signature features using
timetk::step_timeseries_signaturewith the appropriate argument, - add a step to select the columns
value,date_index.num,date_month.lbl,date_wday.lbl,date_hour, - add a normalization step targeting
date_index.num, - add a step to mutate
date_hour, changing it to a factor, - add a step to one-hot encode nominal predictors.
- add time series signature features using
Exercise 5 models
Now we will create a several models to estimate electricity demand, as follows
- Create a model specification for an exponential smoothing model using engine ‘ets’
- Create a model specification for an arima model using engine ‘auto_arima’
- Create a model specification for a linear model using engine ‘glmnet’ and penalty = 0.02, mixture = 0.5
Exercise 6 model fitting
Create a workflow for each model using workflows::workflow.
- Add a recipe to the workflow
- the linear model uses the
lm_recrecipe created above - the
etsandarimamodels use thebase_recrecipe created above
- the linear model uses the
- Add a model to each workflow
- Fit with the training data
Exercise 7: calibrate
In this exercise we’ll use the testing data with our fitted models.
- Create a table with the fitted workflows using
modeltime::modeltime_table - Using the table you just created, run a calibration on the test data with the function
modeltime::modeltime_calibrate. - Compare the accuracy of the models using the
modeltime::modeltime_accuracy()on the results of the calibration
Which is the best model by the rmse metric?
Exercise 8: forecast - training data
Use the calibration table with modeltime::modeltime_forecast to graphically compare the fits to the testing data with the observed values.
Exercise 9: forecast - future
Now refit the models using the full data set (using the calibration table and modeltime::modeltime_refit). Save the result in the variable refit_tbl.
- Use the refit data in the variable refit_tbl, along with
modeltime::modeltime_forecastand argumenth= ‘2 weeks’ (remember to also set theactual_dataargument). This will use the models to forecast electricity demand two weeks into the future. - Plot the forecast with
modeltime::plot_modeltime_forecast.
Exercise 10: Seasonal Time Series Modeling for Retail Forecasting
You are working as a Senior Business Analyst for “TechMart,” a consumer electronics retailer with 50 stores across North America. The company is planning its inventory strategy for 2025 and needs accurate forecasts of monthly sales. The CFO has specifically requested that you use advanced decomposition methods that can handle the complex seasonal patterns in electronics retail.
You have been provided with 60 months of historical sales data (Jan 2019 - Dec 2023) showing strong monthly seasonality (holiday shopping peaks) and an underlying growth trend due to market expansion.
#
# retail data
retail_dat <- readr::read_csv('data/TechMart_sales.csv',show_col_types = FALSE)
# Display data summary
cat("TechMart Monthly Sales Data Summary:\n")TechMart Monthly Sales Data Summary:
cat("====================================\n")====================================
cat("Period:", format(min(retail_dat$date), "%b %Y"), "to",
format(max(retail_dat$date), "%b %Y"), "\n")Period: Jan 2019 to Dec 2023
cat("Observations:", nrow(retail_dat), "\n")Observations: 60
print(summary(retail_dat$monthly_sales)) Min. 1st Qu. Median Mean 3rd Qu. Max.
2.979 4.364 5.716 5.843 6.830 10.585
Using the modeltime and parsnip frameworks,
build and compare ETS(A,A,A) and ETS(M,MD,M) models. Use log(sales) as the outcome. Use
workflowsandmodeltime::exp_smoothing, setting the error, trend, season, and damped argument explicitly for each model (see the help formodeltime::exp_smoothing). For ETS(M,M,M), set damping = “damped” (NOTE: the default is ‘damped’).forecast sales one month ahead, and extract the forecast for January 2024. Plot the forecasts.
Grading
Total points available: 30 points.
| Component | Points |
|---|---|
| Ex 1 - 10 | 30 |