Packages Steven P. Sanderson II, MPH - Date: 2025-11-21
This analysis follows a Nested Modeltime Workflow from modeltime
along with using the NNS package. I use this to monitor the
downloads of all of my packages:
glimpse(downloads_tbl)
Rows: 160,769
Columns: 11
$ date <date> 2020-11-23, 2020-11-23, 2020-11-23, 2020-11-23, 2020-11-23,…
$ time <Period> 15H 36M 55S, 11H 26M 39S, 23H 34M 44S, 18H 39M 32S, 9H 0M…
$ date_time <dttm> 2020-11-23 15:36:55, 2020-11-23 11:26:39, 2020-11-23 23:34:…
$ size <int> 4858294, 4858294, 4858301, 4858295, 361, 4863722, 4864794, 4…
$ r_version <chr> NA, "4.0.3", "3.5.3", "3.5.2", NA, NA, NA, NA, NA, NA, NA, N…
$ r_arch <chr> NA, "x86_64", "x86_64", "x86_64", NA, NA, NA, NA, NA, NA, NA…
$ r_os <chr> NA, "mingw32", "mingw32", "linux-gnu", NA, NA, NA, NA, NA, N…
$ package <chr> "healthyR.data", "healthyR.data", "healthyR.data", "healthyR…
$ version <chr> "1.0.0", "1.0.0", "1.0.0", "1.0.0", "1.0.0", "1.0.0", "1.0.0…
$ country <chr> "US", "US", "US", "GB", "US", "US", "DE", "HK", "JP", "US", …
$ ip_id <int> 2069, 2804, 78827, 27595, 90474, 90474, 42435, 74, 7655, 638…
The last day in the data set is 2025-11-19 23:47:25, the file was birthed on: 2025-10-31 10:47:59.603742, and at report knit time is 464.99 hours old. Happy analyzing!
Now that we have our data lets take a look at it using the skimr
package.
skim(downloads_tbl)
| Name | downloads_tbl |
| Number of rows | 160769 |
| Number of columns | 11 |
| _______________________ | |
| Column type frequency: | |
| character | 6 |
| Date | 1 |
| numeric | 2 |
| POSIXct | 1 |
| Timespan | 1 |
| ________________________ | |
| Group variables | None |
Data summary
Variable type: character
| skim_variable | n_missing | complete_rate | min | max | empty | n_unique | whitespace |
|---|---|---|---|---|---|---|---|
| r_version | 117891 | 0.27 | 5 | 7 | 0 | 50 | 0 |
| r_arch | 117891 | 0.27 | 1 | 7 | 0 | 6 | 0 |
| r_os | 117891 | 0.27 | 7 | 19 | 0 | 24 | 0 |
| package | 0 | 1.00 | 7 | 13 | 0 | 8 | 0 |
| version | 0 | 1.00 | 5 | 17 | 0 | 62 | 0 |
| country | 15083 | 0.91 | 2 | 2 | 0 | 165 | 0 |
Variable type: Date
| skim_variable | n_missing | complete_rate | min | max | median | n_unique |
|---|---|---|---|---|---|---|
| date | 0 | 1 | 2020-11-23 | 2025-11-19 | 2023-10-26 | 1816 |
Variable type: numeric
| skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|
| size | 0 | 1 | 1124849.3 | 1490144.95 | 355 | 27068 | 310086 | 2355045 | 5677952 | ▇▁▂▁▁ |
| ip_id | 0 | 1 | 11331.8 | 21991.25 | 1 | 230 | 2903 | 11961 | 299146 | ▇▁▁▁▁ |
Variable type: POSIXct
| skim_variable | n_missing | complete_rate | min | max | median | n_unique |
|---|---|---|---|---|---|---|
| date_time | 0 | 1 | 2020-11-23 09:00:41 | 2025-11-19 23:47:25 | 2023-10-26 17:35:12 | 101241 |
Variable type: Timespan
| skim_variable | n_missing | complete_rate | min | max | median | n_unique |
|---|---|---|---|---|---|---|
| time | 0 | 1 | 0 | 59 | 12H 6M 25S | 60 |
We can see that the following columns are missing a lot of data and for
us are most likely not useful anyways, so we will drop them
c(r_version, r_arch, r_os)
Now lets take a look at a time-series plot of the total daily downloads by package. We will use a log scale and place a vertical line at each version release for each package.


