healthyverse_tsa

Time Series Analysis, Modeling and Forecasting of the Healthyverse

Packages Steven P. Sanderson II, MPH - Date: 2025-10-17

Introduction

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:

Get Data

glimpse(downloads_tbl)
Rows: 157,000
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-10-15 23:22:23, the file was birthed on: 2024-08-07 07:35:44.428716, and at report knit time is 1.042778^{4} 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 157000
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 114833 0.27 5 5 0 48 0
r_arch 114833 0.27 3 7 0 5 0
r_os 114833 0.27 7 15 0 23 0
package 0 1.00 7 13 0 8 0
version 0 1.00 5 17 0 62 0
country 14762 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-10-15 2023-10-02 1781

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
size 0 1 1124750.1 1494610.50 355 16879 308131 2360708 5677952 ▇▁▂▁▁
ip_id 0 1 11328.6 21946.94 1 204 2928 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-10-15 23:22:23 2023-10-02 21:25:16 98643

Variable type: Timespan

skim_variable n_missing complete_rate min max median n_unique
time 0 1 0 59 20.5 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)

Plots

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]]

Feature Engineering

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 
-147.81  -36.24  -10.98   27.04  815.22 

Coefficients:
                                                     Estimate Std. Error
(Intercept)                                        -2.122e+02  6.232e+01
date                                                1.266e-02  3.303e-03
lag(value, 1)                                       1.159e-01  2.346e-02
lag(value, 7)                                       8.967e-02  2.429e-02
lag(value, 14)                                      8.200e-02  2.433e-02
lag(value, 21)                                      6.750e-02  2.440e-02
lag(value, 28)                                      7.696e-02  2.431e-02
lag(value, 35)                                      6.790e-02  2.452e-02
lag(value, 42)                                      5.527e-02  2.467e-02
lag(value, 49)                                      6.815e-02  2.454e-02
month(date, label = TRUE).L                        -8.077e+00  5.109e+00
month(date, label = TRUE).Q                         2.233e+00  5.019e+00
month(date, label = TRUE).C                        -1.634e+01  5.042e+00
month(date, label = TRUE)^4                        -8.302e+00  5.067e+00
month(date, label = TRUE)^5                        -9.550e+00  5.035e+00
month(date, label = TRUE)^6                        -1.258e+00  5.073e+00
month(date, label = TRUE)^7                        -6.039e+00  4.964e+00
month(date, label = TRUE)^8                        -4.596e+00  4.916e+00
month(date, label = TRUE)^9                         2.721e+00  4.869e+00
month(date, label = TRUE)^10                        1.021e+00  4.852e+00
month(date, label = TRUE)^11                       -4.160e+00  4.832e+00
fourier_vec(date, type = "sin", K = 1, period = 7) -1.122e+01  2.256e+00
fourier_vec(date, type = "cos", K = 1, period = 7)  6.814e+00  2.345e+00
                                                   t value Pr(>|t|)    
(Intercept)                                         -3.405 0.000677 ***
date                                                 3.832 0.000131 ***
lag(value, 1)                                        4.941 8.51e-07 ***
lag(value, 7)                                        3.691 0.000230 ***
lag(value, 14)                                       3.371 0.000767 ***
lag(value, 21)                                       2.766 0.005731 ** 
lag(value, 28)                                       3.166 0.001572 ** 
lag(value, 35)                                       2.769 0.005685 ** 
lag(value, 42)                                       2.240 0.025215 *  
lag(value, 49)                                       2.777 0.005548 ** 
month(date, label = TRUE).L                         -1.581 0.114060    
month(date, label = TRUE).Q                          0.445 0.656363    
month(date, label = TRUE).C                         -3.242 0.001211 ** 
month(date, label = TRUE)^4                         -1.638 0.101521    
month(date, label = TRUE)^5                         -1.897 0.058026 .  
month(date, label = TRUE)^6                         -0.248 0.804244    
month(date, label = TRUE)^7                         -1.216 0.223965    
month(date, label = TRUE)^8                         -0.935 0.350011    
month(date, label = TRUE)^9                          0.559 0.576245    
month(date, label = TRUE)^10                         0.210 0.833423    
month(date, label = TRUE)^11                        -0.861 0.389407    
fourier_vec(date, type = "sin", K = 1, period = 7)  -4.973 7.26e-07 ***
fourier_vec(date, type = "cos", K = 1, period = 7)   2.906 0.003706 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 59.23 on 1709 degrees of freedom
  (49 observations deleted due to missingness)
