healthyverse_tsa

Time Series Analysis, Modeling and Forecasting of the Healthyverse Packages

Steven P. Sanderson II, MPH - Date: 2026-09-09

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: 188,302
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 2026-09-07 23:58:13, the file was birthed on: 2025-10-31 10:47:59.603742, and at report knit time is 7473.17 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 188302
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 141536 0.25 5 17 0 54 0
r_arch 141536 0.25 1 7 0 7 0
r_os 141536 0.25 7 33 0 39 0
package 0 1.00 7 13 0 8 0
version 0 1.00 5 17 0 63 0
country 18390 0.90 2 2 0 172 0

Variable type: Date

skim_variable n_missing complete_rate min max median n_unique
date 0 1 2020-11-23 2026-09-07 2024-03-06 2108

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
size 0 1 1140931.93 1472136.49 355 46550 329202.5 2354658 5677952 ▇▁▂▁▁
ip_id 0 1 12561.65 26412.84 1 163 2742.0 12327 429286 ▇▁▁▁▁

Variable type: POSIXct

skim_variable n_missing complete_rate min max median n_unique
date_time 0 1 2020-11-23 09:00:41 2026-09-07 23:58:13 2024-03-06 04:45:33 121661

Variable type: Timespan

skim_variable n_missing complete_rate min max median n_unique
time 0 1 0 59 32 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 
-151.55  -38.67  -11.85   28.30  826.55 

Coefficients:
                                                     Estimate Std. Error
(Intercept)                                        -1.229e+02  4.720e+01
date                                                8.176e-03  2.485e-03
lag(value, 1)                                       9.876e-02  2.171e-02
lag(value, 7)                                       7.125e-02  2.234e-02
lag(value, 14)                                      7.680e-02  2.215e-02
lag(value, 21)                                      8.801e-02  2.224e-02
lag(value, 28)                                      8.335e-02  2.216e-02
lag(value, 35)                                      3.507e-02  2.219e-02
lag(value, 42)                                      6.115e-02  2.230e-02
lag(value, 49)                                      7.258e-02  2.225e-02
month(date, label = TRUE).L                        -8.480e+00  4.726e+00
month(date, label = TRUE).Q                         2.020e+00  4.617e+00
month(date, label = TRUE).C                        -1.501e+01  4.676e+00
month(date, label = TRUE)^4                        -9.871e+00  4.711e+00
month(date, label = TRUE)^5                        -5.391e+00  4.660e+00
month(date, label = TRUE)^6                         4.895e-01  4.694e+00
month(date, label = TRUE)^7                        -2.033e+00  4.623e+00
month(date, label = TRUE)^8                        -4.298e+00  4.598e+00
month(date, label = TRUE)^9                        -3.543e-01  4.605e+00
month(date, label = TRUE)^10                       -3.412e-01  4.544e+00
month(date, label = TRUE)^11                       -5.152e-01  4.477e+00
fourier_vec(date, type = "sin", K = 1, period = 7) -1.031e+01  2.056e+00
fourier_vec(date, type = "cos", K = 1, period = 7)  6.946e+00  2.114e+00
                                                   t value Pr(>|t|)    
(Intercept)                                         -2.604 0.009275 ** 
date                                                 3.290 0.001018 ** 
lag(value, 1)                                        4.549 5.71e-06 ***
lag(value, 7)                                        3.189 0.001447 ** 
lag(value, 14)                                       3.468 0.000536 ***
lag(value, 21)                                       3.958 7.81e-05 ***
lag(value, 28)                                       3.761 0.000174 ***
lag(value, 35)                                       1.580 0.114224    
lag(value, 42)                                       2.742 0.006157 ** 
lag(value, 49)                                       3.262 0.001123 ** 
month(date, label = TRUE).L                         -1.794 0.072899 .  
month(date, label = TRUE).Q                          0.438 0.661766    
month(date, label = TRUE).C                         -3.211 0.001345 ** 
month(date, label = TRUE)^4                         -2.096 0.036243 *  
month(date, label = TRUE)^5                         -1.157 0.247444    
month(date, label = TRUE)^6                          0.104 0.916951    
month(date, label = TRUE)^7                         -0.440 0.660175    
month(date, label = TRUE)^8                         -0.935 0.350058    
month(date, label = TRUE)^9                         -0.077 0.938689    
month(date, label = TRUE)^10                        -0.075 0.940146    
month(date, label = TRUE)^11                        -0.115 0.908400    
fourier_vec(date, type = "sin", K = 1, period = 7)  -5.013 5.82e-07 ***
fourier_vec(date, type = "cos", K = 1, period = 7)   3.286 0.001032 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 60.14 on 2036 degrees of freedom
  (49 observations deleted due to missingness)