[[1]]

[[2]]

[[3]]

[[4]]

[[5]]

[[6]]

[[7]]

[[8]]

Now lets take a look at some time series decomposition graphs.
[[1]]

[[2]]

[[3]]

[[4]]

[[5]]

[[6]]

[[7]]

[[8]]

[[1]]

[[2]]

[[3]]

[[4]]

[[5]]

[[6]]

[[7]]

[[8]]

Seasonal Diagnostics:
[[1]]

[[2]]

[[3]]

[[4]]

[[5]]

[[6]]

[[7]]

[[8]]

ACF and PACF Diagnostics:
[[1]]

[[2]]

[[3]]

[[4]]

[[5]]

[[6]]

[[7]]

[[8]]

Now that we have our basic data and a shot of what it looks like, let’s
add some features to our data which can be very helpful in modeling.
Lets start by making a tibble that is aggregated by the day and
package, as we are going to be interested in forecasting the next 4
weeks or 28 days for each package. First lets get our base data.
Call:
stats::lm(formula = .formula, data = df)
Residuals:
Min 1Q Median 3Q Max
-145.79 -36.49 -11.37 27.24 819.39
Coefficients:
Estimate Std. Error
(Intercept) -1.929e+02 6.142e+01
date 1.174e-02 3.257e-03
lag(value, 1) 1.109e-01 2.332e-02
lag(value, 7) 8.800e-02 2.406e-02
lag(value, 14) 7.913e-02 2.404e-02
lag(value, 21) 7.865e-02 2.413e-02
lag(value, 28) 6.509e-02 2.405e-02
lag(value, 35) 5.778e-02 2.410e-02
lag(value, 42) 6.005e-02 2.423e-02
lag(value, 49) 6.143e-02 2.417e-02
month(date, label = TRUE).L -9.934e+00 5.073e+00
month(date, label = TRUE).Q 1.076e+00 5.007e+00
month(date, label = TRUE).C -1.553e+01 5.024e+00
month(date, label = TRUE)^4 -6.258e+00 4.997e+00
month(date, label = TRUE)^5 -7.083e+00 4.955e+00
month(date, label = TRUE)^6 8.149e-01 4.969e+00
month(date, label = TRUE)^7 -4.894e+00 4.878e+00
month(date, label = TRUE)^8 -4.255e+00 4.839e+00
month(date, label = TRUE)^9 2.594e+00 4.840e+00
month(date, label = TRUE)^10 8.580e-01 4.855e+00
month(date, label = TRUE)^11 -4.076e+00 4.841e+00
fourier_vec(date, type = "sin", K = 1, period = 7) -1.155e+01 2.231e+00
fourier_vec(date, type = "cos", K = 1, period = 7) 7.455e+00 2.312e+00
t value Pr(>|t|)
(Intercept) -3.141 0.001714 **
date 3.605 0.000321 ***
lag(value, 1) 4.753 2.17e-06 ***
lag(value, 7) 3.658 0.000262 ***
lag(value, 14) 3.292 0.001016 **
lag(value, 21) 3.259 0.001140 **
lag(value, 28) 2.707 0.006863 **
lag(value, 35) 2.397 0.016616 *
lag(value, 42) 2.478 0.013300 *
lag(value, 49) 2.541 0.011128 *
month(date, label = TRUE).L -1.958 0.050382 .
month(date, label = TRUE).Q 0.215 0.829847
month(date, label = TRUE).C -3.091 0.002030 **
month(date, label = TRUE)^4 -1.252 0.210573
month(date, label = TRUE)^5 -1.429 0.153074
month(date, label = TRUE)^6 0.164 0.869752
month(date, label = TRUE)^7 -1.003 0.315835
month(date, label = TRUE)^8 -0.879 0.379374
month(date, label = TRUE)^9 0.536 0.592126
month(date, label = TRUE)^10 0.177 0.859740
month(date, label = TRUE)^11 -0.842 0.399939
fourier_vec(date, type = "sin", K = 1, period = 7) -5.176 2.53e-07 ***
fourier_vec(date, type = "cos", K = 1, period = 7) 3.224 0.001286 **
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 59.38 on 1744 degrees of freedom
(49 observations deleted due to missingness)
Multiple R-squared: 0.2319, Adjusted R-squared: 0.2222
F-statistic: 23.93 on 22 and 1744 DF, p-value: < 2.2e-16

