healthyverse_tsa

Time Series Analysis, Modeling and Forecasting of the Healthyverse

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

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,105
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-14 23:15:21, the file was birthed on: 2022-07-02 23:58:17.511888, and at report knit time is 3.025928^{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 163105
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 119680 0.27 5 7 0 50 0
r_arch 119680 0.27 1 7 0 6 0
r_os 119680 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 15296 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-14 2023-11-08 1841

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
size 0 1 1124358.31 1487385.92 355 27733 310317 2352303 5677952 ▇▁▂▁▁
ip_id 0 1 11311.04 21951.29 1 235 2889 11933 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-14 23:15:21 2023-11-08 12:45:09 103015

Variable type: Timespan

skim_variable n_missing complete_rate min max median n_unique
time 0 1 0 59 12H 6M 50S 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.90  -36.40  -11.24   26.95  820.08 

Coefficients:
                                                     Estimate Std. Error
(Intercept)                                        -1.848e+02  6.004e+01
date                                                1.130e-02  3.182e-03
lag(value, 1)                                       1.090e-01  2.315e-02
lag(value, 7)                                       8.856e-02  2.387e-02
lag(value, 14)                                      7.757e-02  2.382e-02
lag(value, 21)                                      8.167e-02  2.390e-02
lag(value, 28)                                      6.767e-02  2.380e-02
lag(value, 35)                                      5.296e-02  2.383e-02
lag(value, 42)                                      6.567e-02  2.397e-02
lag(value, 49)                                      6.141e-02  2.388e-02
month(date, label = TRUE).L                        -1.043e+01  4.997e+00
month(date, label = TRUE).Q                         7.413e-01  4.909e+00
month(date, label = TRUE).C                        -1.566e+01  4.940e+00
month(date, label = TRUE)^4                        -5.919e+00  4.925e+00
month(date, label = TRUE)^5                        -6.561e+00  4.887e+00
month(date, label = TRUE)^6                         1.330e+00  4.903e+00
month(date, label = TRUE)^7                        -4.435e+00  4.840e+00
month(date, label = TRUE)^8                        -4.022e+00  4.814e+00
month(date, label = TRUE)^9                         2.786e+00  4.827e+00
month(date, label = TRUE)^10                        9.308e-01  4.844e+00
month(date, label = TRUE)^11                       -4.072e+00  4.830e+00
fourier_vec(date, type = "sin", K = 1, period = 7) -1.116e+01  2.208e+00
fourier_vec(date, type = "cos", K = 1, period = 7)  7.305e+00  2.285e+00
                                                   t value Pr(>|t|)    
(Intercept)                                         -3.077 0.002120 ** 
date                                                 3.552 0.000392 ***
lag(value, 1)                                        4.709 2.68e-06 ***
lag(value, 7)                                        3.710 0.000214 ***
lag(value, 14)                                       3.257 0.001149 ** 
lag(value, 21)                                       3.416 0.000649 ***
lag(value, 28)                                       2.843 0.004523 ** 
lag(value, 35)                                       2.222 0.026421 *  
lag(value, 42)                                       2.740 0.006197 ** 
lag(value, 49)                                       2.572 0.010199 *  
month(date, label = TRUE).L                         -2.087 0.037007 *  
month(date, label = TRUE).Q                          0.151 0.879982    
month(date, label = TRUE).C                         -3.169 0.001553 ** 
month(date, label = TRUE)^4                         -1.202 0.229635    
month(date, label = TRUE)^5                         -1.343 0.179587    
month(date, label = TRUE)^6                          0.271 0.786180    
month(date, label = TRUE)^7                         -0.916 0.359627    
month(date, label = TRUE)^8                         -0.835 0.403638    
month(date, label = TRUE)^9                          0.577 0.563869    
month(date, label = TRUE)^10                         0.192 0.847630    
month(date, label = TRUE)^11                        -0.843 0.399349    
fourier_vec(date, type = "sin", K = 1, period = 7)  -5.056 4.73e-07 ***
fourier_vec(date, type = "cos", K = 1, period = 7)   3.196 0.001416 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 59.25 on 1769 degrees of freedom
  (49 observations deleted due to missingness)
Multiple R-squared:  0.2294,    Adjusted R-squared:  0.2198 
F-statistic: 23.93 on 22 and 1769 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 = 423.837625315235"
[1] "BEST method = 'lin' PATH MEMBER = c( 1 )"
[1] "BEST lin OBJECTIVE FUNCTION = 423.837625315235"
[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 = 107.004634711231"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 1 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 107.004634711231"
[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 = 247.900790186791"
[1] "BEST method = 'both' PATH MEMBER = c( 1 )"
[1] "BEST both OBJECTIVE FUNCTION = 247.900790186791"

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 = 23.4232449134407"
[1] "BEST method = 'lin' PATH MEMBER = c( 3 )"
[1] "BEST lin OBJECTIVE FUNCTION = 23.4232449134407"
[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 = 13.6721692244789"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 3 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 13.6721692244789"
[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 = 16.7292893798583"
[1] "BEST method = 'both' PATH MEMBER = c( 3 )"
[1] "BEST both OBJECTIVE FUNCTION = 16.7292893798583"

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( 2 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 357.990367175258"
[1] "BEST method = 'lin' PATH MEMBER = c( 2 )"
[1] "BEST lin OBJECTIVE FUNCTION = 357.990367175258"
[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 = 5.88513067948722"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 2 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 5.88513067948722"
[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 = 7.48265578371466"
[1] "BEST method = 'both' PATH MEMBER = c( 2 )"
[1] "BEST both OBJECTIVE FUNCTION = 7.48265578371466"

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 = 17.9263765210634"
[1] "BEST method = 'lin' PATH MEMBER = c( 19 )"
[1] "BEST lin OBJECTIVE FUNCTION = 17.9263765210634"
[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 = 13.9850168481418"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 19 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 13.9850168481418"
[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 = 17.5837536385415"
[1] "BEST method = 'both' PATH MEMBER = c( 19 )"
[1] "BEST both OBJECTIVE FUNCTION = 17.5837536385415"

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 = 9.29712183104311"
[1] "BEST method = 'lin' PATH MEMBER = c( 4 )"
[1] "BEST lin OBJECTIVE FUNCTION = 9.29712183104311"
[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.2883784774975"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 4 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 9.2883784774975"
[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 = 8.50357086525901"
[1] "BEST method = 'both' PATH MEMBER = c( 4 )"
[1] "BEST both OBJECTIVE FUNCTION = 8.50357086525901"

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.11415196738458"
[1] "BEST method = 'lin' PATH MEMBER = c( 23 )"
[1] "BEST lin OBJECTIVE FUNCTION = 8.11415196738458"
[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.73752266180947"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 23 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 5.73752266180947"
[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.683176355396"
[1] "BEST method = 'both' PATH MEMBER = c( 23 )"
[1] "BEST both OBJECTIVE FUNCTION = 5.683176355396"

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 = 162.988215442403"
[1] "BEST method = 'lin' PATH MEMBER = c( 1 )"
[1] "BEST lin OBJECTIVE FUNCTION = 162.988215442403"
[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 = 33.7071960258099"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 1 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 33.7071960258099"
[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 = 34.1699118765355"
[1] "BEST method = 'both' PATH MEMBER = c( 1 )"
[1] "BEST both OBJECTIVE FUNCTION = 34.1699118765355"

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 = 12.5443842203178"
[1] "BEST method = 'lin' PATH MEMBER = c( 7 )"
[1] "BEST lin OBJECTIVE FUNCTION = 12.5443842203178"
[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 = 10.8829543373614"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 7 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 10.8829543373614"
[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 = 12.3864044839651"
[1] "BEST method = 'both' PATH MEMBER = c( 7 )"
[1] "BEST both OBJECTIVE FUNCTION = 12.3864044839651"

[[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,833 × 50]> <tibble [28 × 50]> <split [1805|28]>
2 healthyR      <tibble [1,824 × 50]> <tibble [28 × 50]> <split [1796|28]>
3 healthyR.ts   <tibble [1,769 × 50]> <tibble [28 × 50]> <split [1741|28]>
4 healthyverse  <tibble [1,740 × 50]> <tibble [28 × 50]> <split [1712|28]>
5 healthyR.ai   <tibble [1,566 × 50]> <tibble [28 × 50]> <split [1538|28]>
6 TidyDensity   <tibble [1,417 × 50]> <tibble [28 × 50]> <split [1389|28]>
7 tidyAML       <tibble [1,024 × 50]> <tibble [28 × 50]> <split [996|28]> 
8 RandomWalker  <tibble [447 × 50]>   <tibble [28 × 50]> <split [419|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.6405336 109.74864 0.7309688 129.53379 0.7638890 0.0450898
healthyR.data 2 LM Test 0.6104739 185.07381 0.6966650 105.71054 0.7521770 0.0005239
healthyR.data 3 EARTH Test 0.6727750 274.14020 0.7677622 96.25974 0.8052706 0.0190628
healthyR.data 4 NNAR Test 0.6625972 249.08712 0.7561474 112.66758 0.7802426 0.0006441
healthyR 1 ARIMA Test 0.5709736 157.43893 0.7879975 141.05371 0.7146578 0.0065708
healthyR 2 LM Test 0.5620079 433.94519 0.7756240 110.84721 0.6845832 0.0515447
healthyR 3 EARTH Test 0.7766663 852.79396 1.0718729 102.51307 0.9519201 0.0432813
healthyR 4 NNAR Test 0.5552993 264.59067 0.7663655 125.46593 0.6625720 0.0852044
healthyR.ts 1 ARIMA Test 0.6059940 123.61115 0.7302510 139.92696 0.7585825 0.0300536
healthyR.ts 2 LM Test 0.7244076 180.77247 0.8729448 134.60775 0.8826946 0.0041798
healthyR.ts 3 EARTH Test 0.5433493 128.00634 0.6547612 100.61207 0.7170904 0.0006068
healthyR.ts 4 NNAR Test 0.7792081 193.97392 0.9389820 146.57874 0.9446083 0.0043421
healthyverse 1 ARIMA Test 0.7371031 88.73857 0.7556244 132.88478 0.9706330 0.0154141
healthyverse 2 LM Test 0.8041318 137.21844 0.8243373 121.14916 0.9553127 0.0000056
healthyverse 3 EARTH Test 0.7387231 86.95169 0.7572850 134.27604 0.9665230 NA
healthyverse 4 NNAR Test 0.7250974 116.86338 0.7433169 114.56132 0.9083967 0.0406100
healthyR.ai 1 ARIMA Test 0.8229141 102.41629 0.9402023 182.19284 0.9937573 0.1593240
healthyR.ai 2 LM Test 1.0635789 190.22607 1.2151685 152.91763 1.2835648 0.0880664
healthyR.ai 3 EARTH Test 2.4077687 864.57993 2.7509428 125.54800 2.8680473 0.2612615
healthyR.ai 4 NNAR Test 1.0403103 193.21271 1.1885835 159.48283 1.2415946 0.0314801
TidyDensity 1 ARIMA Test 1.0328558 228.74199 0.6930347 162.64268 1.1725809 0.1270217
TidyDensity 2 LM Test 0.9056677 136.71350 0.6076929 162.99855 1.0856311 0.0080809
TidyDensity 3 EARTH Test 0.8997841 129.61684 0.6037451 181.71804 1.0701766 0.0023764
TidyDensity 4 NNAR Test 1.0069810 163.37266 0.6756730 152.29781 1.1918485 0.0001570
tidyAML 1 ARIMA Test 0.5713255 112.73479 0.7336173 147.46575 0.7400101 0.0143055
tidyAML 2 LM Test 0.6397208 272.07826 0.8214412 137.83704 0.7888353 0.0243861
tidyAML 3 EARTH Test 0.6579586 282.71295 0.8448596 113.79097 0.7770538 0.1710669
tidyAML 4 NNAR Test 0.7122028 330.05564 0.9145125 130.99565 0.8327698 0.0000257
RandomWalker 1 ARIMA Test 0.7012012 178.10163 0.7231308 157.56714 0.7942708 0.2007504
RandomWalker 2 LM Test 0.7628786 163.95798 0.7867371 156.60547 0.9044253 0.0003460
RandomWalker 3 EARTH Test 0.8511978 215.84725 0.8778185 174.62093 0.9065964 0.0345232
RandomWalker 4 NNAR Test 0.8565578 222.85139 0.8833461 179.35241 0.9923136 0.0403869

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.610  185. 0.697  106. 0.752 5.24e-4
2 healthyR             4 NNAR        Test  0.555  265. 0.766  125. 0.663 8.52e-2
3 healthyR.ts          3 EARTH       Test  0.543  128. 0.655  101. 0.717 6.07e-4
4 healthyverse         4 NNAR        Test  0.725  117. 0.743  115. 0.908 4.06e-2
5 healthyR.ai          1 ARIMA       Test  0.823  102. 0.940  182. 0.994 1.59e-1
6 TidyDensity          3 EARTH       Test  0.900  130. 0.604  182. 1.07  2.38e-3
7 tidyAML              1 ARIMA       Test  0.571  113. 0.734  147. 0.740 1.43e-2
8 RandomWalker         1 ARIMA       Test  0.701  178. 0.723  158. 0.794 2.01e-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 [1805|28]> <mdl_tm_t [1 × 5]>
2 healthyR      <tibble>     <tibble>     <split [1796|28]> <mdl_tm_t [1 × 5]>
3 healthyR.ts   <tibble>     <tibble>     <split [1741|28]> <mdl_tm_t [1 × 5]>
4 healthyverse  <tibble>     <tibble>     <split [1712|28]> <mdl_tm_t [1 × 5]>
5 healthyR.ai   <tibble>     <tibble>     <split [1538|28]> <mdl_tm_t [1 × 5]>
6 TidyDensity   <tibble>     <tibble>     <split [1389|28]> <mdl_tm_t [1 × 5]>
7 tidyAML       <tibble>     <tibble>     <split [996|28]>  <mdl_tm_t [1 × 5]>
8 RandomWalker  <tibble>     <tibble>     <split [419|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")