healthyverse_tsa

Time Series Analysis, Modeling and Forecasting of the Healthyverse

Packages Steven P. Sanderson II, MPH - Date: 2025-12-18

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: 163,393
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-12-16 23:04:24, the file was birthed on: 2022-07-02 23:58:17.511888, and at report knit time is 3.03071^{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 163393
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 119920 0.27 5 7 0 50 0
r_arch 119920 0.27 1 7 0 6 0
r_os 119920 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 15313 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 2025-12-16 2023-11-08 1843

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
size 0 1 1124293.44 1487029.96 355 29181 310324 2349544 5677952 ▇▁▂▁▁
ip_id 0 1 11299.48 21939.64 1 228 2882 11921 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-12-16 23:04:24 2023-11-08 23:25:30 103225

Variable type: Timespan

skim_variable n_missing complete_rate min max median n_unique
time 0 1 0 59 12H 6M 49S 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 
-146.94  -36.44  -11.32   26.88  820.25 

Coefficients:
                                                     Estimate Std. Error
(Intercept)                                        -1.877e+02  5.996e+01
date                                                1.146e-02  3.178e-03
lag(value, 1)                                       1.092e-01  2.314e-02
lag(value, 7)                                       8.822e-02  2.387e-02
lag(value, 14)                                      7.740e-02  2.380e-02
lag(value, 21)                                      8.287e-02  2.388e-02
lag(value, 28)                                      6.706e-02  2.380e-02
lag(value, 35)                                      5.175e-02  2.381e-02
lag(value, 42)                                      6.663e-02  2.395e-02
lag(value, 49)                                      6.118e-02  2.388e-02
month(date, label = TRUE).L                        -1.019e+01  4.991e+00
month(date, label = TRUE).Q                         1.049e+00  4.900e+00
month(date, label = TRUE).C                        -1.537e+01  4.931e+00
month(date, label = TRUE)^4                        -5.690e+00  4.921e+00
month(date, label = TRUE)^5                        -6.391e+00  4.884e+00
month(date, label = TRUE)^6                         1.430e+00  4.902e+00
month(date, label = TRUE)^7                        -4.373e+00  4.840e+00
month(date, label = TRUE)^8                        -4.005e+00  4.814e+00
month(date, label = TRUE)^9                         2.794e+00  4.827e+00
month(date, label = TRUE)^10                        9.289e-01  4.844e+00
month(date, label = TRUE)^11                       -4.060e+00  4.830e+00
fourier_vec(date, type = "sin", K = 1, period = 7) -1.122e+01  2.206e+00
fourier_vec(date, type = "cos", K = 1, period = 7)  7.224e+00  2.285e+00
                                                   t value Pr(>|t|)    
(Intercept)                                         -3.131 0.001773 ** 
date                                                 3.605 0.000320 ***
lag(value, 1)                                        4.716 2.59e-06 ***
lag(value, 7)                                        3.696 0.000225 ***
lag(value, 14)                                       3.251 0.001170 ** 
lag(value, 21)                                       3.470 0.000534 ***
lag(value, 28)                                       2.818 0.004885 ** 
lag(value, 35)                                       2.173 0.029895 *  
lag(value, 42)                                       2.782 0.005463 ** 
lag(value, 49)                                       2.562 0.010487 *  
month(date, label = TRUE).L                         -2.042 0.041293 *  
month(date, label = TRUE).Q                          0.214 0.830434    
month(date, label = TRUE).C                         -3.116 0.001860 ** 
month(date, label = TRUE)^4                         -1.156 0.247685    
month(date, label = TRUE)^5                         -1.309 0.190863    
month(date, label = TRUE)^6                          0.292 0.770471    
month(date, label = TRUE)^7                         -0.904 0.366329    
month(date, label = TRUE)^8                         -0.832 0.405543    
month(date, label = TRUE)^9                          0.579 0.562733    
month(date, label = TRUE)^10                         0.192 0.847938    
month(date, label = TRUE)^11                        -0.841 0.400689    
fourier_vec(date, type = "sin", K = 1, period = 7)  -5.083 4.10e-07 ***
fourier_vec(date, type = "cos", K = 1, period = 7)   3.162 0.001593 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 59.25 on 1771 degrees of freedom
  (49 observations deleted due to missingness)
Multiple R-squared:  0.2292,    Adjusted R-squared:  0.2197 
F-statistic: 23.94 on 22 and 1771 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( 1 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 243.478092455172"
[1] "BEST method = 'lin' PATH MEMBER = c( 1 )"
[1] "BEST lin OBJECTIVE FUNCTION = 243.478092455172"
[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 = 30.0366329894861"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 1 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 30.0366329894861"
[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 = 44.329328767277"
[1] "BEST method = 'both' PATH MEMBER = c( 1 )"
[1] "BEST both OBJECTIVE FUNCTION = 44.329328767277"

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( 3 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 25.6385795145685"
[1] "BEST method = 'lin' PATH MEMBER = c( 3 )"
[1] "BEST lin OBJECTIVE FUNCTION = 25.6385795145685"
[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 = 20.7505942967418"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 3 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 20.7505942967418"
[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 = 33.8876693481393"
[1] "BEST method = 'both' PATH MEMBER = c( 3 )"
[1] "BEST both OBJECTIVE FUNCTION = 33.8876693481393"

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( 5 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 21.0668758514447"
[1] "BEST method = 'lin' PATH MEMBER = c( 5 )"
[1] "BEST lin OBJECTIVE FUNCTION = 21.0668758514447"
[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 = 4.34974531506221"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 5 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 4.34974531506221"
[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 = 6.40297197837678"
[1] "BEST method = 'both' PATH MEMBER = c( 5 )"
[1] "BEST both OBJECTIVE FUNCTION = 6.40297197837678"

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( 19 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 18.1201913399835"
[1] "BEST method = 'lin' PATH MEMBER = c( 19 )"
[1] "BEST lin OBJECTIVE FUNCTION = 18.1201913399835"
[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 = 21.1594070337835"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 19 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 21.1594070337835"
[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 = 22.3002416739308"
[1] "BEST method = 'both' PATH MEMBER = c( 19 )"
[1] "BEST both OBJECTIVE FUNCTION = 22.3002416739308"

Package: healthyverse
[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 = 8.00358885549315"
[1] "BEST method = 'lin' PATH MEMBER = c( 4 )"
[1] "BEST lin OBJECTIVE FUNCTION = 8.00358885549315"
[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 = 9.44183272187358"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 4 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 9.44183272187358"
[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 = 9.34700411858771"
[1] "BEST method = 'both' PATH MEMBER = c( 4 )"
[1] "BEST both OBJECTIVE FUNCTION = 9.34700411858771"

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

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

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

[[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,835 × 50]> <tibble [28 × 50]> <split [1807|28]>
2 healthyR      <tibble [1,826 × 50]> <tibble [28 × 50]> <split [1798|28]>
3 healthyR.ts   <tibble [1,771 × 50]> <tibble [28 × 50]> <split [1743|28]>
4 healthyverse  <tibble [1,742 × 50]> <tibble [28 × 50]> <split [1714|28]>
5 healthyR.ai   <tibble [1,568 × 50]> <tibble [28 × 50]> <split [1540|28]>
6 TidyDensity   <tibble [1,419 × 50]> <tibble [28 × 50]> <split [1391|28]>
7 tidyAML       <tibble [1,026 × 50]> <tibble [28 × 50]> <split [998|28]> 
8 RandomWalker  <tibble [449 × 50]>   <tibble [28 × 50]> <split [421|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.6295378 142.71893 0.6832193 133.8406 0.7205613 0.1066485
healthyR.data 2 LM Test 0.6421498 212.15756 0.6969067 114.8769 0.7788429 0.0029393
healthyR.data 3 EARTH Test 1.4793042 704.58514 1.6054463 119.9178 1.7286596 0.0535162
healthyR.data 4 NNAR Test 0.7094235 224.58871 0.7699169 130.5761 0.8322777 0.0018386
healthyR 1 ARIMA Test 0.5590016 218.89165 0.8119011 138.4364 0.7092469 0.0332536
healthyR 2 LM Test 0.5584064 444.28955 0.8110366 112.1494 0.6825985 0.0544181
healthyR 3 EARTH Test 0.7594535 818.04051 1.1030399 105.1129 0.9259031 0.0891325
healthyR 4 NNAR Test 0.5810239 290.55244 0.8438865 133.4932 0.6928366 0.0408093
healthyR.ts 1 ARIMA Test 0.6083277 119.69416 0.7093934 146.4844 0.7583060 0.0302680
healthyR.ts 2 LM Test 0.7334489 185.73691 0.8553019 137.8295 0.8885957 0.0079987
healthyR.ts 3 EARTH Test 0.5557010 130.21659 0.6480235 103.7069 0.7284684 0.0004797
healthyR.ts 4 NNAR Test 0.8468405 183.64304 0.9875321 157.4683 1.0213269 0.0598094
healthyverse 1 ARIMA Test 0.7100772 85.04524 0.6903345 132.0528 0.9259873 0.0340838
healthyverse 2 LM Test 0.8052442 134.10717 0.7828555 121.8219 0.9451966 0.0003340
healthyverse 3 EARTH Test 0.9790064 240.17660 0.9517864 106.7423 1.1598628 0.2271117
healthyverse 4 NNAR Test 0.7323805 124.13894 0.7120176 128.3646 0.8545041 0.0883922
healthyR.ai 1 ARIMA Test 0.8303134 97.33738 1.0363633 178.5921 0.9915573 0.4814802
healthyR.ai 2 LM Test 1.0765458 190.24565 1.3437005 155.5457 1.2798128 0.0953732
healthyR.ai 3 EARTH Test 0.8416255 111.82412 1.0504826 151.7271 1.0074093 0.2959586
healthyR.ai 4 NNAR Test 1.1067304 194.12150 1.3813757 160.8017 1.2905852 0.0026308
TidyDensity 1 ARIMA Test 1.1282701 232.81935 0.7062059 166.8137 1.2344122 0.1118411
TidyDensity 2 LM Test 0.9732163 136.55889 0.6091547 162.0322 1.1653704 0.0027889
TidyDensity 3 EARTH Test 1.0424516 167.14861 0.6524904 174.6130 1.1744119 0.0321696
TidyDensity 4 NNAR Test 1.0461327 166.46550 0.6547945 157.7004 1.1785186 0.0233588
tidyAML 1 ARIMA Test 0.5256984 123.75762 0.6236111 126.3257 0.6753641 0.0522170
tidyAML 2 LM Test 0.6420028 280.31938 0.7615774 140.3228 0.7936177 0.0306975
tidyAML 3 EARTH Test 1.2405593 637.67625 1.4716166 127.5621 1.4459581 0.0990013
tidyAML 4 NNAR Test 0.7106039 349.70407 0.8429557 122.4697 0.8418906 0.0133717
RandomWalker 1 ARIMA Test 0.6990402 124.77274 0.6667399 158.1787 0.7969401 0.2363105
RandomWalker 2 LM Test 0.7390193 134.96611 0.7048717 151.5084 0.9042367 0.0078612
RandomWalker 3 EARTH Test 0.8839719 221.99772 0.8431266 163.8289 0.9438590 0.0853449
RandomWalker 4 NNAR Test 0.7113354 117.44345 0.6784670 145.1600 0.8925960 0.0099587

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.630 143.  0.683  134. 0.721 1.07e-1
2 healthyR             2 LM          Test  0.558 444.  0.811  112. 0.683 5.44e-2
3 healthyR.ts          3 EARTH       Test  0.556 130.  0.648  104. 0.728 4.80e-4
4 healthyverse         4 NNAR        Test  0.732 124.  0.712  128. 0.855 8.84e-2
5 healthyR.ai          1 ARIMA       Test  0.830  97.3 1.04   179. 0.992 4.81e-1
6 TidyDensity          2 LM          Test  0.973 137.  0.609  162. 1.17  2.79e-3
7 tidyAML              1 ARIMA       Test  0.526 124.  0.624  126. 0.675 5.22e-2
8 RandomWalker         1 ARIMA       Test  0.699 125.  0.667  158. 0.797 2.36e-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 [1807|28]> <mdl_tm_t [1 × 5]>
2 healthyR      <tibble>     <tibble>     <split [1798|28]> <mdl_tm_t [1 × 5]>
3 healthyR.ts   <tibble>     <tibble>     <split [1743|28]> <mdl_tm_t [1 × 5]>
4 healthyverse  <tibble>     <tibble>     <split [1714|28]> <mdl_tm_t [1 × 5]>
5 healthyR.ai   <tibble>     <tibble>     <split [1540|28]> <mdl_tm_t [1 × 5]>
6 TidyDensity   <tibble>     <tibble>     <split [1391|28]> <mdl_tm_t [1 × 5]>
7 tidyAML       <tibble>     <tibble>     <split [998|28]>  <mdl_tm_t [1 × 5]>
8 RandomWalker  <tibble>     <tibble>     <split [421|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")