This is something I have been wanting to try for a while. The NNS
package is a great package for forecasting time series data.
library(NNS)
data_list <- base_data |>
select(package, value) |>
group_split(package)
data_list |>
imap(
\(x, idx) {
obj <- x
x <- obj |> pull(value) |> tail(7*52)
train_set_size <- length(x) - 56
pkg <- obj |> pluck(1) |> unique()
# sf <- NNS.seas(x, modulo = 7, plot = FALSE)$periods
seas <- t(
sapply(
1:25,
function(i) c(
i,
sqrt(
mean((
NNS.ARMA(x,
h = 28,
training.set = train_set_size,
method = "lin",
seasonal.factor = i,
plot=FALSE
) - tail(x, 28)) ^ 2)))
)
)
colnames(seas) <- c("Period", "RMSE")
sf <- seas[which.min(seas[, 2]), 1]
cat(paste0("Package: ", pkg, "\n"))
NNS.ARMA.optim(
variable = x,
h = 28,
training.set = train_set_size,
#seasonal.factor = seq(12, 60, 7),
seasonal.factor = sf,
pred.int = 0.95,
plot = TRUE
)
title(
sub = paste0("\n",
"Package: ", pkg, " - NNS Optimization")
)
}
)
Package: healthyR
[1] "CURRNET METHOD: lin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'lin' , seasonal.factor = c( 25 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 12.899078009383"
[1] "BEST method = 'lin' PATH MEMBER = c( 25 )"
[1] "BEST lin OBJECTIVE FUNCTION = 12.899078009383"
[1] "CURRNET METHOD: nonlin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'nonlin' , seasonal.factor = c( 25 ) ...)"
[1] "CURRENT nonlin OBJECTIVE FUNCTION = 6.45585734302479"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 25 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 6.45585734302479"
[1] "CURRNET METHOD: both"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'both' , seasonal.factor = c( 25 ) ...)"
[1] "CURRENT both OBJECTIVE FUNCTION = 7.95562249885686"
[1] "BEST method = 'both' PATH MEMBER = c( 25 )"
[1] "BEST both OBJECTIVE FUNCTION = 7.95562249885686"

Package: healthyR.ai
[1] "CURRNET METHOD: lin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'lin' , seasonal.factor = c( 1 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 141.746467648729"
[1] "BEST method = 'lin' PATH MEMBER = c( 1 )"
[1] "BEST lin OBJECTIVE FUNCTION = 141.746467648729"
[1] "CURRNET METHOD: nonlin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'nonlin' , seasonal.factor = c( 1 ) ...)"
[1] "CURRENT nonlin OBJECTIVE FUNCTION = 24.3196811228587"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 1 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 24.3196811228587"
[1] "CURRNET METHOD: both"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'both' , seasonal.factor = c( 1 ) ...)"
[1] "CURRENT both OBJECTIVE FUNCTION = 35.6612645641263"
[1] "BEST method = 'both' PATH MEMBER = c( 1 )"
[1] "BEST both OBJECTIVE FUNCTION = 35.6612645641263"

