healthyverse_tsa

Time Series Analysis, Modeling and Forecasting of the Healthyverse

Packages Steven P. Sanderson II, MPH - Date: 2025-11-15

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: 160,240
Columns: 11
$ date      <date> 2020-11-23, 2020-11-23, 2020-11-23, 2020-11-23, 2020-11-23,…
$ time      <Period> 15H 36M 55S, 11H 26M 39S, 23H 34M 44S, 18H 39M 32S, 9H 0M…
$ date_time <dttm> 2020-11-23 15:36:55, 2020-11-23 11:26:39, 2020-11-23 23:34:…
$ size      <int> 4858294, 4858294, 4858301, 4858295, 361, 4863722, 4864794, 4…
$ r_version <chr> NA, "4.0.3", "3.5.3", "3.5.2", NA, NA, NA, NA, NA, NA, NA, N…
$ r_arch    <chr> NA, "x86_64", "x86_64", "x86_64", NA, NA, NA, NA, NA, NA, NA…
$ r_os      <chr> NA, "mingw32", "mingw32", "linux-gnu", NA, NA, NA, NA, NA, N…
$ package   <chr> "healthyR.data", "healthyR.data", "healthyR.data", "healthyR…
$ version   <chr> "1.0.0", "1.0.0", "1.0.0", "1.0.0", "1.0.0", "1.0.0", "1.0.0…
$ country   <chr> "US", "US", "US", "GB", "US", "US", "DE", "HK", "JP", "US", …
$ ip_id     <int> 2069, 2804, 78827, 27595, 90474, 90474, 42435, 74, 7655, 638…