Multiple R-squared:  0.1946,    Adjusted R-squared:  0.1859 
F-statistic: 22.36 on 22 and 2036 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( 19 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 5.6043077477447"
[1] "BEST method = 'lin' PATH MEMBER = c( 19 )"
[1] "BEST lin OBJECTIVE FUNCTION = 5.6043077477447"
[1] "CURRNET METHOD: nonlin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method =  'nonlin' , seasonal.factor =  c( 19 ) ...)"
[1] "CURRENT nonlin OBJECTIVE FUNCTION = 4.94273363775119"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 19 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 4.94273363775119"
[1] "CURRNET METHOD: both"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method =  'both' , seasonal.factor =  c( 19 ) ...)"
[1] "CURRENT both OBJECTIVE FUNCTION = 4.2522389573637"
[1] "BEST method = 'both' PATH MEMBER = c( 19 )"
[1] "BEST both OBJECTIVE FUNCTION = 4.2522389573637"

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( 22 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 6.36203963059885"
[1] "BEST method = 'lin' PATH MEMBER = c( 22 )"
[1] "BEST lin OBJECTIVE FUNCTION = 6.36203963059885"
[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 = 6.37599749998468"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 22 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 6.37599749998468"
[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.35900726255883"
[1] "BEST method = 'both' PATH MEMBER = c( 22 )"
[1] "BEST both OBJECTIVE FUNCTION = 6.35900726255883"

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

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

Package: healthyverse
[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 = 17.1164541932593"
[1] "BEST method = 'lin' PATH MEMBER = c( 5 )"
[1] "BEST lin OBJECTIVE FUNCTION = 17.1164541932593"
[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 = 13.9036425865023"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 5 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 13.9036425865023"
[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 = 18.6446423268379"
[1] "BEST method = 'both' PATH MEMBER = c( 5 )"
[1] "BEST both OBJECTIVE FUNCTION = 18.6446423268379"

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

Package: tidyAML
[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 = 11.2934696216867"
[1] "BEST method = 'lin' PATH MEMBER = c( 25 )"
[1] "BEST lin OBJECTIVE FUNCTION = 11.2934696216867"
[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 = 8.04321308691866"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 25 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 8.04321308691866"
[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.78936462099608"
[1] "BEST method = 'both' PATH MEMBER = c( 25 )"
[1] "BEST both OBJECTIVE FUNCTION = 7.78936462099608"

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

[[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 [2,096 × 50]> <tibble [28 × 50]> <split [2068|28]>
2 healthyR      <tibble [2,090 × 50]> <tibble [28 × 50]> <split [2062|28]>
3 healthyR.ts   <tibble [2,026 × 50]> <tibble [28 × 50]> <split [1998|28]>
4 healthyverse  <tibble [1,905 × 50]> <tibble [28 × 50]> <split [1877|28]>
5 healthyR.ai   <tibble [1,831 × 50]> <tibble [28 × 50]> <split [1803|28]>
6 TidyDensity   <tibble [1,684 × 50]> <tibble [28 × 50]> <split [1656|28]>
7 tidyAML       <tibble [1,287 × 50]> <tibble [28 × 50]> <split [1259|28]>
8 RandomWalker  <tibble [711 × 50]>   <tibble [28 × 50]> <split [683|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.8214067 423.3073 0.6293988 122.21771 1.0719371 0.0079288
healthyR.data 2 LM Test 0.7912731 529.8779 0.6063090 122.30888 1.0457989 0.0087175
healthyR.data 3 EARTH Test 5.5353115 4339.6429 4.2414047 182.55327 6.0303505 0.0912142
healthyR.data 4 NNAR Test 0.8029431 518.4518 0.6152511 126.60729 1.0404334 0.0220981
healthyR 1 ARIMA Test 0.7570316 301.2617 0.7319784 127.74055 0.9329499 0.0181410
healthyR 2 LM Test 0.8044477 260.3289 0.7778254 131.55618 1.0437135 0.0109078
healthyR 3 EARTH Test 2.5077737 1476.9466 2.4247817 175.41243 2.7969593 0.1653644
healthyR 4 NNAR Test 0.6615984 140.6529 0.6397035 133.92025 0.8775111 0.1010255
healthyR.ts 1 ARIMA Test 0.5645618 331.8148 0.6829879 111.31123 0.7528614 0.0082659
healthyR.ts 2 LM Test 0.6936134 422.4811 0.8391101 168.67772 0.7965853 0.0006473
healthyR.ts 3 EARTH Test 0.7128802 480.6145 0.8624184 118.64584 0.8770672 0.0625081
healthyR.ts 4 NNAR Test 0.6655983 354.2060 0.8052184 153.85452 0.7973727 0.0006676
healthyverse 1 ARIMA Test 0.5147528 258.8732 0.9238375 44.56867 0.6250428 0.0143777
healthyverse 2 LM Test 0.7861959 216.7522 1.4110020 78.51723 0.9226040 0.0100059
healthyverse 3 EARTH Test 0.4958259 303.9026 0.8898690 41.58116 0.6708712 0.0385566
healthyverse 4 NNAR Test 0.9411170 204.7580 1.6890422 100.99996 1.0410958 0.0012186
healthyR.ai 1 ARIMA Test 0.8746560 152.8643 1.0795743 145.69247 1.0328594 0.1090918
healthyR.ai 2 LM Test 0.9445083 157.3660 1.1657920 157.20279 1.1051544 0.0444481
healthyR.ai 3 EARTH Test 2.8984405 670.7082 3.5775003 160.47196 3.1789482 0.2855280
healthyR.ai 4 NNAR Test 0.9561294 154.1105 1.1801357 156.31372 1.1297796 0.0077178
TidyDensity 1 ARIMA Test 0.9102311 235.5154 0.8346333 142.81712 1.0984217 0.0316294
TidyDensity 2 LM Test 0.7701492 198.7797 0.7061857 142.54863 0.8783090 0.0221856
TidyDensity 3 EARTH Test 2.7508609 1250.4174 2.5223925 153.43564 3.1076936 0.0616347
TidyDensity 4 NNAR Test 0.7661659 232.6606 0.7025332 137.17611 0.8905447 0.0455362
tidyAML 1 ARIMA Test 0.9414153 118.9509 1.3447336 183.26298 1.0932761 0.0017669
tidyAML 2 LM Test 1.0175523 119.3348 1.4534890 160.14704 1.2160093 0.0044608
tidyAML 3 EARTH Test 0.9333616 101.6304 1.3332296 187.60134 1.0909329 0.2252899
tidyAML 4 NNAR Test 1.0399672 150.6841 1.4855068 154.93084 1.2692850 0.0218667
RandomWalker 1 ARIMA Test 0.7870756 249.1851 0.7810382 122.63982 0.9550054 0.1824893
RandomWalker 2 LM Test 0.6062261 108.2093 0.6015759 160.69376 0.6940545 0.0250432
RandomWalker 3 EARTH Test 1.3250794 406.7844 1.3149153 137.08975 1.5055904 0.0059858
RandomWalker 4 NNAR Test 0.6298964 127.0596 0.6250647 140.38101 0.7670723 0.0000535

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…         4 NNAR        Test  0.803  518. 0.615 127.  1.04  0.0221 
2 healthyR             4 NNAR        Test  0.662  141. 0.640 134.  0.878 0.101  
3 healthyR.ts          1 ARIMA       Test  0.565  332. 0.683 111.  0.753 0.00827
4 healthyverse         1 ARIMA       Test  0.515  259. 0.924  44.6 0.625 0.0144 
5 healthyR.ai          1 ARIMA       Test  0.875  153. 1.08  146.  1.03  0.109  
6 TidyDensity          2 LM          Test  0.770  199. 0.706 143.  0.878 0.0222 
7 tidyAML              3 EARTH       Test  0.933  102. 1.33  188.  1.09  0.225  
8 RandomWalker         2 LM          Test  0.606  108. 0.602 161.  0.694 0.0250 
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 [2068|28]> <mdl_tm_t [1 × 5]>
2 healthyR      <tibble>     <tibble>     <split [2062|28]> <mdl_tm_t [1 × 5]>
3 healthyR.ts   <tibble>     <tibble>     <split [1998|28]> <mdl_tm_t [1 × 5]>
4 healthyverse  <tibble>     <tibble>     <split [1877|28]> <mdl_tm_t [1 × 5]>
5 healthyR.ai   <tibble>     <tibble>     <split [1803|28]> <mdl_tm_t [1 × 5]>
6 TidyDensity   <tibble>     <tibble>     <split [1656|28]> <mdl_tm_t [1 × 5]>
7 tidyAML       <tibble>     <tibble>     <split [1259|28]> <mdl_tm_t [1 × 5]>
8 RandomWalker  <tibble>     <tibble>     <split [683|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")