Package: healthyR.data
[1] "CURRNET METHOD: lin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'lin' , seasonal.factor = c( 1 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 328625.058437315"
[1] "BEST method = 'lin' PATH MEMBER = c( 1 )"
[1] "BEST lin OBJECTIVE FUNCTION = 328625.058437315"
[1] "CURRNET METHOD: nonlin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'nonlin' , seasonal.factor = c( 1 ) ...)"
[1] "CURRENT nonlin OBJECTIVE FUNCTION = 133.196089455299"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 1 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 133.196089455299"
[1] "CURRNET METHOD: both"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'both' , seasonal.factor = c( 1 ) ...)"
[1] "CURRENT both OBJECTIVE FUNCTION = 261.390827882138"
[1] "BEST method = 'both' PATH MEMBER = c( 1 )"
[1] "BEST both OBJECTIVE FUNCTION = 261.390827882138"

Package: healthyR.ts
[1] "CURRNET METHOD: lin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'lin' , seasonal.factor = c( 11 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 20.8047182923878"
[1] "BEST method = 'lin' PATH MEMBER = c( 11 )"
[1] "BEST lin OBJECTIVE FUNCTION = 20.8047182923878"
[1] "CURRNET METHOD: nonlin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'nonlin' , seasonal.factor = c( 11 ) ...)"
[1] "CURRENT nonlin OBJECTIVE FUNCTION = 11.5896207002355"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 11 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 11.5896207002355"
[1] "CURRNET METHOD: both"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'both' , seasonal.factor = c( 11 ) ...)"
[1] "CURRENT both OBJECTIVE FUNCTION = 12.7948758905471"
[1] "BEST method = 'both' PATH MEMBER = c( 11 )"
[1] "BEST both OBJECTIVE FUNCTION = 12.7948758905471"

Package: healthyverse
[1] "CURRNET METHOD: lin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'lin' , seasonal.factor = c( 25 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 4.43481767653326"
[1] "BEST method = 'lin' PATH MEMBER = c( 25 )"
[1] "BEST lin OBJECTIVE FUNCTION = 4.43481767653326"
[1] "CURRNET METHOD: nonlin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'nonlin' , seasonal.factor = c( 25 ) ...)"
[1] "CURRENT nonlin OBJECTIVE FUNCTION = 4.43241897436715"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 25 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 4.43241897436715"
[1] "CURRNET METHOD: both"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'both' , seasonal.factor = c( 25 ) ...)"
[1] "CURRENT both OBJECTIVE FUNCTION = 3.85130988197239"
[1] "BEST method = 'both' PATH MEMBER = c( 25 )"
[1] "BEST both OBJECTIVE FUNCTION = 3.85130988197239"

Package: RandomWalker
[1] "CURRNET METHOD: lin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'lin' , seasonal.factor = c( 11 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 8.24365562745494"
[1] "BEST method = 'lin' PATH MEMBER = c( 11 )"
[1] "BEST lin OBJECTIVE FUNCTION = 8.24365562745494"
[1] "CURRNET METHOD: nonlin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'nonlin' , seasonal.factor = c( 11 ) ...)"
[1] "CURRENT nonlin OBJECTIVE FUNCTION = 8.64059884419028"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 11 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 8.64059884419028"
[1] "CURRNET METHOD: both"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'both' , seasonal.factor = c( 11 ) ...)"
[1] "CURRENT both OBJECTIVE FUNCTION = 8.53612504361394"
[1] "BEST method = 'both' PATH MEMBER = c( 11 )"
[1] "BEST both OBJECTIVE FUNCTION = 8.53612504361394"

Package: tidyAML
[1] "CURRNET METHOD: lin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'lin' , seasonal.factor = c( 10 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 56.3361330782767"
[1] "BEST method = 'lin' PATH MEMBER = c( 10 )"
[1] "BEST lin OBJECTIVE FUNCTION = 56.3361330782767"
[1] "CURRNET METHOD: nonlin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'nonlin' , seasonal.factor = c( 10 ) ...)"
[1] "CURRENT nonlin OBJECTIVE FUNCTION = 11.4632125506106"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 10 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 11.4632125506106"
[1] "CURRNET METHOD: both"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'both' , seasonal.factor = c( 10 ) ...)"
[1] "CURRENT both OBJECTIVE FUNCTION = 16.6795462408972"
[1] "BEST method = 'both' PATH MEMBER = c( 10 )"
[1] "BEST both OBJECTIVE FUNCTION = 16.6795462408972"