Multiple R-squared:  0.2392,    Adjusted R-squared:  0.2294 
F-statistic: 24.42 on 22 and 1709 DF,  p-value: < 2.2e-16

NNS Forecasting

This is something I have been wanting to try for a while. The NNS package is a great package for forecasting time series data.

NNS GitHub

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( 3 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 8.14355679426738"
[1] "BEST method = 'lin' PATH MEMBER = c( 3 )"
[1] "BEST lin OBJECTIVE FUNCTION = 8.14355679426738"
[1] "CURRNET METHOD: nonlin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method =  'nonlin' , seasonal.factor =  c( 3 ) ...)"
[1] "CURRENT nonlin OBJECTIVE FUNCTION = 11.5188963808431"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 3 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 11.5188963808431"
[1] "CURRNET METHOD: both"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method =  'both' , seasonal.factor =  c( 3 ) ...)"
[1] "CURRENT both OBJECTIVE FUNCTION = 8.08916319720732"
[1] "BEST method = 'both' PATH MEMBER = c( 3 )"
[1] "BEST both OBJECTIVE FUNCTION = 8.08916319720732"

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( 5 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 53.8707732608271"
[1] "BEST method = 'lin' PATH MEMBER = c( 5 )"
[1] "BEST lin OBJECTIVE FUNCTION = 53.8707732608271"
[1] "CURRNET METHOD: nonlin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method =  'nonlin' , seasonal.factor =  c( 5 ) ...)"
[1] "CURRENT nonlin OBJECTIVE FUNCTION = 35.6602438776922"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 5 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 35.6602438776922"
[1] "CURRNET METHOD: both"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method =  'both' , seasonal.factor =  c( 5 ) ...)"
[1] "CURRENT both OBJECTIVE FUNCTION = 66.2911720840684"
[1] "BEST method = 'both' PATH MEMBER = c( 5 )"
[1] "BEST both OBJECTIVE FUNCTION = 66.2911720840684"

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( 22 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 7.55984402656023"
[1] "BEST method = 'lin' PATH MEMBER = c( 22 )"
[1] "BEST lin OBJECTIVE FUNCTION = 7.55984402656023"
[1] "CURRNET METHOD: nonlin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method =  'nonlin' , seasonal.factor =  c( 22 ) ...)"
[1] "CURRENT nonlin OBJECTIVE FUNCTION = 7.40948053856251"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 22 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 7.40948053856251"
[1] "CURRNET METHOD: both"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method =  'both' , seasonal.factor =  c( 22 ) ...)"
[1] "CURRENT both OBJECTIVE FUNCTION = 6.39078789196786"
[1] "BEST method = 'both' PATH MEMBER = c( 22 )"
[1] "BEST both OBJECTIVE FUNCTION = 6.39078789196786"

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( 2 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 65.8538420444278"
[1] "BEST method = 'lin' PATH MEMBER = c( 2 )"
[1] "BEST lin OBJECTIVE FUNCTION = 65.8538420444278"
[1] "CURRNET METHOD: nonlin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method =  'nonlin' , seasonal.factor =  c( 2 ) ...)"
[1] "CURRENT nonlin OBJECTIVE FUNCTION = 24.402423908038"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 2 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 24.402423908038"
[1] "CURRNET METHOD: both"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method =  'both' , seasonal.factor =  c( 2 ) ...)"
[1] "CURRENT both OBJECTIVE FUNCTION = 26.3221262460319"
[1] "BEST method = 'both' PATH MEMBER = c( 2 )"
[1] "BEST both OBJECTIVE FUNCTION = 26.3221262460319"