The last day in the data set is 2025-11-13 21:11:20, the file was birthed on: 2022-07-02 23:58:17.511888, and at report knit time is 2.951322^{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 160240
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 117529 0.27 5 7 0 50 0
r_arch 117529 0.27 1 7 0 6 0
r_os 117529 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 14997 0.91 2 2 0 165 0

Variable type: Date

skim_variable n_missing complete_rate min max median n_unique
date 0 1 2020-11-23 2025-11-13 2023-10-24 1810

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
size 0 1 1125227.35 1491076.70 355 26719.75 309998 2355466 5677952 ▇▁▂▁▁
ip_id 0 1 11323.88 21981.89 1 228.00 2910 11961 299146 ▇▁▁▁▁

Variable type: POSIXct

skim_variable n_missing complete_rate min max median n_unique
date_time 0 1 2020-11-23 09:00:41 2025-11-13 21:11:20 2023-10-24 08:51:59 100809

Variable type: Timespan

skim_variable n_missing complete_rate min max median n_unique
time 0 1 0 59 24 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.72  -36.46  -11.40   27.26  818.81 

Coefficients:
                                                     Estimate Std. Error
(Intercept)                                        -1.991e+02  6.164e+01
date                                                1.206e-02  3.269e-03
lag(value, 1)                                       1.103e-01  2.336e-02
lag(value, 7)                                       8.760e-02  2.413e-02
lag(value, 14)                                      7.960e-02  2.410e-02
lag(value, 21)                                      7.705e-02  2.417e-02
lag(value, 28)                                      6.516e-02  2.409e-02
lag(value, 35)                                      5.804e-02  2.416e-02
lag(value, 42)                                      5.972e-02  2.434e-02
lag(value, 49)                                      6.524e-02  2.426e-02
month(date, label = TRUE).L                        -9.478e+00  5.084e+00
month(date, label = TRUE).Q                         1.446e+00  5.015e+00
month(date, label = TRUE).C                        -1.562e+01  5.026e+00
month(date, label = TRUE)^4                        -6.766e+00  5.012e+00
month(date, label = TRUE)^5                        -7.748e+00  4.979e+00
month(date, label = TRUE)^6                         4.436e-02  5.001e+00
month(date, label = TRUE)^7                        -5.442e+00  4.894e+00
month(date, label = TRUE)^8                        -4.665e+00  4.849e+00
month(date, label = TRUE)^9                         2.436e+00  4.843e+00
month(date, label = TRUE)^10                        8.164e-01  4.856e+00
month(date, label = TRUE)^11                       -4.143e+00  4.843e+00
fourier_vec(date, type = "sin", K = 1, period = 7) -1.154e+01  2.237e+00
fourier_vec(date, type = "cos", K = 1, period = 7)  7.340e+00  2.317e+00
                                                   t value Pr(>|t|)    
(Intercept)                                         -3.230 0.001263 ** 
date                                                 3.689 0.000232 ***
lag(value, 1)                                        4.722 2.52e-06 ***
lag(value, 7)                                        3.631 0.000291 ***
lag(value, 14)                                       3.303 0.000976 ***
lag(value, 21)                                       3.187 0.001461 ** 
lag(value, 28)                                       2.705 0.006895 ** 
lag(value, 35)                                       2.402 0.016396 *  
lag(value, 42)                                       2.454 0.014235 *  
lag(value, 49)                                       2.689 0.007232 ** 
month(date, label = TRUE).L                         -1.864 0.062442 .  
month(date, label = TRUE).Q                          0.288 0.773162    
month(date, label = TRUE).C                         -3.107 0.001921 ** 
month(date, label = TRUE)^4                         -1.350 0.177225    
month(date, label = TRUE)^5                         -1.556 0.119813    
month(date, label = TRUE)^6                          0.009 0.992924    
month(date, label = TRUE)^7                         -1.112 0.266308    
month(date, label = TRUE)^8                         -0.962 0.336172    
month(date, label = TRUE)^9                          0.503 0.614971    
month(date, label = TRUE)^10                         0.168 0.866507    
month(date, label = TRUE)^11                        -0.856 0.392348    
fourier_vec(date, type = "sin", K = 1, period = 7)  -5.160 2.76e-07 ***
fourier_vec(date, type = "cos", K = 1, period = 7)   3.168 0.001562 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 59.39 on 1738 degrees of freedom
  (49 observations deleted due to missingness)
Multiple R-squared:  0.2333,    Adjusted R-squared:  0.2236 
F-statistic: 24.04 on 22 and 1738 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( 5 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 10.2218309948049"
[1] "BEST method = 'lin' PATH MEMBER = c( 5 )"
[1] "BEST lin OBJECTIVE FUNCTION = 10.2218309948049"
[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 = 7.39054316240373"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 5 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 7.39054316240373"
[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 = 10.206755757149"
[1] "BEST method = 'both' PATH MEMBER = c( 5 )"
[1] "BEST both OBJECTIVE FUNCTION = 10.206755757149"

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

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

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( 16 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 12.7055468358819"
[1] "BEST method = 'lin' PATH MEMBER = c( 16 )"
[1] "BEST lin OBJECTIVE FUNCTION = 12.7055468358819"
[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 = 7.65296232676752"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 16 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 7.65296232676752"
[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 = 8.98694619703699"
[1] "BEST method = 'both' PATH MEMBER = c( 16 )"
[1] "BEST both OBJECTIVE FUNCTION = 8.98694619703699"

Package: healthyverse
[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 = 34.2412531933398"
[1] "BEST method = 'lin' PATH MEMBER = c( 3 )"
[1] "BEST lin OBJECTIVE FUNCTION = 34.2412531933398"
[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 = 6.31664591371713"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 3 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 6.31664591371713"
[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 = 9.7265148876117"
[1] "BEST method = 'both' PATH MEMBER = c( 3 )"
[1] "BEST both OBJECTIVE FUNCTION = 9.7265148876117"

Package: RandomWalker
[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 = 155.832310375856"
[1] "BEST method = 'lin' PATH MEMBER = c( 2 )"
[1] "BEST lin OBJECTIVE FUNCTION = 155.832310375856"
[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 = 52.8697031611539"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 2 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 52.8697031611539"
[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 = 63.9055771101315"
[1] "BEST method = 'both' PATH MEMBER = c( 2 )"
[1] "BEST both OBJECTIVE FUNCTION = 63.9055771101315"

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

Package: TidyDensity
[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 = 11.1423185427416"
[1] "BEST method = 'lin' PATH MEMBER = c( 3 )"
[1] "BEST lin OBJECTIVE FUNCTION = 11.1423185427416"
[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 = 2.5148843004701"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 3 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 2.5148843004701"
[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 = 3.18847973177893"
[1] "BEST method = 'both' PATH MEMBER = c( 3 )"
[1] "BEST both OBJECTIVE FUNCTION = 3.18847973177893"

[[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,802 × 50]> <tibble [28 × 50]> <split [1774|28]>
2 healthyR      <tibble [1,793 × 50]> <tibble [28 × 50]> <split [1765|28]>
3 healthyR.ts   <tibble [1,739 × 50]> <tibble [28 × 50]> <split [1711|28]>
4 healthyverse  <tibble [1,710 × 50]> <tibble [28 × 50]> <split [1682|28]>
5 healthyR.ai   <tibble [1,535 × 50]> <tibble [28 × 50]> <split [1507|28]>
6 TidyDensity   <tibble [1,386 × 50]> <tibble [28 × 50]> <split [1358|28]>
7 tidyAML       <tibble [993 × 50]>   <tibble [28 × 50]> <split [965|28]> 
8 RandomWalker  <tibble [416 × 50]>   <tibble [28 × 50]> <split [388|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.6263653 104.55831 0.6088444 154.6322 0.7702092 0.0961322
healthyR.data 2 LM Test 0.7151764 188.25074 0.6951713 143.7417 0.8224735 0.0025983
healthyR.data 3 EARTH Test 0.6702533 97.25623 0.6515048 177.2687 0.8105751 0.0090187
healthyR.data 4 NNAR Test 0.7455827 200.02308 0.7247270 149.0294 0.8586736 0.0005147
healthyR 1 ARIMA Test 0.7548705 112.63959 0.6863639 177.3977 1.0514138 0.0205642
healthyR 2 LM Test 0.7937890 266.26544 0.7217504 139.8340 0.9717826 0.0450710
healthyR 3 EARTH Test 0.8485882 193.38121 0.7715764 133.3505 1.2227113 0.0339109
healthyR 4 NNAR Test 0.7625437 194.70382 0.6933407 153.9383 0.9678839 0.0621554
healthyR.ts 1 ARIMA Test 0.7345513 121.27237 0.5803031 163.9835 1.0028664 0.0475689
healthyR.ts 2 LM Test 0.9110817 303.32944 0.7197640 168.9436 1.1306189 0.0041613
healthyR.ts 3 EARTH Test 0.7992055 153.53775 0.6313806 151.3809 1.0820263 0.0205395
healthyR.ts 4 NNAR Test 1.0216640 399.77347 0.8071252 182.2319 1.2237781 0.0106494
healthyverse 1 ARIMA Test 0.6073118 90.38111 0.8538870 144.7301 0.8133636 0.1167300
healthyverse 2 LM Test 0.7784853 188.92126 1.0945588 149.4198 0.8981262 0.0000971
healthyverse 3 EARTH Test 0.6803035 101.65719 0.9565141 166.6507 0.8788223 0.0596485
healthyverse 4 NNAR Test 0.7519458 186.79748 1.0572440 137.7688 0.9146347 0.0005821
healthyR.ai 1 ARIMA Test 0.5149260 97.04598 0.8438281 177.4301 0.7038524 0.0081149
healthyR.ai 2 LM Test 0.6762455 257.01739 1.1081882 156.2170 0.8032432 0.0042448
healthyR.ai 3 EARTH Test 0.6706520 203.41774 1.0990220 129.1312 0.8687384 0.0105133
healthyR.ai 4 NNAR Test 0.6480671 217.92350 1.0620112 157.1391 0.7674413 0.0046300
TidyDensity 1 ARIMA Test 0.9759969 345.25756 0.6076949 134.2522 1.0862754 0.0567669
TidyDensity 2 LM Test 0.8581685 146.44390 0.5343302 147.1794 1.0800026 0.0090094
TidyDensity 3 EARTH Test 1.1804694 569.46870 0.7350076 130.9809 1.3284643 0.0006641
TidyDensity 4 NNAR Test 0.9309029 236.51608 0.5796175 143.2679 1.1131906 0.0044373
tidyAML 1 ARIMA Test 0.7658128 146.59503 0.7532331 152.3139 1.0149978 0.0422946
tidyAML 2 LM Test 0.7587890 158.63572 0.7463248 141.7573 0.9545947 0.0218389
tidyAML 3 EARTH Test 1.9942053 801.17291 1.9614474 149.1246 2.3219629 0.0289668
tidyAML 4 NNAR Test 0.6950095 136.22270 0.6835929 145.0702 0.8994436 0.0912264
RandomWalker 1 ARIMA Test 0.7106099 103.58272 0.5423400 145.3718 0.8245327 0.1874503
RandomWalker 2 LM Test 0.8391255 158.98362 0.6404235 147.4285 0.9188780 0.0004570
RandomWalker 3 EARTH Test 0.8372669 154.72938 0.6390050 141.1055 0.9015402 0.0044252
RandomWalker 4 NNAR Test 0.9913480 203.03491 0.7566002 157.1338 1.0891837 0.0796963

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.626 105.  0.609  155. 0.770 0.0961 
2 healthyR             4 NNAR        Test  0.763 195.  0.693  154. 0.968 0.0622 
3 healthyR.ts          1 ARIMA       Test  0.735 121.  0.580  164. 1.00  0.0476 
4 healthyverse         1 ARIMA       Test  0.607  90.4 0.854  145. 0.813 0.117  
5 healthyR.ai          1 ARIMA       Test  0.515  97.0 0.844  177. 0.704 0.00811
6 TidyDensity          2 LM          Test  0.858 146.  0.534  147. 1.08  0.00901
7 tidyAML              4 NNAR        Test  0.695 136.  0.684  145. 0.899 0.0912 
8 RandomWalker         1 ARIMA       Test  0.711 104.  0.542  145. 0.825 0.187  
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 [1774|28]> <mdl_tm_t [1 × 5]>
2 healthyR      <tibble>     <tibble>     <split [1765|28]> <mdl_tm_t [1 × 5]>
3 healthyR.ts   <tibble>     <tibble>     <split [1711|28]> <mdl_tm_t [1 × 5]>
4 healthyverse  <tibble>     <tibble>     <split [1682|28]> <mdl_tm_t [1 × 5]>
5 healthyR.ai   <tibble>     <tibble>     <split [1507|28]> <mdl_tm_t [1 × 5]>
6 TidyDensity   <tibble>     <tibble>     <split [1358|28]> <mdl_tm_t [1 × 5]>
7 tidyAML       <tibble>     <tibble>     <split [965|28]>  <mdl_tm_t [1 × 5]>
8 RandomWalker  <tibble>     <tibble>     <split [388|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")