Package: TidyDensity
[1] "CURRNET METHOD: lin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'lin' , seasonal.factor = c( 1 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 477.982835797126"
[1] "BEST method = 'lin' PATH MEMBER = c( 1 )"
[1] "BEST lin OBJECTIVE FUNCTION = 477.982835797126"
[1] "CURRNET METHOD: nonlin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'nonlin' , seasonal.factor = c( 1 ) ...)"
[1] "CURRENT nonlin OBJECTIVE FUNCTION = 169.410793992195"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 1 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 169.410793992195"
[1] "CURRNET METHOD: both"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method = 'both' , seasonal.factor = c( 1 ) ...)"
[1] "CURRENT both OBJECTIVE FUNCTION = 251.011394537871"
[1] "BEST method = 'both' PATH MEMBER = c( 1 )"
[1] "BEST both OBJECTIVE FUNCTION = 251.011394537871"

[[1]]
NULL
[[2]]
NULL
[[3]]
NULL
[[4]]
NULL
[[5]]
NULL
[[6]]
NULL
[[7]]
NULL
[[8]]
NULL
Now we are going to do some basic pre-processing.
data_padded_tbl <- base_data %>%
pad_by_time(
.date_var = date,
.pad_value = 0
)
# Get log interval and standardization parameters
log_params <- liv(data_padded_tbl$value, limit_lower = 0, offset = 1, silent = TRUE)
limit_lower <- log_params$limit_lower
limit_upper <- log_params$limit_upper
offset <- log_params$offset
data_liv_tbl <- data_padded_tbl %>%
# Get log interval transform
mutate(value_trans = liv(value, limit_lower = 0, offset = 1, silent = TRUE)$log_scaled)
# Get Standardization Params
std_params <- standard_vec(data_liv_tbl$value_trans, silent = TRUE)
std_mean <- std_params$mean
std_sd <- std_params$sd
data_transformed_tbl <- data_liv_tbl %>%
group_by(package) %>%
# get standardization
mutate(value_trans = standard_vec(value_trans, silent = TRUE)$standard_scaled) %>%
tk_augment_fourier(
.date_var = date,
.periods = c(7, 14, 30, 90, 180),
.K = 2
) %>%
tk_augment_timeseries_signature(
.date_var = date
) %>%
ungroup() %>%
select(-c(value, -year.iso))
Since this is panel data we can follow one of two different modeling strategies. We can search for a global model in the panel data or we can use nested forecasting finding the best model for each of the time series. Since we only have 5 panels, we will use nested forecasting.
To do this we will use the nest_timeseries and
split_nested_timeseries functions to create a nested tibble.
horizon <- 4*7
nested_data_tbl <- data_transformed_tbl %>%
# 0. Filter out column where package is NA
filter(!is.na(package)) %>%
# 1. Extending: We'll predict n days into the future.
extend_timeseries(
.id_var = package,
.date_var = date,
.length_future = horizon
) %>%
# 2. Nesting: We'll group by id, and create a future dataset
# that forecasts n days of extended data and
# an actual dataset that contains n*2 days
nest_timeseries(
.id_var = package,
.length_future = horizon
#.length_actual = horizon*2
) %>%
# 3. Splitting: We'll take the actual data and create splits
# for accuracy and confidence interval estimation of n das (test)
# and the rest is training data
split_nested_timeseries(
.length_test = horizon
)
nested_data_tbl
# A tibble: 8 × 4
package .actual_data .future_data .splits
<fct> <list> <list> <list>
1 healthyR.data <tibble [1,808 × 50]> <tibble [28 × 50]> <split [1780|28]>
2 healthyR <tibble [1,799 × 50]> <tibble [28 × 50]> <split [1771|28]>
3 healthyR.ts <tibble [1,744 × 50]> <tibble [28 × 50]> <split [1716|28]>
4 healthyverse <tibble [1,715 × 50]> <tibble [28 × 50]> <split [1687|28]>
5 healthyR.ai <tibble [1,541 × 50]> <tibble [28 × 50]> <split [1513|28]>
6 TidyDensity <tibble [1,392 × 50]> <tibble [28 × 50]> <split [1364|28]>
7 tidyAML <tibble [999 × 50]> <tibble [28 × 50]> <split [971|28]>
8 RandomWalker <tibble [422 × 50]> <tibble [28 × 50]> <split [394|28]>
Now it is time to make some recipes and models using the modeltime workflow.
recipe_base <- recipe(
value_trans ~ .
, data = extract_nested_test_split(nested_data_tbl)
)
recipe_base
recipe_date <- recipe(
value_trans ~ date
, data = extract_nested_test_split(nested_data_tbl)
)
# Models ------------------------------------------------------------------
# Auto ARIMA --------------------------------------------------------------
model_spec_arima_no_boost <- arima_reg() %>%
set_engine(engine = "auto_arima")
wflw_auto_arima <- workflow() %>%
add_recipe(recipe = recipe_date) %>%
add_model(model_spec_arima_no_boost)
# NNETAR ------------------------------------------------------------------
model_spec_nnetar <- nnetar_reg(
mode = "regression"
, seasonal_period = "auto"
) %>%
set_engine("nnetar")
wflw_nnetar <- workflow() %>%
add_recipe(recipe = recipe_base) %>%
add_model(model_spec_nnetar)
# TSLM --------------------------------------------------------------------
model_spec_lm <- linear_reg() %>%
set_engine("lm")
wflw_lm <- workflow() %>%
add_recipe(recipe = recipe_base) %>%
add_model(model_spec_lm)
# MARS --------------------------------------------------------------------
model_spec_mars <- mars(mode = "regression") %>%
set_engine("earth")
wflw_mars <- workflow() %>%
add_recipe(recipe = recipe_date) %>%
add_model(model_spec_mars)
nested_modeltime_tbl <- modeltime_nested_fit(
# Nested Data
nested_data = nested_data_tbl,
control = control_nested_fit(
verbose = TRUE,
allow_par = FALSE
),
# Add workflows
wflw_auto_arima,
wflw_lm,
wflw_mars,
wflw_nnetar
)
nested_modeltime_tbl <- nested_modeltime_tbl[!is.na(nested_modeltime_tbl$package),]
nested_modeltime_tbl %>%
extract_nested_test_accuracy() %>%
filter(!is.na(package)) %>%
knitr::kable()
| package | .model_id | .model_desc | .type | mae | mape | mase | smape | rmse | rsq |
|---|---|---|---|---|---|---|---|---|---|
| healthyR.data | 1 | ARIMA | Test | 0.7670347 | 108.96610 | 0.6960527 | 163.1357 | 0.9383914 | 0.0165474 |
| healthyR.data | 2 | LM | Test | 0.8064269 | 170.58255 | 0.7317995 | 144.9154 | 0.9145595 | 0.0021029 |
| healthyR.data | 3 | NULL | NA | NA | NA | NA | NA | NA | NA |
| healthyR.data | 4 | NNAR | Test | 0.8789529 | 243.36872 | 0.7976138 | 154.4925 | 0.9682275 | 0.0035503 |
| healthyR | 1 | ARIMA | Test | 0.7589691 | 93.41715 | 0.7191014 | 163.3105 | 1.0526849 | 0.0131450 |
| healthyR | 2 | LM | Test | 0.8031627 | 342.51019 | 0.7609735 | 142.0607 | 0.9717747 | 0.0540753 |
| healthyR | 3 | NULL | NA | NA | NA | NA | NA | NA | NA |
| healthyR | 4 | NNAR | Test | 0.8141327 | 277.68656 | 0.7713674 | 147.9877 | 1.0245663 | 0.0270289 |
| healthyR.ts | 1 | ARIMA | Test | 0.6998091 | 114.45790 | 0.5866782 | 149.5112 | 0.9665861 | 0.1182587 |
| healthyR.ts | 2 | LM | Test | 0.9087308 | 359.86240 | 0.7618256 | 166.4695 | 1.1163386 | 0.0004680 |
| healthyR.ts | 3 | NULL | NA | NA | NA | NA | NA | NA | NA |
| healthyR.ts | 4 | NNAR | Test | 0.9735180 | 437.44835 | 0.8161394 | 166.3911 | 1.1521169 | 0.0018316 |
| healthyverse | 1 | ARIMA | Test | 0.6192494 | 96.42625 | 0.8719198 | 143.9649 | 0.8341094 | 0.0397532 |
| healthyverse | 2 | LM | Test | 0.8284835 | 215.12307 | 1.1665269 | 158.8880 | 0.9420605 | 0.0033741 |
| healthyverse | 3 | NULL | NA | NA | NA | NA | NA | NA | NA |
| healthyverse | 4 | NNAR | Test | 0.8167735 | 219.60586 | 1.1500390 | 151.6104 | 0.9525531 | 0.0014112 |
| healthyR.ai | 1 | ARIMA | Test | 0.6396387 | 99.98062 | 0.8418496 | 173.3020 | 0.9044671 | 0.0008725 |
| healthyR.ai | 2 | LM | Test | 0.7733788 | 252.82399 | 1.0178693 | 157.3297 | 0.9335255 | 0.0703493 |
| healthyR.ai | 3 | NULL | NA | NA | NA | NA | NA | NA | NA |
| healthyR.ai | 4 | NNAR | Test | 0.7494834 | 241.50250 | 0.9864198 | 146.1212 | 0.8973307 | 0.0894583 |
| TidyDensity | 1 | ARIMA | Test | 0.9477972 | 338.26245 | 0.6129667 | 134.1729 | 1.0875021 | 0.1448263 |
| TidyDensity | 2 | LM | Test | 0.8797928 | 157.53015 | 0.5689864 | 148.4995 | 1.0769541 | 0.0044833 |
| TidyDensity | 3 | NULL | NA | NA | NA | NA | NA | NA | NA |
| TidyDensity | 4 | NNAR | Test | 0.9679495 | 211.24352 | 0.6259998 | 160.9762 | 1.1378034 | 0.0180565 |
| tidyAML | 1 | ARIMA | Test | 0.7934918 | 120.44666 | 0.8815158 | 175.4149 | 0.9896338 | 0.0325714 |
| tidyAML | 2 | LM | Test | 0.7485934 | 140.44823 | 0.8316368 | 135.9207 | 0.9419760 | 0.0234691 |
| tidyAML | 3 | NULL | NA | NA | NA | NA | NA | NA | NA |
| tidyAML | 4 | NNAR | Test | 0.6553459 | 113.31429 | 0.7280451 | 129.0581 | 0.8563175 | 0.1412180 |
| RandomWalker | 1 | ARIMA | Test | 0.7072695 | 117.87896 | 0.5812580 | 153.0881 | 0.8016743 | 0.2113436 |
| RandomWalker | 2 | LM | Test | 0.8573588 | 182.79460 | 0.7046065 | 155.1721 | 0.9259880 | 0.0009522 |
| RandomWalker | 3 | NULL | NA | NA | NA | NA | NA | NA | NA |
| RandomWalker | 4 | NNAR | Test | 1.0027393 | 232.94176 | 0.8240852 | 158.3198 | 1.0965402 | 0.0456052 |
nested_modeltime_tbl %>%
extract_nested_test_forecast() %>%
group_by(package) %>%
filter_by_time(.date_var = .index, .start_date = max(.index) - 60) %>%
ungroup() %>%
plot_modeltime_forecast(
.interactive = FALSE,
.conf_interval_show = FALSE,
.facet_scales = "free"
) +
theme_minimal() +
facet_wrap(~ package, nrow = 3) +
theme(legend.position = "bottom")

