Explanation 1
The live starter code is self-contained and uses only runner-compatible functions.
Loading
Lesson 10 of 22
Represent ordered data correctly and separate levels, changes, trend, seasonality, and remainder.
Motivation
Time adds order and dependence. Treating monthly observations as an unordered sample discards information needed for forecasting and uncertainty.
Why this matters
Represent ordered data correctly and separate levels, changes, trend, seasonality, and remainder.
Packages and data
Base stats; tsibble, fable, and feasts are optional modern extensions.
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
Observations per seasonal cycle; metadata, not proof of complete dates.
12 for monthly data
A rising price level can coexist with falling inflation.
State horizon and denominator
Future information entering a feature used at an earlier forecast origin.
Centred smoothers can leak
Notation and formulas
yt = trend_t + seasonal_t + remainder_t
STL components depend on smoothing choices and are not uniquely observed causal quantities.
Worked example
Scenario
Can a regular monthly series be decomposed into trend, seasonality, and remainder?
R check
set.seed(1010)
t <- 1:60
x <- ts(100 + 0.3*t + 2*sin(2*pi*t/12) + rnorm(60,0,0.5), frequency=12, start=c(2021,1))
fit <- stl(x, s.window='periodic', robust=TRUE)
print(frequency(x))
print(sd(fit$time.series[,'remainder']))
plot(fit)The periodic seasonal pattern repeats by month while trend rises; remainder captures what this decomposition leaves. Limitation: Full-sample decomposition is descriptive and uses future values near earlier dates; forecast evaluation must refit on training data.
Visual
A simulated monthly series combines a rising trend and repeating seasonal movement.
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 time-series objects, trend, and decomposition.
Ready to run
Common mistake
Do not infer regular monthly spacing from row count alone.
STATLAB Tip
Plot both the level and a substantively meaningful change.
Guided practice
Scenario
Aggregate monthly values to quarters using both averages and final-month levels; explain the different questions.
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
Compute first and lag-12 differences.
Explanation 2
Compare annual average with December level.
Explanation 3
Change the STL seasonal window.
Selected solutions
These are compact solution routes. Confirm dimensions, units, and any changed modelling choices.
Explanation 1
diff(x); diff(x,lag=12)
Explanation 2
aggregate(x,nfrequency=1,FUN=mean)
Explanation 3
Refit and compare endpoint components; they are smoothing-dependent.
Chapter summary
Represent ordered data correctly and separate levels, changes, trend, seasonality, and remainder.
Explanation 1
Validate the time index.
Explanation 2
Levels and changes answer different questions.
Explanation 3
STL is model-dependent.
Explanation 4
Prevent future information leakage.
Terminology
regular series
Use in Time-Series Objects, Trend, and Decomposition.
seasonal frequency
Use in Time-Series Objects, Trend, and Decomposition.
change
Use in Time-Series Objects, Trend, and Decomposition.
decomposition
Use in Time-Series Objects, Trend, and Decomposition.
References and provenance
Safavi (2026), Chapter 10; Statistics Canada (2026). 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%.