Package: healthyverse
[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 = 13.965442386704"
[1] "BEST method = 'lin' PATH MEMBER = c( 10 )"
[1] "BEST lin OBJECTIVE FUNCTION = 13.965442386704"
[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 = 12.0106291038189"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 10 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 12.0106291038189"
[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 = 9.06103294267949"
[1] "BEST method = 'both' PATH MEMBER = c( 10 )"
[1] "BEST both OBJECTIVE FUNCTION = 9.06103294267949"

Package: RandomWalker
[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 = 3.78036881955424"
[1] "BEST method = 'lin' PATH MEMBER = c( 25 )"
[1] "BEST lin OBJECTIVE FUNCTION = 3.78036881955424"
[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 = 7.19230486208912"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 25 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 7.19230486208912"
[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 = 5.83321048649789"
[1] "BEST method = 'both' PATH MEMBER = c( 25 )"
[1] "BEST both OBJECTIVE FUNCTION = 5.83321048649789"

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

Package: TidyDensity
[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 = 34.1253496735053"
[1] "BEST method = 'lin' PATH MEMBER = c( 11 )"
[1] "BEST lin OBJECTIVE FUNCTION = 34.1253496735053"
[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.0860039979295"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 11 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 11.0860039979295"
[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 = 24.9634405673427"
[1] "BEST method = 'both' PATH MEMBER = c( 11 )"
[1] "BEST both OBJECTIVE FUNCTION = 24.9634405673427"

[[1]]
NULL

[[2]]
NULL

[[3]]
NULL

[[4]]
NULL

[[5]]
NULL

[[6]]
NULL

[[7]]
NULL

[[8]]
NULL

Pre-Processing

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,773 × 50]> <tibble [28 × 50]> <split [1745|28]>
2 healthyR      <tibble [1,764 × 50]> <tibble [28 × 50]> <split [1736|28]>
3 healthyR.ts   <tibble [1,710 × 50]> <tibble [28 × 50]> <split [1682|28]>
4 healthyverse  <tibble [1,681 × 50]> <tibble [28 × 50]> <split [1653|28]>
5 healthyR.ai   <tibble [1,506 × 50]> <tibble [28 × 50]> <split [1478|28]>
6 TidyDensity   <tibble [1,357 × 50]> <tibble [28 × 50]> <split [1329|28]>
7 tidyAML       <tibble [964 × 50]>   <tibble [28 × 50]> <split [936|28]> 
8 RandomWalker  <tibble [387 × 50]>   <tibble [28 × 50]> <split [359|28]> 

Now it is time to make some recipes and models using the modeltime workflow.

Modeltime Workflow

Recipe Object

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

# 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 Tables

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),]

Model Accuracy

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.6445486 101.61201 0.6942080 162.4485 0.8137877 0.0532867
healthyR.data 2 LM Test 0.8037314 177.53076 0.8656551 143.2744 0.9723433 0.0049995
healthyR.data 3 NULL NA NA NA NA NA NA NA
healthyR.data 4 NNAR Test 0.7536167 129.62546 0.8116793 148.2688 0.9491970 0.0032517
healthyR 1 ARIMA Test 0.5319473 146.89514 0.7896331 173.0737 0.6663800 0.0085956
healthyR 2 LM Test 0.7295269 242.82709 1.0829242 154.5287 0.9451825 0.0283342
healthyR 3 NULL NA NA NA NA NA NA NA
healthyR 4 NNAR Test 0.5898167 187.32180 0.8755355 141.1373 0.8274473 0.0378338
healthyR.ts 1 ARIMA Test 0.5060458 94.93315 0.7209496 158.1647 0.6437125 0.0001573
healthyR.ts 2 LM Test 0.6270048 138.22964 0.8932765 137.1143 0.7816929 0.0036640
healthyR.ts 3 NULL NA NA NA NA NA NA NA
healthyR.ts 4 NNAR Test 0.7022620 186.87738 1.0004933 148.3743 0.8428415 0.0170186
healthyverse 1 ARIMA Test 0.6740553 118.03375 0.8020991 157.9121 0.8100768 0.0353154
healthyverse 2 LM Test 0.6767318 177.67356 0.8052841 152.3307 0.8110548 0.1242553
healthyverse 3 NULL NA NA NA NA NA NA NA
healthyverse 4 NNAR Test 0.6145310 169.13696 0.7312676 140.2969 0.7320920 0.0884013
healthyR.ai 1 ARIMA Test 0.4909486 106.39598 0.8693612 151.2732 0.6219120 0.0042982
healthyR.ai 2 LM Test 0.4617832 109.77640 0.8177158 134.3170 0.6255310 0.0548925
healthyR.ai 3 NULL NA NA NA NA NA NA NA
healthyR.ai 4 NNAR Test 0.5650957 156.28646 1.0006593 140.5847 0.7142858 0.0000382
TidyDensity 1 ARIMA Test 1.2718747 268.08817 0.9695626 119.1108 1.5500526 0.0043474
TidyDensity 2 LM Test 1.5543726 199.40353 1.1849135 161.3009 1.9254038 0.0117932
TidyDensity 3 NULL NA NA NA NA NA NA NA
TidyDensity 4 NNAR Test 1.3054563 202.86336 0.9951622 127.1320 1.5870346 0.0544057
tidyAML 1 ARIMA Test 0.9514485 119.71351 0.9244233 157.5239 1.3696256 0.0015414
tidyAML 2 LM Test 1.0359531 199.53931 1.0065276 156.9688 1.5004747 0.0002959
tidyAML 3 NULL NA NA NA NA NA NA NA
tidyAML 4 NNAR Test 1.0032804 180.36467 0.9747829 155.8998 1.4252395 0.0252275
RandomWalker 1 ARIMA Test 0.7356171 230.06898 0.5664817 148.1566 0.8301111 0.2861879
RandomWalker 2 LM Test 0.7855958 598.35566 0.6049692 145.0738 0.9353710 0.0029209
RandomWalker 3 NULL NA NA NA NA NA NA NA
RandomWalker 4 NNAR Test 0.8927815 430.40204 0.6875103 154.1877 1.1016064 0.0510776

Plot Models

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 Model

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…         1 ARIMA       Test  0.645 102.  0.694  162. 0.814 5.33e-2
2 healthyR             1 ARIMA       Test  0.532 147.  0.790  173. 0.666 8.60e-3
3 healthyR.ts          1 ARIMA       Test  0.506  94.9 0.721  158. 0.644 1.57e-4
4 healthyverse         4 NNAR        Test  0.615 169.  0.731  140. 0.732 8.84e-2
5 healthyR.ai          1 ARIMA       Test  0.491 106.  0.869  151. 0.622 4.30e-3
6 TidyDensity          1 ARIMA       Test  1.27  268.  0.970  119. 1.55  4.35e-3
7 tidyAML              1 ARIMA       Test  0.951 120.  0.924  158. 1.37  1.54e-3
8 RandomWalker         1 ARIMA       Test  0.736 230.  0.566  148. 0.830 2.86e-1
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")

Refitting and Future Forecast

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 [1745|28]> <mdl_tm_t [1 × 5]>
2 healthyR      <tibble>     <tibble>     <split [1736|28]> <mdl_tm_t [1 × 5]>
3 healthyR.ts   <tibble>     <tibble>     <split [1682|28]> <mdl_tm_t [1 × 5]>
4 healthyverse  <tibble>     <tibble>     <split [1653|28]> <mdl_tm_t [1 × 5]>
5 healthyR.ai   <tibble>     <tibble>     <split [1478|28]> <mdl_tm_t [1 × 5]>
6 TidyDensity   <tibble>     <tibble>     <split [1329|28]> <mdl_tm_t [1 × 5]>
7 tidyAML       <tibble>     <tibble>     <split [936|28]>  <mdl_tm_t [1 × 5]>
8 RandomWalker  <tibble>     <tibble>     <split [359|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")