best_nested_modeltime_tbl <- nested_modeltime_tbl %>%
modeltime_nested_select_best(
metric = "rmse",
minimize = TRUE,
filter_test_forecasts = TRUE
)
best_nested_modeltime_tbl %>%
extract_nested_best_model_report()
# Nested Modeltime Table
# A tibble: 8 × 10
package .model_id .model_desc .type mae mape mase smape rmse rsq
<fct> <int> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 healthyR.da… 2 LM Test 0.806 171. 0.732 145. 0.915 0.00210
2 healthyR 2 LM Test 0.803 343. 0.761 142. 0.972 0.0541
3 healthyR.ts 1 ARIMA Test 0.700 114. 0.587 150. 0.967 0.118
4 healthyverse 1 ARIMA Test 0.619 96.4 0.872 144. 0.834 0.0398
5 healthyR.ai 4 NNAR Test 0.749 242. 0.986 146. 0.897 0.0895
6 TidyDensity 2 LM Test 0.880 158. 0.569 148. 1.08 0.00448
7 tidyAML 4 NNAR Test 0.655 113. 0.728 129. 0.856 0.141
8 RandomWalker 1 ARIMA Test 0.707 118. 0.581 153. 0.802 0.211
best_nested_modeltime_tbl %>%
extract_nested_test_forecast() %>%
#filter(!is.na(.model_id)) %>%
group_by(package) %>%
filter_by_time(.date_var = .index, .start_date = max(.index) - 60) %>%
ungroup() %>%
plot_modeltime_forecast(
.interactive = FALSE,
.conf_interval_alpha = 0.2,
.facet_scales = "free"
) +
facet_wrap(~ package, nrow = 3) +
theme_minimal() +
theme(legend.position = "bottom")

