healthyverse_tsa

Time Series Analysis, Modeling and Forecasting of the Healthyverse

Packages Steven P. Sanderson II, MPH - Date: 2026-01-07

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: 165,021
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-01-05 23:44:26, the file was birthed on: 2025-10-31 10:47:59.603742, and at report knit time is 1592.94 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 165021
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 121380 0.26 5 7 0 50 0
r_arch 121380 0.26 1 7 0 6 0
r_os 121380 0.26 7 19 0 24 0
package 0 1.00 7 13 0 8 0
version 0 1.00 5 17 0 62 0
country 15390 0.91 2 2 0 166 0

Variable type: Date

skim_variable n_missing complete_rate min max median n_unique
date 0 1 2020-11-23 2026-01-05 2023-11-17 1863

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
size 0 1 1123594.48 1485769.2 355 31085 310611 2348316 5677952 ▇▁▂▁▁
ip_id 0 1 11236.17 21865.3 1 233 2823 11797 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 2026-01-05 23:44:26 2023-11-17 12:33:09 104285

Variable type: Timespan

skim_variable n_missing complete_rate min max median n_unique
time 0 1 0 59 12H 6M 41S 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 
-148.31  -36.65  -11.17   27.15  820.41 

Coefficients:
                                                     Estimate Std. Error
(Intercept)                                        -1.779e+02  5.877e+01
date                                                1.093e-02  3.113e-03
lag(value, 1)                                       1.067e-01  2.301e-02
lag(value, 7)                                       8.761e-02  2.373e-02
lag(value, 14)                                      7.701e-02  2.369e-02
lag(value, 21)                                      8.547e-02  2.375e-02
lag(value, 28)                                      6.853e-02  2.367e-02
lag(value, 35)                                      5.199e-02  2.368e-02
lag(value, 42)                                      6.507e-02  2.378e-02
lag(value, 49)                                      6.545e-02  2.368e-02
month(date, label = TRUE).L                        -1.039e+01  4.918e+00
month(date, label = TRUE).Q                         2.934e-01  4.810e+00
month(date, label = TRUE).C                        -1.574e+01  4.845e+00
month(date, label = TRUE)^4                        -6.250e+00  4.873e+00
month(date, label = TRUE)^5                        -6.543e+00  4.854e+00
month(date, label = TRUE)^6                         1.167e+00  4.885e+00
month(date, label = TRUE)^7                        -4.438e+00  4.830e+00
month(date, label = TRUE)^8                        -4.101e+00  4.807e+00
month(date, label = TRUE)^9                         2.817e+00  4.820e+00
month(date, label = TRUE)^10                        9.131e-01  4.837e+00
month(date, label = TRUE)^11                       -4.103e+00  4.823e+00
fourier_vec(date, type = "sin", K = 1, period = 7) -1.110e+01  2.188e+00
fourier_vec(date, type = "cos", K = 1, period = 7)  7.116e+00  2.263e+00
                                                   t value Pr(>|t|)    
(Intercept)                                         -3.026 0.002509 ** 
date                                                 3.512 0.000457 ***
lag(value, 1)                                        4.636 3.82e-06 ***
lag(value, 7)                                        3.692 0.000229 ***
lag(value, 14)                                       3.251 0.001173 ** 
lag(value, 21)                                       3.598 0.000329 ***
lag(value, 28)                                       2.895 0.003838 ** 
lag(value, 35)                                       2.196 0.028234 *  
lag(value, 42)                                       2.736 0.006278 ** 
lag(value, 49)                                       2.764 0.005773 ** 
month(date, label = TRUE).L                         -2.112 0.034862 *  
month(date, label = TRUE).Q                          0.061 0.951372    
month(date, label = TRUE).C                         -3.249 0.001179 ** 
month(date, label = TRUE)^4                         -1.283 0.199831    
month(date, label = TRUE)^5                         -1.348 0.177812    
month(date, label = TRUE)^6                          0.239 0.811193    
month(date, label = TRUE)^7                         -0.919 0.358358    
month(date, label = TRUE)^8                         -0.853 0.393627    
month(date, label = TRUE)^9                          0.584 0.559041    
month(date, label = TRUE)^10                         0.189 0.850278    
month(date, label = TRUE)^11                        -0.851 0.395028    
fourier_vec(date, type = "sin", K = 1, period = 7)  -5.074 4.30e-07 ***
fourier_vec(date, type = "cos", K = 1, period = 7)   3.144 0.001692 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 59.16 on 1791 degrees of freedom
  (49 observations deleted due to missingness)
