Workshop Activities: Week 3
Week 3 Mini-Studio
This page follows the same sequence as week3-code.R. It is designed as a stable seminar companion: read the code, run the matching block in R or RStudio, then answer the short prompts here in words. That keeps the focus on the class code, which is the style you are most likely to meet in the exam.
- Open week3-code.R beside this page.
- Work through the sections in order.
- Run the matching block in RStudio.
- Use the prompts here to practise explanation and model choice, not just syntax.
A. Generate white noise
This section simulates white noise.
set.seed(100)
T=1000
my_data <- tsibble(t = seq(T), y = rnorm(T), index = t)
my_data |> autoplot(y)
my_data |> gg_tsdisplay(y, plot_type = "histogram")- Sets a random seed for reproducibility.
- Generates 1000 random normal values as white noise.
- Plots the time series and displays ACF/PACF with histogram.
- The ACF should show no significant spikes (within blue lines).
- Histogram should be approximately normal.
B. Generate random walk
Simulate a random walk.
set.seed(100)
T=1000
tsibble(t = seq(T), y = cumsum(rnorm(T)), index = t) |>
gg_tsdisplay(y, plot_type = "histogram")- Generates a random walk by cumulative sum of random normals.
- Displays the time series, ACF/PACF, and histogram.
- The series wanders without returning to mean.
- ACF decays slowly, indicating non-stationarity.
C. Plot US GDP
Plot raw GDP.
us_economy <- global_economy |>
filter(Country == "United States")
us_economy |>
autoplot(GDP)- Filters global_economy for US.
- Plots GDP over time.
- Look for increasing variance over time.
D. Plot log GDP
Apply log transformation.
us_economy |>
autoplot(log(GDP))- Plots the natural log of GDP.
- Variance may be more stable, but check if it’s appropriate.
E. Box-Cox transformations
Try Box-Cox with lambda 0 (log) and 0.3.
us_economy |>
autoplot(box_cox(GDP, 0))
us_economy |>
autoplot(box_cox(GDP, 0.3))- Applies Box-Cox transformation with different lambdas.
- Plots the transformed series.
- Lambda 0.3 may stabilize variance better than log.
F. Guerrero method
Use Guerrero to find optimal lambda.
us_economy |>
features(GDP, guerrero) |>
pull(lambda_guerrero)- Computes the Guerrero lambda for Box-Cox.
- This gives the recommended lambda value.
G. STL decomposition for US retail employment
Perform STL decomposition.
us_retail_employment <- us_employment |>
filter(year(Month) >= 1990, Title == "Retail Trade") |>
select(-Series_ID)
dcmp <- us_retail_employment |>
model(stl = STL(Employed))- Filters US employment data for retail trade from 1990.
- Fits STL decomposition.
- STL decomposes into trend, seasonal, remainder.
H. Plot the decomposition
Visualize the components.
dcmp |> components() |> autoplot()- Plots the trend, seasonal, and remainder components.
- Examine each component’s behavior.
I. Fit trend over data
Overlay trend on original data.
us_retail_employment |>
autoplot(Employed) +
autolayer(dcmp |> components(), trend)- Plots original data with trend component overlaid.
- How well the trend captures the overall movement.
J. Fit trend and seasonally adjusted
Plot trend and seasonally adjusted series.
us_retail_employment |>
autoplot(Employed) +
autolayer(dcmp |> components(), trend) +
autolayer(dcmp |> components(), season_adjust)- Adds the seasonally adjusted series to the plot.
- Seasonally adjusted removes seasonal variation.
K. Seasonal shape over time
Plot seasonal component with gg_season.
dcmp |> components() |> gg_season(seasonal)- Plots the seasonal component by season.
- How the seasonal pattern changes over years.
L. Experiment with window arguments
Change window values in STL.
# Example: change trend window
dcmp_alt <- us_retail_employment |>
model(stl = STL(Employed ~ trend(window = 13) + season(window = 7)))
dcmp_alt |> components() |> autoplot()- Fits STL with different window sizes.
- How window changes affect smoothness of components.
M. Plausible seasonally adjusted series
Assess the seasonally adjusted series.
Evaluate if the seasonally adjusted series is plausible. Consider the trend and any remaining patterns.