Explanation 1
The live starter code is self-contained and uses only runner-compatible functions.
Loading
Lesson 13 of 22
Combine calendar structure and predictors with time-series errors while respecting future availability.
Motivation
Ordinary regression standard errors fail when errors remain autocorrelated; dynamic regression models the mean and serial dependence together.
Why this matters
Combine calendar structure and predictors with time-series errors while respecting future availability.
Packages and data
Base stats; forecast/fable optional.
Explanation 1
The live starter code is self-contained and uses only runner-compatible functions.
Explanation 2
Book-only outputs are labelled when the separate source dataset or advanced package was not supplied.
Explanation 3
R code, paths, function names, and formulas remain left-to-right in every locale.
Terminology
Current value minus the same seasonal position one cycle earlier.
yt - y(t-12)
Smooth sine/cosine seasonal basis.
Parsimonious calendar pattern
Pulse, step, or ramp encoding a known event.
Not automatically causal
Notation and formulas
yt = beta0 + beta' xt + nt, where nt follows ARIMA
Predictor coefficients describe the conditional mean while the error model handles remaining dependence.
Worked example
Scenario
How can monthly seasonality be encoded without twelve separate parameters?
R check
t <- 1:60; s <- 12
fourier <- cbind(sin1=sin(2*pi*t/s), cos1=cos(2*pi*t/s), sin2=sin(4*pi*t/s), cos2=cos(4*pi*t/s))
y <- 2 + .03*t + 1.2*fourier[,1] + .6*fourier[,2]
fit <- lm(y ~ t + fourier)
print(coef(fit))
print(Box.test(residuals(fit),lag=12,type='Ljung-Box'))Calendar terms are known in advance, so they can support forecasts without requiring an external predictor scenario. Limitation: Policy and exchange-rate futures are not known; using realized future values would be leakage.
Visual
Illustrative monthly index repeats a smooth annual pattern.
R connection
Run the self-contained starter code in the protected STATLAB R runner. The code prints an auditable result and avoids network or unrestricted file access.
Live R Lab
Run a self-contained example, verify its output, and explain one limitation for seasonality and dynamic regression.
Ready to run
Common mistake
A lag establishes timing, not exogeneity or causation.
STATLAB Tip
Draw a timeline of when every predictor becomes available relative to the forecast origin.
Guided practice
Scenario
Fit monthly indicator effects and compare residual ACF with the Fourier model.
Compare your result with the definition, units, and model assumptions—not only with a target number.
Exercises
Complete these without looking at the selected solutions. More than one defensible program may exist.
Explanation 1
Create December pulse and post-2020 step variables.
Explanation 2
Generate two harmonic pairs.
Explanation 3
Define two future predictor scenarios.
Selected solutions
These are compact solution routes. Confirm dimensions, units, and any changed modelling choices.
Explanation 1
december <- as.integer(month==12); post <- as.integer(date>=as.Date('2020-03-01'))
Explanation 2
Use sin(2*pi*k*t/12) and cos(...) for k=1,2.
Explanation 3
State both the assumed path and how forecast uncertainty depends on it.
Chapter summary
Combine calendar structure and predictors with time-series errors while respecting future availability.
Explanation 1
Seasonality can be deterministic or stochastic.
Explanation 2
Indicators and Fourier terms impose different structures.
Explanation 3
ARIMA errors repair serial-dependence inference.
Explanation 4
Future predictor availability governs forecasting.
Terminology
seasonal change
Use in Seasonality and Dynamic Regression.
indicators
Use in Seasonality and Dynamic Regression.
Fourier terms
Use in Seasonality and Dynamic Regression.
distributed lag
Use in Seasonality and Dynamic Regression.
References and provenance
Safavi (2026), Chapter 13. Student notes: Mohammad Safavi, Ph.D., STATLAB Academy, Version 1.0.
Resource
The authoritative 125-page English PDF accompanies this native lesson.
DownloadReflection
Name the assumption, evidence you would seek, and how the recommendation might change.
Exit check
Reach 70% to complete the chapter. Explanations appear after submission.
Checkpoint
Question 1 of 2. Answered 0/2. Passing score: 70%.