healthyverse_tsa

Time Series Analysis, Modeling and Forecasting of the Healthyverse Packages

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

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: 184,539
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-07-21 23:50:31, the file was birthed on: 2025-10-31 10:47:59.603742, and at report knit time is 6321.04 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 184539
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 138372 0.25 5 17 0 54 0
r_arch 138372 0.25 1 7 0 7 0
r_os 138372 0.25 7 33 0 33 0
package 0 1.00 7 13 0 8 0
version 0 1.00 5 17 0 63 0
country 18015 0.90 2 2 0 170 0

Variable type: Date

skim_variable n_missing complete_rate min max median n_unique
date 0 1 2020-11-23 2026-07-21 2024-02-14 2060

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
size 0 1 1133217.14 1473785.69 355 43661 325606 2334859 5677952 ▇▁▂▁▁
ip_id 0 1 12106.51 25530.05 1 157 2697 11959 429286 ▇▁▁▁▁

Variable type: POSIXct

skim_variable n_missing complete_rate min max median n_unique
date_time 0 1 2020-11-23 09:00:41 2026-07-21 23:50:31 2024-02-14 09:38:09 118216

Variable type: Timespan

skim_variable n_missing complete_rate min max median n_unique
time 0 1 0 59 12H 12M 23S 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 
-152.98  -38.41  -12.07   28.39  827.42 

Coefficients:
                                                     Estimate Std. Error
(Intercept)                                        -1.423e+02  4.978e+01
date                                                9.228e-03  2.631e-03
lag(value, 1)                                       9.301e-02  2.201e-02
lag(value, 7)                                       6.719e-02  2.260e-02
lag(value, 14)                                      7.002e-02  2.246e-02
lag(value, 21)                                      8.681e-02  2.255e-02
lag(value, 28)                                      7.905e-02  2.247e-02
lag(value, 35)                                      3.689e-02  2.252e-02
lag(value, 42)                                      6.213e-02  2.266e-02
lag(value, 49)                                      8.113e-02  2.266e-02
month(date, label = TRUE).L                        -8.484e+00  4.752e+00
month(date, label = TRUE).Q                         8.970e-01  4.670e+00
month(date, label = TRUE).C                        -1.559e+01  4.728e+00
month(date, label = TRUE)^4                        -9.181e+00  4.732e+00
month(date, label = TRUE)^5                        -4.505e+00  4.731e+00
month(date, label = TRUE)^6                        -7.166e-01  4.737e+00
month(date, label = TRUE)^7                        -3.324e+00  4.689e+00
month(date, label = TRUE)^8                        -4.130e+00  4.664e+00
month(date, label = TRUE)^9                         1.298e+00  4.667e+00
month(date, label = TRUE)^10                        7.561e-01  4.696e+00
month(date, label = TRUE)^11                       -9.325e-01  4.598e+00
fourier_vec(date, type = "sin", K = 1, period = 7) -1.074e+01  2.098e+00
fourier_vec(date, type = "cos", K = 1, period = 7)  7.684e+00  2.158e+00
                                                   t value Pr(>|t|)    
(Intercept)                                         -2.859 0.004293 ** 
date                                                 3.508 0.000462 ***
lag(value, 1)                                        4.225 2.49e-05 ***
lag(value, 7)                                        2.974 0.002977 ** 
lag(value, 14)                                       3.118 0.001849 ** 
lag(value, 21)                                       3.850 0.000122 ***
lag(value, 28)                                       3.518 0.000445 ***
lag(value, 35)                                       1.638 0.101657    
lag(value, 42)                                       2.741 0.006175 ** 
lag(value, 49)                                       3.581 0.000351 ***
month(date, label = TRUE).L                         -1.785 0.074372 .  
month(date, label = TRUE).Q                          0.192 0.847681    
month(date, label = TRUE).C                         -3.297 0.000993 ***
month(date, label = TRUE)^4                         -1.940 0.052515 .  
month(date, label = TRUE)^5                         -0.952 0.341109    
month(date, label = TRUE)^6                         -0.151 0.879777    
month(date, label = TRUE)^7                         -0.709 0.478506    
month(date, label = TRUE)^8                         -0.886 0.375951    
month(date, label = TRUE)^9                          0.278 0.780885    
month(date, label = TRUE)^10                         0.161 0.872106    
month(date, label = TRUE)^11                        -0.203 0.839316    
fourier_vec(date, type = "sin", K = 1, period = 7)  -5.120 3.36e-07 ***
fourier_vec(date, type = "cos", K = 1, period = 7)   3.561 0.000378 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 60.27 on 1988 degrees of freedom
  (49 observations deleted due to missingness)
