This notebook replicates the calendar variation analysis of the search volume index for the keyword “sapi” with the lunarcast package. The six classical structures are fitted with three calendar variation designs (a holiday-month dummy, lead and lag dummies, and proportional windows), which gives 18 models, and they are compared with the automatic lunarcast model.
lunarcast is installed from GitHub. The chunk installs only what is missing, so it can be run every time.
if (!requireNamespace("lunarcast", quietly = TRUE)) {
if (!requireNamespace("remotes", quietly = TRUE)) install.packages("remotes")
remotes::install_github("novrisuhermi/lunarcast")
}
for (p in c("tidyverse", "gridExtra"))
if (!requireNamespace(p, quietly = TRUE)) install.packages(p)
library(lunarcast)
library(tidyverse)
library(gridExtra)
The data set sapi in lunarcast is the monthly Google
Trends index for the keyword “sapi” (cattle) in Indonesia. The dates of
Eid al-Adha also come from the package: they are the official dates of
the Indonesian Ministry of Religious Affairs, including the two
occurrences of 2006 (10 January and 31 December).
sapi
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2004 20 16 23 12 16 15 14 16 12 17 10 18
2005 14 12 14 23 17 11 9 10 13 15 18 19
2006 15 14 15 14 20 15 13 9 14 15 17 14
2007 12 13 16 16 17 15 14 14 18 18 18 22
2008 19 20 21 20 19 18 15 15 18 19 20 26
2009 21 20 23 20 20 18 17 18 19 21 30 23
2010 20 22 21 19 19 18 16 19 18 20 32 19
2011 15 15 15 14 14 17 15 17 17 20 35 19
2012 17 17 17 16 16 15 18 19 20 40 23 17
2013 17 19 17 16 19 17 21 19 21 52 21 17
2014 18 17 17 18 18 17 22 20 27 55 22 20
2015 22 20 21 20 20 22 25 27 73 32 24 23
2016 24 23 22 22 22 27 25 24 78 26 22 20
2017 22 22 21 21 24 29 22 34 74 25 23 22
2018 22 23 22 21 27 29 24 83 34 27 28 26
2019 27 26 25 24 34 35 30 97 33 29 27 25
2020 27 27 30 40 53 38 58 79 38 36 36 39
2021 32 33 36 41 44 34 100 38 34 30 30 28
2022 27 31 32 37 32 36 86 30 30 27 28 26
2023 27 28 32 36 30 61 52 25 24 23 23 23
2024 21 20 26 25 26 61 26 22 21 21 22 22
2025 21 24 26 23 27 55 20 19 20 20 20
adha <- holiday_dates("idul_adha", years = 2004:2026)
adha
[1] "2004-02-01" "2005-01-21" "2006-01-10" "2006-12-31" "2007-12-20" "2008-12-08" "2009-11-27" "2010-11-17" "2011-11-06"
[10] "2012-10-26" "2013-10-15" "2014-10-05" "2015-09-24" "2016-09-12" "2017-09-01" "2018-08-22" "2019-08-11" "2020-07-31"
[19] "2021-07-20" "2022-07-10" "2023-06-29" "2024-06-17" "2025-06-06" "2026-05-27"
df <- tibble(
ts = seq(as.Date("2004-01-01"), by = "month", length.out = length(sapi)),
t = seq_along(sapi),
idul_adha = as.integer(holiday_window(adha, c(0, 0), sapi)[, 1] > 0),
year = as.integer(format(ts, "%Y")),
month = as.integer(format(ts, "%m")),
search = as.numeric(sapi)
)
df
df %>% filter(idul_adha == 1)
ggplot(data = df) +
geom_line(aes(x = ts, y = search)) +
ggtitle("Search Volume Index for keyword sapi") +
xlab("") + ylab("") +
scale_x_date(date_breaks = "2 year", date_labels = "%Y")
The models are fitted to 2004-2024 and tested on 2025.
train <- df %>% filter(year != 2025)
test <- df %>% filter(year == 2025)
y_train <- window(sapi, end = c(2024, 12))
y_test <- window(sapi, start = c(2025, 1))
h <- length(y_test)
Every classical model has a trend, a seasonal component, a calendar variation component C_t and an ARIMA error. The calendar variation component has three designs:
calvar_classic() builds the regressors, fits the
regression with ARIMA errors with auto.arima() and keeps
everything needed for forecasting. With Fourier terms the number of
harmonics K is chosen by AICc. The
helper below fits one structure with the three designs.
fit_designs <- function(trend, season) {
list(month = calvar_classic(y_train, adha, trend, season, design = "month"),
leadlag = calvar_classic(y_train, adha, trend, season, design = "leadlag"),
window = calvar_classic(y_train, adha, trend, season, design = "window"))
}
Y_t = \underbrace{\beta_0 + \beta_1 t}_{\text{deterministic trend}} + \underbrace{\sum_{i=1}^{s-1} \delta_i S_{i,t}}_{\text{deterministic seasonal with dummy variable}} + \underbrace{C_t}_{\text{calendar variation effect}} + \underbrace{\frac{\theta_q(B)}{\phi_p(B)} a_t}_{\text{ARIMA error}}
m1 <- fit_designs("linear", "dummies")
m1
$month
Classical calendar variation model: linear trend + seasonal dummies
Holiday design: holiday-month dummy
Errors: ARIMA(3,0,2) (regression with ARIMA errors), AICc = 1783.9
Holiday effects (same every year, original scale):
$leadlag
Classical calendar variation model: linear trend + seasonal dummies
Holiday design: lead and lag dummies (period before, of and after the holiday)
Errors: ARIMA(5,0,2) (regression with ARIMA errors), AICc = 1755
Holiday effects (same every year, original scale):
$window
Classical calendar variation model: linear trend + seasonal dummies
Holiday design: proportional windows: before [-14, -1] days, holiday [0, +3] days
Errors: ARIMA(4,0,3) (regression with ARIMA errors), AICc = 1711.8
Holiday effects (same every year, original scale):
AICc of the candidate windows:
NA
Y_t = \underbrace{\beta_0 + \beta_1 t}_{\text{deterministic trend}} + \underbrace{\sum_{k=1}^{K} \left(a_k \sin\left(\frac{2\pi k t}{s}\right) + b_k \cos\left(\frac{2\pi k t}{s}\right)\right)}_{\text{deterministic seasonal with Fourier terms}} + \underbrace{C_t}_{\text{calendar variation}} + \underbrace{\frac{\theta_q(B)}{\phi_p(B)} a_t}_{\text{ARIMA error}}
m2 <- fit_designs("linear", "fourier")
m2
$month
Classical calendar variation model: linear trend + Fourier terms (1 harmonic)
Holiday design: holiday-month dummy
Errors: ARIMA(3,0,2) (regression with ARIMA errors), AICc = 1760.3
Holiday effects (same every year, original scale):
$leadlag
Classical calendar variation model: linear trend + Fourier terms (2 harmonics)
Holiday design: lead and lag dummies (period before, of and after the holiday)
Errors: ARIMA(4,0,2) (regression with ARIMA errors), AICc = 1741.7
Holiday effects (same every year, original scale):
$window
Classical calendar variation model: linear trend + Fourier terms (2 harmonics)
Holiday design: proportional windows: before [-7, -1] days, holiday [0, +3] days
Errors: ARIMA(4,0,3) (regression with ARIMA errors), AICc = 1696.5
Holiday effects (same every year, original scale):
AICc of the candidate windows:
NA
Y_t = \beta_0 + \underbrace{\sum_{i=1}^{s-1} \delta_i S_{i,t}}_{\text{deterministic seasonal with dummy}} + \underbrace{C_t}_{\text{calendar variation effect}} + \underbrace{\frac{\theta_q(B)}{\phi_p(B)(1-B)^d} a_t}_{\text{ARIMA error}}
m3 <- fit_designs("stochastic", "dummies")
m3
$month
Classical calendar variation model: stochastic trend + seasonal dummies
Holiday design: holiday-month dummy
Errors: ARIMA(2,1,2) (regression with ARIMA errors), AICc = 1774.4
Holiday effects (same every year, original scale):
$leadlag
Classical calendar variation model: stochastic trend + seasonal dummies
Holiday design: lead and lag dummies (period before, of and after the holiday)
Errors: ARIMA(5,1,2) (regression with ARIMA errors), AICc = 1734.8
Holiday effects (same every year, original scale):
$window
Classical calendar variation model: stochastic trend + seasonal dummies
Holiday design: proportional windows: before [-14, -1] days, holiday [0, +3] days
Errors: ARIMA(3,1,3) (regression with ARIMA errors), AICc = 1701.6
Holiday effects (same every year, original scale):
AICc of the candidate windows:
NA
Y_t = \beta_0 + \underbrace{\sum_{k=1}^{K} \left(a_k \sin\left(\frac{2\pi k t}{s}\right) + b_k \cos\left(\frac{2\pi k t}{s}\right)\right)}_{\text{deterministic seasonal with Fourier terms}} + \underbrace{C_t}_{\text{calendar variation}} + \underbrace{\frac{\theta_q(B)}{\phi_p(B)(1-B)^d} a_t}_{\text{ARIMA error}}
m4 <- fit_designs("stochastic", "fourier")
m4
$month
Classical calendar variation model: stochastic trend + Fourier terms (1 harmonic)
Holiday design: holiday-month dummy
Errors: ARIMA(2,1,2) (regression with ARIMA errors), AICc = 1754.3
Holiday effects (same every year, original scale):
$leadlag
Classical calendar variation model: stochastic trend + Fourier terms (1 harmonic)
Holiday design: lead and lag dummies (period before, of and after the holiday)
Errors: ARIMA(5,1,2) (regression with ARIMA errors), AICc = 1727.1
Holiday effects (same every year, original scale):
$window
Classical calendar variation model: stochastic trend + Fourier terms (2 harmonics)
Holiday design: proportional windows: before [-14, -1] days, holiday [0, +3] days
Errors: ARIMA(3,1,3) (regression with ARIMA errors), AICc = 1686.7
Holiday effects (same every year, original scale):
AICc of the candidate windows:
NA
Y_t = \underbrace{\beta_0 + \beta_1 t}_{\text{deterministic trend}} + \underbrace{C_t}_{\text{calendar variation effect}} + \underbrace{\frac{\theta_q(B)\Theta_Q(B^s)}{\phi_p(B)\Phi_P(B^s)(1-B^s)^D} a_t}_{\text{Seasonal ARIMA}}
m5 <- fit_designs("linear", "sarima")
m5
$month
Classical calendar variation model: linear trend + seasonal ARIMA
Holiday design: holiday-month dummy
Errors: ARIMA(1,0,2)(2,0,0)[12] (regression with ARIMA errors), AICc = 1655.8
Holiday effects (same every year, original scale):
$leadlag
Classical calendar variation model: linear trend + seasonal ARIMA
Holiday design: lead and lag dummies (period before, of and after the holiday)
Errors: ARIMA(1,0,2)(2,0,0)[12] (regression with ARIMA errors), AICc = 1649.4
Holiday effects (same every year, original scale):
$window
Classical calendar variation model: linear trend + seasonal ARIMA
Holiday design: proportional windows: before [-21, -1] days, holiday [0, +3] days
Errors: ARIMA(3,0,0)(2,0,0)[12] (regression with ARIMA errors), AICc = 1598.4
Holiday effects (same every year, original scale):
AICc of the candidate windows:
NA
Y_t = \beta_0 + \underbrace{C_t}_{\text{calendar variation effect}} + \underbrace{\frac{\theta_q(B)\Theta_Q(B^s)}{\phi_p(B)\Phi_P(B^s)(1-B)^d(1-B^s)^D} a_t}_{\text{Seasonal ARIMA}}
m6 <- fit_designs("stochastic", "sarima")
m6
$month
Classical calendar variation model: stochastic trend + seasonal ARIMA
Holiday design: holiday-month dummy
Errors: ARIMA(1,1,1)(1,0,2)[12] (regression with ARIMA errors), AICc = 1647.5
Holiday effects (same every year, original scale):
$leadlag
Classical calendar variation model: stochastic trend + seasonal ARIMA
Holiday design: lead and lag dummies (period before, of and after the holiday)
Errors: ARIMA(2,1,2)(0,0,2)[12] (regression with ARIMA errors), AICc = 1640.9
Holiday effects (same every year, original scale):
$window
Classical calendar variation model: stochastic trend + seasonal ARIMA
Holiday design: proportional windows: before [-14, -1] days, holiday [0, +3] days
Errors: ARIMA(2,1,2)(2,0,0)[12] (regression with ARIMA errors), AICc = 1593.2
Holiday effects (same every year, original scale):
AICc of the candidate windows:
NA
The automatic model is fitted on the log scale, z_t = \log Y_t:
z_t = \underbrace{\sum_{h} \sum_{j} \log\left\{1 + \left(e^{\alpha_{hj}} - 1\right) w_{hj,t}\right\}}_{\text{holiday effects with window shares}} + \underbrace{\mathbf{x}_t^\top \boldsymbol\beta}_{\text{trend and seasonality}} + \underbrace{N_t}_{\text{ARIMA error}}
where w_{hj,t} is the share of a window around occurrence j of holiday h that falls in month t. Nothing is fixed by hand: lunarcast tries the moving holidays of Indonesia and keeps those with an effect, chooses the window of each holiday and whether its effect is the same every year or different for every occurrence (in which case the next effect is forecast from the past ones), then chooses the trend and the seasonality by cross-validation. Fitting takes about a minute.
m7 <- lunarcast(y_train, country = "Indonesia")
m7
lunarcast: automatic calendar variation model
Monthly data, 252 observations from 2004-01 to 2024-12
Candidates: the moving holidays of Indonesia
Calendar effects found automatically (kept when they lower the AICc by 10 or more):
idul_fitri constant window [-28, 0] days; effect x1.17 (+17%); AICc gain 20.8
idul_adha two-level window [-3, +7] days; effects x0.66 to x3.74, next x2.54; AICc gain 316.9
not found: chinese_new_year (AICc gain 1.1), easter (AICc gain -0.8)
Trend and seasonality: stochastic trend + no seasonality (chosen by cross-validation, one-standard-error rule)
Errors: ARIMA(5,1,1) (regression with ARIMA errors), AICc = -264.3
Cross-validated RMSE of the candidate structures:
Effect of every occurrence of idul_adha; the next effect is forecast by the simple exponential smoothing of the effects:
The summary lists the holidays that were found, with their windows, the size of their effects and how much they improve the AICc, followed by the holidays that were not found. Unlike the classical models, which use Eid al-Adha only, the automatic model can keep more than one holiday.
autoplot(m7)
structures <- c(m1 = "Deterministic Trend & Seasonality (Dummy)",
m2 = "Deterministic Trend & Seasonality (Fourier)",
m3 = "Stochastic Trend & Deterministic Seasonality (Dummy)",
m4 = "Stochastic Trend & Deterministic Seasonality (Fourier)",
m5 = "Deterministic Trend & Stochastic Seasonality",
m6 = "Stochastic Trend & Seasonality")
designs <- c(month = "Holiday-month dummy", leadlag = "Lead and lag dummies",
window = "Proportional windows")
colours <- c(m1 = "red", m2 = "blue", m3 = "brown", m4 = "purple", m5 = "green4",
m6 = "orange")
classic <- list(m1 = m1, m2 = m2, m3 = m3, m4 = m4, m5 = m5, m6 = m6)
my_theme <- theme(
legend.position = c(0.2, 0.75),
legend.text = element_text(size = 5),
legend.title = element_text(size = 10),
legend.key.size = unit(0.3, "lines"),
legend.box.margin = margin(0, 0, 0, 0),
legend.background = element_rect(fill = "white", colour = "grey80"),
plot.title = element_text(size = 8, face = "bold")
)
plot_fit <- function(data, column, title, colour, breaks = "3 year", labels = "%Y") {
ggplot(data = data) +
geom_line(aes(x = ts, y = actual, color = "Actual")) +
geom_line(aes(x = ts, y = .data[[column]], color = "Predicted")) +
scale_color_manual(name = NULL,
values = c("Actual" = "grey", "Predicted" = colour)) +
ggtitle(title) +
xlab("") + ylab("") +
scale_x_date(date_breaks = breaks, date_labels = labels) +
my_theme
}
for (d in names(designs)) {
plots <- lapply(names(classic), function(m)
plot_fit(predict_train, paste0(m, "_", d, "_predict"),
structures[[m]], colours[[m]]))
grid.arrange(grobs = plots, ncol = 2, top = designs[[d]])
}
for (d in names(designs)) {
plots <- lapply(names(classic), function(m)
plot_fit(predict_test, paste0(m, "_", d, "_predict"),
structures[[m]], colours[[m]],
breaks = "2 month", labels = "%b"))
grid.arrange(grobs = plots, ncol = 2, top = designs[[d]])
}
plot_fit(predict_train, "m7_predict", "Automatic Model (lunarcast)", "black")
forecast() builds the future regressors (trend,
seasonality and the calendar variation component) from the holiday
dates, so no future design matrix has to be prepared by hand.
predict_test <- test %>% mutate(actual = search)
for (m in names(classic)) for (d in names(designs))
predict_test[[paste0(m, "_", d, "_predict")]] <-
as.numeric(forecast(classic[[m]][[d]], h = h)$mean)
predict_test$m7_predict <- as.numeric(forecast(m7, h = h)$mean)
predict_test <- predict_test %>% select(ts, actual, ends_with("_predict"))
predict_test
for (d in names(designs)) {
plots <- lapply(names(classic), function(m)
plot_fit(predict_test, paste0(m, "_", d, "_predict"),
paste0(structures[[m]], ", ", designs[[d]]), colours[[m]],
breaks = "2 month", labels = "%b"))
grid.arrange(grobs = plots, ncol = 2)
}
plot_fit(predict_test, "m7_predict", "Automatic Model (lunarcast)", "black",
breaks = "2 month", labels = "%b")
forecast_accuracy() gives the RMSE, MAE, MAPE and sMAPE
(the last two in percent) of every model.
evaluate <- function(predictions) {
pred <- predictions %>% select(ends_with("_predict"))
do.call(forecast_accuracy, c(list(actual = predictions$actual), as.list(pred))) %>%
as_tibble() %>%
mutate(code = sub("_predict$", "", model),
Model = ifelse(code == "m7", "Automatic Model (lunarcast)",
structures[sub("_.*", "", code)]),
`Calendar variation` = ifelse(code == "m7", "found automatically",
designs[sub("^m[1-6]_", "", code)])) %>%
select(Model, `Calendar variation`, RMSE, MAPE, sMAPE) %>%
arrange(RMSE)
}
evaluate(predict_train)
evaluate(predict_test)