Multiple R-squared:  0.2281,    Adjusted R-squared:  0.2186 
F-statistic: 24.05 on 22 and 1791 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( 16 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 15.4152984346734"
[1] "BEST method = 'lin' PATH MEMBER = c( 16 )"
[1] "BEST lin OBJECTIVE FUNCTION = 15.4152984346734"
[1] "CURRNET METHOD: nonlin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method =  'nonlin' , seasonal.factor =  c( 16 ) ...)"
[1] "CURRENT nonlin OBJECTIVE FUNCTION = 14.9783221158427"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 16 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 14.9783221158427"
[1] "CURRNET METHOD: both"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method =  'both' , seasonal.factor =  c( 16 ) ...)"
[1] "CURRENT both OBJECTIVE FUNCTION = 15.9810155175667"
[1] "BEST method = 'both' PATH MEMBER = c( 16 )"
[1] "BEST both OBJECTIVE FUNCTION = 15.9810155175667"

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

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

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

Package: healthyverse
[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 = 12.2768225695489"
[1] "BEST method = 'lin' PATH MEMBER = c( 2 )"
[1] "BEST lin OBJECTIVE FUNCTION = 12.2768225695489"
[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 = 60.0571785006502"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 2 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 60.0571785006502"
[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 = 23.0419899836871"
[1] "BEST method = 'both' PATH MEMBER = c( 2 )"
[1] "BEST both OBJECTIVE FUNCTION = 23.0419899836871"

Package: RandomWalker
[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 = 4.71435603451493"
[1] "BEST method = 'lin' PATH MEMBER = c( 22 )"
[1] "BEST lin OBJECTIVE FUNCTION = 4.71435603451493"
[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.16653785319029"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 22 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 7.16653785319029"
[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.69913022981683"
[1] "BEST method = 'both' PATH MEMBER = c( 22 )"
[1] "BEST both OBJECTIVE FUNCTION = 6.69913022981683"

Package: tidyAML
[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 = 29.1310826811074"
[1] "BEST method = 'lin' PATH MEMBER = c( 5 )"
[1] "BEST lin OBJECTIVE FUNCTION = 29.1310826811074"
[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 = 5.09309826104778"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 5 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 5.09309826104778"
[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 = 4.20360441318497"
[1] "BEST method = 'both' PATH MEMBER = c( 5 )"
[1] "BEST both OBJECTIVE FUNCTION = 4.20360441318497"

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 = 13.1656287060795"
[1] "BEST method = 'lin' PATH MEMBER = c( 11 )"
[1] "BEST lin OBJECTIVE FUNCTION = 13.1656287060795"
[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 = 3.68785664839474"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 11 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 3.68785664839474"
[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 = 4.59617566633061"
[1] "BEST method = 'both' PATH MEMBER = c( 11 )"
[1] "BEST both OBJECTIVE FUNCTION = 4.59617566633061"

[[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,854 × 50]> <tibble [28 × 50]> <split [1826|28]>
2 healthyR      <tibble [1,846 × 50]> <tibble [28 × 50]> <split [1818|28]>
3 healthyR.ts   <tibble [1,788 × 50]> <tibble [28 × 50]> <split [1760|28]>
4 healthyverse  <tibble [1,752 × 50]> <tibble [28 × 50]> <split [1724|28]>
5 healthyR.ai   <tibble [1,588 × 50]> <tibble [28 × 50]> <split [1560|28]>
6 TidyDensity   <tibble [1,439 × 50]> <tibble [28 × 50]> <split [1411|28]>
7 tidyAML       <tibble [1,046 × 50]> <tibble [28 × 50]> <split [1018|28]>
8 RandomWalker  <tibble [469 × 50]>   <tibble [28 × 50]> <split [441|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.7700455 156.5418 0.6499645 146.2637 0.8726351 0.0540641
healthyR.data 2 LM Test 0.6955837 173.5825 0.5871143 131.4360 0.8119165 0.0975661
healthyR.data 3 EARTH Test 0.9753583 309.4283 0.8232608 126.4238 1.1612631 0.0056600
healthyR.data 4 NNAR Test 0.7644509 177.2692 0.6452423 137.4837 0.9157157 0.0006987
healthyR 1 ARIMA Test 0.5436782 286.2946 0.7427131 121.9137 0.6980680 0.1180019
healthyR 2 LM Test 0.5904421 451.8492 0.8065967 109.5139 0.7497147 0.0051117
healthyR 3 EARTH Test 0.8376259 868.6402 1.1442720 115.2661 0.9675747 0.0997740
healthyR 4 NNAR Test 0.5370739 239.9227 0.7336910 117.4048 0.7119163 0.0346538
healthyR.ts 1 ARIMA Test 0.7099362 130.6125 0.7712800 140.6835 1.0012655 0.0032415
healthyR.ts 2 LM Test 0.7520962 122.2661 0.8170830 122.1258 1.0517861 0.0014138
healthyR.ts 3 EARTH Test 0.6428525 134.8351 0.6983998 95.4240 0.8968520 0.0847293
healthyR.ts 4 NNAR Test 0.7554584 123.7695 0.8207357 127.1870 1.0666258 0.0005618
healthyverse 1 ARIMA Test 0.6972805 104.3177 0.7581001 109.4398 0.8683722 0.0013685
healthyverse 2 LM Test 0.7664275 127.5640 0.8332783 120.0347 0.9063872 0.0154571
healthyverse 3 EARTH Test 0.9256873 231.2822 1.0064293 101.5756 1.0751317 0.0867023
healthyverse 4 NNAR Test 0.7580834 125.0793 0.8242064 128.7876 0.9457266 0.0264734
healthyR.ai 1 ARIMA Test 1.0462267 138.9485 0.9292592 175.6361 1.2647297 0.0042927
healthyR.ai 2 LM Test 0.8650978 147.0916 0.7683805 136.3783 1.0302146 0.0820251
healthyR.ai 3 EARTH Test 1.5112775 380.1440 1.3423177 109.7202 1.9404137 0.0706054
healthyR.ai 4 NNAR Test 0.9105342 167.4718 0.8087371 149.4882 1.0664483 0.0768396
TidyDensity 1 ARIMA Test 1.0994889 178.0195 0.5689558 159.5115 1.2160380 0.1208148
TidyDensity 2 LM Test 1.1263779 192.8379 0.5828701 156.2024 1.2604858 0.0527263
TidyDensity 3 EARTH Test 1.1401044 150.6733 0.5899732 124.8304 1.4422800 0.0031176
TidyDensity 4 NNAR Test 1.1254646 203.0156 0.5823975 156.8293 1.2448875 0.0545559
tidyAML 1 ARIMA Test 0.8072872 107.7089 0.9493717 131.2575 1.0037174 0.0037743
tidyAML 2 LM Test 0.8772240 185.9247 1.0316176 136.4019 1.0609471 0.0555635
tidyAML 3 EARTH Test 1.2227186 188.9284 1.4379200 176.6407 1.5188868 0.1626243
tidyAML 4 NNAR Test 0.7911418 213.4876 0.9303847 120.9961 0.8969707 0.0745942
RandomWalker 1 ARIMA Test 0.8671046 121.4217 0.6026929 175.2116 0.9837434 0.0600833
RandomWalker 2 LM Test 0.9465890 137.3449 0.6579397 159.7801 1.0977088 0.0026896
RandomWalker 3 EARTH Test 1.0901726 217.3735 0.7577395 124.0966 1.4414515 0.0172453
RandomWalker 4 NNAR Test 0.9547820 205.1002 0.6636343 157.0835 1.0857085 0.0035698

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…         2 LM          Test  0.696  174. 0.587 131.  0.812 0.0976 
2 healthyR             1 ARIMA       Test  0.544  286. 0.743 122.  0.698 0.118  
3 healthyR.ts          3 EARTH       Test  0.643  135. 0.698  95.4 0.897 0.0847 
4 healthyverse         1 ARIMA       Test  0.697  104. 0.758 109.  0.868 0.00137
5 healthyR.ai          2 LM          Test  0.865  147. 0.768 136.  1.03  0.0820 
6 TidyDensity          1 ARIMA       Test  1.10   178. 0.569 160.  1.22  0.121  
7 tidyAML              4 NNAR        Test  0.791  213. 0.930 121.  0.897 0.0746 
8 RandomWalker         1 ARIMA       Test  0.867  121. 0.603 175.  0.984 0.0601 
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 [1826|28]> <mdl_tm_t [1 × 5]>
2 healthyR      <tibble>     <tibble>     <split [1818|28]> <mdl_tm_t [1 × 5]>
3 healthyR.ts   <tibble>     <tibble>     <split [1760|28]> <mdl_tm_t [1 × 5]>
4 healthyverse  <tibble>     <tibble>     <split [1724|28]> <mdl_tm_t [1 × 5]>
5 healthyR.ai   <tibble>     <tibble>     <split [1560|28]> <mdl_tm_t [1 × 5]>
6 TidyDensity   <tibble>     <tibble>     <split [1411|28]> <mdl_tm_t [1 × 5]>
7 tidyAML       <tibble>     <tibble>     <split [1018|28]> <mdl_tm_t [1 × 5]>
8 RandomWalker  <tibble>     <tibble>     <split [441|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")