Multiple R-squared:  0.1997,    Adjusted R-squared:  0.1908 
F-statistic: 22.55 on 22 and 1988 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( 21 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 6.36814853440202"
[1] "BEST method = 'lin' PATH MEMBER = c( 21 )"
[1] "BEST lin OBJECTIVE FUNCTION = 6.36814853440202"
[1] "CURRNET METHOD: nonlin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method =  'nonlin' , seasonal.factor =  c( 21 ) ...)"
[1] "CURRENT nonlin OBJECTIVE FUNCTION = 11.9735120024927"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 21 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 11.9735120024927"
[1] "CURRNET METHOD: both"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method =  'both' , seasonal.factor =  c( 21 ) ...)"
[1] "CURRENT both OBJECTIVE FUNCTION = 8.65489368389751"
[1] "BEST method = 'both' PATH MEMBER = c( 21 )"
[1] "BEST both OBJECTIVE FUNCTION = 8.65489368389751"

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

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

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

Package: healthyverse
[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 = 7.82010991065232"
[1] "BEST method = 'lin' PATH MEMBER = c( 10 )"
[1] "BEST lin OBJECTIVE FUNCTION = 7.82010991065232"
[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 = 16.3257001717509"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 10 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 16.3257001717509"
[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 = 9.33336831632444"
[1] "BEST method = 'both' PATH MEMBER = c( 10 )"
[1] "BEST both OBJECTIVE FUNCTION = 9.33336831632444"

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

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

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

[[1]]
NULL

[[2]]
NULL

[[3]]
NULL

[[4]]
NULL

[[5]]
NULL

[[6]]
NULL

[[7]]
NULL

[[8]]
NULL

Pre-Processing

Now we are going to do some basic pre-processing.

data_padded_tbl <- base_data %>%
  pad_by_time(
    .date_var  = date,
    .pad_value = 0
  )

# Get log interval and standardization parameters
log_params  <- liv(data_padded_tbl$value, limit_lower = 0, offset = 1, silent = TRUE)
limit_lower <- log_params$limit_lower
limit_upper <- log_params$limit_upper
offset      <- log_params$offset

data_liv_tbl <- data_padded_tbl %>%
  # Get log interval transform
  mutate(value_trans = liv(value, limit_lower = 0, offset = 1, silent = TRUE)$log_scaled)

# Get Standardization Params
std_params <- standard_vec(data_liv_tbl$value_trans, silent = TRUE)
std_mean   <- std_params$mean
std_sd     <- std_params$sd

data_transformed_tbl <- data_liv_tbl %>%
  group_by(package) %>%
  # get standardization
  mutate(value_trans = standard_vec(value_trans, silent = TRUE)$standard_scaled) %>%
  tk_augment_fourier(
    .date_var = date,
    .periods  = c(7, 14, 30, 90, 180),
    .K        = 2
  ) %>%
  tk_augment_timeseries_signature(
    .date_var = date
  ) %>%
  ungroup() %>%
  select(-c(value, -year.iso))

Since this is panel data we can follow one of two different modeling strategies. We can search for a global model in the panel data or we can use nested forecasting finding the best model for each of the time series. Since we only have 5 panels, we will use nested forecasting.

To do this we will use the nest_timeseries and split_nested_timeseries functions to create a nested tibble.

horizon <- 4*7

nested_data_tbl <- data_transformed_tbl %>%

    # 0. Filter out column where package is NA
    filter(!is.na(package)) %>%
    
    # 1. Extending: We'll predict n days into the future.
    extend_timeseries(
        .id_var        = package,
        .date_var      = date,
        .length_future = horizon
    ) %>%
    
    # 2. Nesting: We'll group by id, and create a future dataset
    #    that forecasts n days of extended data and
    #    an actual dataset that contains n*2 days
    nest_timeseries(
        .id_var        = package,
        .length_future = horizon
        #.length_actual = horizon*2
    ) %>%
    
   # 3. Splitting: We'll take the actual data and create splits
   #    for accuracy and confidence interval estimation of n das (test)
   #    and the rest is training data
    split_nested_timeseries(
        .length_test = horizon
    )

nested_data_tbl
# A tibble: 8 × 4
  package       .actual_data          .future_data       .splits          
  <fct>         <list>                <list>             <list>           
1 healthyR.data <tibble [2,048 × 50]> <tibble [28 × 50]> <split [2020|28]>
2 healthyR      <tibble [2,042 × 50]> <tibble [28 × 50]> <split [2014|28]>
3 healthyR.ts   <tibble [1,978 × 50]> <tibble [28 × 50]> <split [1950|28]>
4 healthyverse  <tibble [1,878 × 50]> <tibble [28 × 50]> <split [1850|28]>
5 healthyR.ai   <tibble [1,783 × 50]> <tibble [28 × 50]> <split [1755|28]>
6 TidyDensity   <tibble [1,636 × 50]> <tibble [28 × 50]> <split [1608|28]>
7 tidyAML       <tibble [1,240 × 50]> <tibble [28 × 50]> <split [1212|28]>
8 RandomWalker  <tibble [664 × 50]>   <tibble [28 × 50]> <split [636|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.7888585 89.06619 0.9314941 163.40479 0.9531812 0.0221274
healthyR.data 2 LM Test 0.7618324 128.73246 0.8995815 143.35205 0.8873318 0.0123283
healthyR.data 3 EARTH Test 0.7839696 107.87955 0.9257213 171.31551 0.9121673 0.1834255
healthyR.data 4 NNAR Test 0.6971001 108.73356 0.8231447 134.90007 0.8403623 0.1132506
healthyR 1 ARIMA Test 0.7396618 201.17751 1.0296096 121.76302 0.9299677 0.1261359
healthyR 2 LM Test 0.8751429 178.92508 1.2181994 159.40285 1.0412698 0.0194374
healthyR 3 EARTH Test 5.0290070 3851.65242 7.0003804 161.63656 5.4649724 0.1142701
healthyR 4 NNAR Test 0.8053616 150.92858 1.1210638 144.54163 0.9653820 0.1647437
healthyR.ts 1 ARIMA Test 0.8920327 175.61497 1.1123468 182.50379 1.1138183 0.0022580
healthyR.ts 2 LM Test 0.9046183 496.22772 1.1280408 160.41763 1.1647390 0.0055431
healthyR.ts 3 EARTH Test 1.5113605 1057.26426 1.8846360 179.37789 1.7348687 0.0827803
healthyR.ts 4 NNAR Test 0.9021510 551.64662 1.1249640 167.12731 1.1385314 0.0045473
healthyverse 1 ARIMA Test 0.5492663 87.49420 0.8167106 45.44368 0.6704319 0.0613849
healthyverse 2 LM Test 0.8928897 66.75835 1.3276485 85.46715 1.0266848 0.0127254
healthyverse 3 EARTH Test 0.5356991 100.99786 0.7965375 43.02700 0.7363193 0.0870788
healthyverse 4 NNAR Test 0.9587060 68.99313 1.4255116 90.84927 1.1023779 0.0000187
healthyR.ai 1 ARIMA Test 0.6976092 82.52778 1.0273334 128.67748 0.9019774 0.0006089
healthyR.ai 2 LM Test 0.7112766 104.14830 1.0474605 116.19825 0.9100444 0.0224145
healthyR.ai 3 EARTH Test 0.8635459 120.54661 1.2716997 193.35800 1.0245289 0.0396521
healthyR.ai 4 NNAR Test 0.7572702 112.96090 1.1151930 146.28154 0.9788257 0.0144130
TidyDensity 1 ARIMA Test 1.0009376 195.92429 1.1844462 163.33391 1.1229610 0.0133943
TidyDensity 2 LM Test 1.0824009 250.77526 1.2808447 171.11225 1.1950585 0.0070672
TidyDensity 3 EARTH Test 1.0827829 226.82132 1.2812967 168.64921 1.1880744 0.1090895
TidyDensity 4 NNAR Test 1.1279274 285.65708 1.3347179 170.48139 1.2303575 0.0011483
tidyAML 1 ARIMA Test 0.6717428 122.90178 0.9348115 175.90026 0.8908308 0.0024108
tidyAML 2 LM Test 0.5453803 217.69115 0.7589628 123.38510 0.7168719 0.3114899
tidyAML 3 EARTH Test 0.8873507 401.57762 1.2348560 160.73085 1.0456612 0.0134403
tidyAML 4 NNAR Test 0.6409848 259.86085 0.8920080 145.92108 0.8099703 0.1303754
RandomWalker 1 ARIMA Test 0.8836147 128.15017 0.9311119 167.48151 1.0512404 0.0000259
RandomWalker 2 LM Test 0.8986807 175.24802 0.9469878 182.08453 1.0282596 0.0034356
RandomWalker 3 EARTH Test 0.8051439 99.87184 0.8484231 184.83193 0.9567502 0.1135821
RandomWalker 4 NNAR Test 1.0044807 218.83176 1.0584749 180.38876 1.1272290 0.0020915

Plot Models

nested_modeltime_tbl %>%
  extract_nested_test_forecast() %>%
  group_by(package) %>%
  filter_by_time(.date_var = .index, .start_date = max(.index) - 60) %>%
  ungroup() %>%
  plot_modeltime_forecast(
    .interactive = FALSE,
    .conf_interval_show  = FALSE,
    .facet_scales = "free"
  ) +
  theme_minimal() +
  facet_wrap(~ package, nrow = 3) +
  theme(legend.position = "bottom")

Best Model

best_nested_modeltime_tbl <- nested_modeltime_tbl %>%
  modeltime_nested_select_best(
    metric = "rmse",
    minimize = TRUE,
    filter_test_forecasts = TRUE
  )

best_nested_modeltime_tbl %>%
  extract_nested_best_model_report()
# Nested Modeltime Table
  

# A tibble: 8 × 10
  package      .model_id .model_desc .type   mae  mape  mase smape  rmse     rsq
  <fct>            <int> <chr>       <chr> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>
1 healthyR.da…         4 NNAR        Test  0.697 109.  0.823 135.  0.840 1.13e-1
2 healthyR             1 ARIMA       Test  0.740 201.  1.03  122.  0.930 1.26e-1
3 healthyR.ts          1 ARIMA       Test  0.892 176.  1.11  183.  1.11  2.26e-3
4 healthyverse         1 ARIMA       Test  0.549  87.5 0.817  45.4 0.670 6.14e-2
5 healthyR.ai          1 ARIMA       Test  0.698  82.5 1.03  129.  0.902 6.09e-4
6 TidyDensity          1 ARIMA       Test  1.00  196.  1.18  163.  1.12  1.34e-2
7 tidyAML              2 LM          Test  0.545 218.  0.759 123.  0.717 3.11e-1
8 RandomWalker         3 EARTH       Test  0.805  99.9 0.848 185.  0.957 1.14e-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 [2020|28]> <mdl_tm_t [1 × 5]>
2 healthyR      <tibble>     <tibble>     <split [2014|28]> <mdl_tm_t [1 × 5]>
3 healthyR.ts   <tibble>     <tibble>     <split [1950|28]> <mdl_tm_t [1 × 5]>
4 healthyverse  <tibble>     <tibble>     <split [1850|28]> <mdl_tm_t [1 × 5]>
5 healthyR.ai   <tibble>     <tibble>     <split [1755|28]> <mdl_tm_t [1 × 5]>
6 TidyDensity   <tibble>     <tibble>     <split [1608|28]> <mdl_tm_t [1 × 5]>
7 tidyAML       <tibble>     <tibble>     <split [1212|28]> <mdl_tm_t [1 × 5]>
8 RandomWalker  <tibble>     <tibble>     <split [636|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")