Now that we have the best models, we can make our future forecasts.
nested_modeltime_refit_tbl <- best_nested_modeltime_tbl %>%
modeltime_nested_refit(
control = control_nested_refit(verbose = TRUE)
)
nested_modeltime_refit_tbl
# Nested Modeltime Table
# A tibble: 8 × 5
package .actual_data .future_data .splits .modeltime_tables
<fct> <list> <list> <list> <list>
1 healthyR.data <tibble> <tibble> <split [1780|28]> <mdl_tm_t [1 × 5]>
2 healthyR <tibble> <tibble> <split [1771|28]> <mdl_tm_t [1 × 5]>
3 healthyR.ts <tibble> <tibble> <split [1716|28]> <mdl_tm_t [1 × 5]>
4 healthyverse <tibble> <tibble> <split [1687|28]> <mdl_tm_t [1 × 5]>
5 healthyR.ai <tibble> <tibble> <split [1513|28]> <mdl_tm_t [1 × 5]>
6 TidyDensity <tibble> <tibble> <split [1364|28]> <mdl_tm_t [1 × 5]>
7 tidyAML <tibble> <tibble> <split [971|28]> <mdl_tm_t [1 × 5]>
8 RandomWalker <tibble> <tibble> <split [394|28]> <mdl_tm_t [1 × 5]>
nested_modeltime_refit_tbl %>%
extract_nested_future_forecast() %>%
group_by(package) %>%
mutate(across(.value:.conf_hi, .fns = ~ standard_inv_vec(
x = .,
mean = std_mean,
sd = std_sd
)$standard_inverse_value)) %>%
mutate(across(.value:.conf_hi, .fns = ~ liiv(
x = .,
limit_lower = limit_lower,
limit_upper = limit_upper,
offset = offset
)$rescaled_v)) %>%
filter_by_time(.date_var = .index, .start_date = max(.index) - 60) %>%
ungroup() %>%
plot_modeltime_forecast(
.interactive = FALSE,
.conf_interval_alpha = 0.2,
.facet_scales = "free"
) +
facet_wrap(~ package, nrow = 3) +
theme_minimal() +
theme(legend.position = "bottom")
