healthyverse_tsa

Time Series Analysis, Modeling and Forecasting of the Healthyverse

Packages Steven P. Sanderson II, MPH - Date: 2026-03-02

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: 170,572
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-02-28 22:42:03, the file was birthed on: 2025-10-31 10:47:59.603742, and at report knit time is 2887.9 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 170572
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 126185 0.26 5 7 0 50 0
r_arch 126185 0.26 1 7 0 6 0
r_os 126185 0.26 7 19 0 24 0
package 0 1.00 7 13 0 8 0
version 0 1.00 5 17 0 63 0
country 15979 0.91 2 2 0 167 0

Variable type: Date

skim_variable n_missing complete_rate min max median n_unique
date 0 1 2020-11-23 2026-02-28 2023-12-18 1917

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
size 0 1 1127626.46 1482838.9 355 42213 323098 2348372 5677952 ▇▁▂▁▁
ip_id 0 1 11466.42 22865.6 1 198 2759 11772 299146 ▇▁▁▁▁

Variable type: POSIXct

skim_variable n_missing complete_rate min max median n_unique
date_time 0 1 2020-11-23 09:00:41 2026-02-28 22:42:03 2023-12-18 11:34:00 108246

Variable type: Timespan

skim_variable n_missing complete_rate min max median n_unique
time 0 1 0 59 47 60

We can see that the following columns are missing a lot of data and for us are most likely not useful anyways, so we will drop them c(r_version, r_arch, r_os)

Plots

Now lets take a look at a time-series plot of the total daily downloads by package. We will use a log scale and place a vertical line at each version release for each package.

[[1]]

[[2]]

[[3]]

[[4]]

[[5]]

[[6]]

[[7]]

[[8]]

Now lets take a look at some time series decomposition graphs.

[[1]]

[[2]]

[[3]]

[[4]]

[[5]]

[[6]]

[[7]]

[[8]]

[[1]]

[[2]]

[[3]]

[[4]]

[[5]]

[[6]]

[[7]]

[[8]]

Seasonal Diagnostics:

[[1]]

[[2]]

[[3]]

[[4]]

[[5]]

[[6]]

[[7]]

[[8]]

ACF and PACF Diagnostics:

[[1]]

[[2]]

[[3]]

[[4]]

[[5]]

[[6]]

[[7]]

[[8]]

Feature Engineering

Now that we have our basic data and a shot of what it looks like, let’s add some features to our data which can be very helpful in modeling. Lets start by making a tibble that is aggregated by the day and package, as we are going to be interested in forecasting the next 4 weeks or 28 days for each package. First lets get our base data.

Call:
stats::lm(formula = .formula, data = df)

Residuals:
    Min      1Q  Median      3Q     Max 
-148.77  -37.30  -11.45   27.52  826.18 

Coefficients:
                                                     Estimate Std. Error
(Intercept)                                        -1.741e+02  5.571e+01
date                                                1.080e-02  2.948e-03
lag(value, 1)                                       9.793e-02  2.276e-02
lag(value, 7)                                       8.529e-02  2.350e-02
lag(value, 14)                                      6.785e-02  2.337e-02
lag(value, 21)                                      8.713e-02  2.345e-02
lag(value, 28)                                      7.213e-02  2.339e-02
lag(value, 35)                                      4.648e-02  2.343e-02
lag(value, 42)                                      6.860e-02  2.360e-02
lag(value, 49)                                      6.785e-02  2.354e-02
month(date, label = TRUE).L                        -9.331e+00  4.759e+00
month(date, label = TRUE).Q                        -1.001e+00  4.737e+00
month(date, label = TRUE).C                        -1.436e+01  4.795e+00
month(date, label = TRUE)^4                        -8.160e+00  4.806e+00
month(date, label = TRUE)^5                        -5.129e+00  4.761e+00
month(date, label = TRUE)^6                        -1.679e-01  4.806e+00
month(date, label = TRUE)^7                        -3.649e+00  4.790e+00
month(date, label = TRUE)^8                        -4.725e+00  4.802e+00
month(date, label = TRUE)^9                         3.077e+00  4.835e+00
month(date, label = TRUE)^10                        1.017e+00  4.856e+00
month(date, label = TRUE)^11                       -4.225e+00  4.843e+00
fourier_vec(date, type = "sin", K = 1, period = 7) -1.103e+01  2.153e+00
fourier_vec(date, type = "cos", K = 1, period = 7)  7.517e+00  2.226e+00
                                                   t value Pr(>|t|)    
(Intercept)                                         -3.125 0.001808 ** 
date                                                 3.663 0.000256 ***
lag(value, 1)                                        4.303 1.77e-05 ***
lag(value, 7)                                        3.630 0.000291 ***
lag(value, 14)                                       2.903 0.003743 ** 
lag(value, 21)                                       3.716 0.000208 ***
lag(value, 28)                                       3.083 0.002076 ** 
lag(value, 35)                                       1.984 0.047402 *  
lag(value, 42)                                       2.906 0.003702 ** 
lag(value, 49)                                       2.882 0.003994 ** 
month(date, label = TRUE).L                         -1.961 0.050045 .  
month(date, label = TRUE).Q                         -0.211 0.832661    
month(date, label = TRUE).C                         -2.995 0.002783 ** 
month(date, label = TRUE)^4                         -1.698 0.089674 .  
month(date, label = TRUE)^5                         -1.077 0.281475    
month(date, label = TRUE)^6                         -0.035 0.972125    
month(date, label = TRUE)^7                         -0.762 0.446325    
month(date, label = TRUE)^8                         -0.984 0.325267    
month(date, label = TRUE)^9                          0.636 0.524595    
month(date, label = TRUE)^10                         0.209 0.834184    
month(date, label = TRUE)^11                        -0.872 0.383169    
fourier_vec(date, type = "sin", K = 1, period = 7)  -5.121 3.36e-07 ***
fourier_vec(date, type = "cos", K = 1, period = 7)   3.376 0.000750 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 59.42 on 1845 degrees of freedom
  (49 observations deleted due to missingness)
Multiple R-squared:  0.2199,    Adjusted R-squared:  0.2106 
F-statistic: 23.64 on 22 and 1845 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( 7 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 6.37356608194594"
[1] "BEST method = 'lin' PATH MEMBER = c( 7 )"
[1] "BEST lin OBJECTIVE FUNCTION = 6.37356608194594"
[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.276144093962"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 7 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 11.276144093962"
[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 = 10.4470270380261"
[1] "BEST method = 'both' PATH MEMBER = c( 7 )"
[1] "BEST both OBJECTIVE FUNCTION = 10.4470270380261"

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

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

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( 13 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 12.2490300944924"
[1] "BEST method = 'lin' PATH MEMBER = c( 13 )"
[1] "BEST lin OBJECTIVE FUNCTION = 12.2490300944924"
[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 = 8.4972392489969"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 13 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 8.4972392489969"
[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 = 11.1541695160947"
[1] "BEST method = 'both' PATH MEMBER = c( 13 )"
[1] "BEST both OBJECTIVE FUNCTION = 11.1541695160947"

Package: healthyverse
[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 = 7.6773721804901"
[1] "BEST method = 'lin' PATH MEMBER = c( 17 )"
[1] "BEST lin OBJECTIVE FUNCTION = 7.6773721804901"
[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 = 8.9302600030279"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 17 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 8.9302600030279"
[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 = 9.01954473383587"
[1] "BEST method = 'both' PATH MEMBER = c( 17 )"
[1] "BEST both OBJECTIVE FUNCTION = 9.01954473383587"

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

Package: tidyAML
[1] "CURRNET METHOD: lin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method =  'lin' , seasonal.factor =  c( 8 ) ...)"
[1] "CURRENT lin OBJECTIVE FUNCTION = 9.82983930807719"
[1] "BEST method = 'lin' PATH MEMBER = c( 8 )"
[1] "BEST lin OBJECTIVE FUNCTION = 9.82983930807719"
[1] "CURRNET METHOD: nonlin"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method =  'nonlin' , seasonal.factor =  c( 8 ) ...)"
[1] "CURRENT nonlin OBJECTIVE FUNCTION = 21.4039946090901"
[1] "BEST method = 'nonlin' PATH MEMBER = c( 8 )"
[1] "BEST nonlin OBJECTIVE FUNCTION = 21.4039946090901"
[1] "CURRNET METHOD: both"
[1] "COPY LATEST PARAMETERS DIRECTLY FOR NNS.ARMA() IF ERROR:"
[1] "NNS.ARMA(... method =  'both' , seasonal.factor =  c( 8 ) ...)"
[1] "CURRENT both OBJECTIVE FUNCTION = 14.6132765115356"
[1] "BEST method = 'both' PATH MEMBER = c( 8 )"
[1] "BEST both OBJECTIVE FUNCTION = 14.6132765115356"

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

[[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,907 × 50]> <tibble [28 × 50]> <split [1879|28]>
2 healthyR      <tibble [1,900 × 50]> <tibble [28 × 50]> <split [1872|28]>
3 healthyR.ts   <tibble [1,836 × 50]> <tibble [28 × 50]> <split [1808|28]>
4 healthyverse  <tibble [1,791 × 50]> <tibble [28 × 50]> <split [1763|28]>
5 healthyR.ai   <tibble [1,642 × 50]> <tibble [28 × 50]> <split [1614|28]>
6 TidyDensity   <tibble [1,493 × 50]> <tibble [28 × 50]> <split [1465|28]>
7 tidyAML       <tibble [1,099 × 50]> <tibble [28 × 50]> <split [1071|28]>
8 RandomWalker  <tibble [523 × 50]>   <tibble [28 × 50]> <split [495|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.7464457 126.83384 0.8118744 176.16792 0.8597226 0.0062263
healthyR.data 2 LM Test 0.7738882 144.84529 0.8417223 159.24295 0.8970849 0.0456018
healthyR.data 3 EARTH Test 0.8042315 144.28416 0.8747254 168.74712 0.9136580 0.0082615
healthyR.data 4 NNAR Test 0.8002208 151.50062 0.8703631 156.63128 0.9188907 0.0324949
healthyR 1 ARIMA Test 0.7391665 514.78484 0.7340433 147.30360 0.9018639 0.0450721
healthyR 2 LM Test 0.6926233 588.57388 0.6878227 138.20546 0.8667681 0.0859462
healthyR 3 EARTH Test 0.7606081 497.80087 0.7553363 144.85428 0.9308198 0.0173372
healthyR 4 NNAR Test 0.6892321 444.22245 0.6844550 135.84835 0.8524041 0.1110417
healthyR.ts 1 ARIMA Test 0.7075998 163.01846 0.6118779 159.24372 0.9318861 0.0553014
healthyR.ts 2 LM Test 0.7058630 129.00960 0.6103761 146.61655 0.9641982 0.0366540
healthyR.ts 3 EARTH Test 1.9020631 1238.96329 1.6447580 147.79698 2.2290678 0.0020431
healthyR.ts 4 NNAR Test 0.7556262 214.98267 0.6534074 148.33701 1.0055381 0.0204569
healthyverse 1 ARIMA Test 0.9835385 103.00542 1.2406634 96.95159 1.1261561 0.0227958
healthyverse 2 LM Test 1.0332069 153.19972 1.3033165 112.71273 1.1472893 0.1260281
healthyverse 3 EARTH Test 0.7132761 232.25465 0.8997467 57.71983 0.8960681 0.2016009
healthyverse 4 NNAR Test 1.1257794 162.67284 1.4200901 125.96312 1.2757376 0.0354386
healthyR.ai 1 ARIMA Test 0.6585147 164.66405 0.7538030 131.21949 0.8522950 0.0149852
healthyR.ai 2 LM Test 0.7138754 210.12491 0.8171745 154.45338 0.8912640 0.0854100
healthyR.ai 3 EARTH Test 0.7309128 164.65715 0.8366772 158.92415 0.9471818 0.0265064
healthyR.ai 4 NNAR Test 0.7168110 241.12514 0.8205349 147.96642 0.8988304 0.0716873
TidyDensity 1 ARIMA Test 1.1926282 121.59104 0.7518659 171.73820 1.2954543 0.0390283
TidyDensity 2 LM Test 1.0795443 133.30730 0.6805746 145.69753 1.2146105 0.0800296
TidyDensity 3 EARTH Test 1.1685448 116.10067 0.7366830 173.25895 1.2723721 0.0366156
TidyDensity 4 NNAR Test 1.0869431 111.65039 0.6852390 150.18878 1.2227333 0.0560070
tidyAML 1 ARIMA Test 0.7173749 209.10403 0.8631372 132.71737 0.8855874 0.0050701
tidyAML 2 LM Test 0.7371603 208.43836 0.8869428 150.82780 0.8933016 0.0115613
tidyAML 3 EARTH Test 0.7672693 229.83445 0.9231696 145.04370 0.9775633 0.1391832
tidyAML 4 NNAR Test 0.7984529 205.54039 0.9606892 152.48013 1.0152457 0.0487620
RandomWalker 1 ARIMA Test 0.7107075 96.99181 0.4574017 141.95665 0.8422758 0.4962586
RandomWalker 2 LM Test 0.9038988 119.80396 0.5817371 133.36005 1.1718189 0.0000148
RandomWalker 3 EARTH Test 0.8450776 89.18653 0.5438805 146.26421 1.0626170 0.0097714
RandomWalker 4 NNAR Test 1.0315002 152.86065 0.6638596 170.51097 1.1837782 0.0731797

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.746 127.  0.812 176.  0.860 0.00623
2 healthyR             4 NNAR        Test  0.689 444.  0.684 136.  0.852 0.111  
3 healthyR.ts          1 ARIMA       Test  0.708 163.  0.612 159.  0.932 0.0553 
4 healthyverse         3 EARTH       Test  0.713 232.  0.900  57.7 0.896 0.202  
5 healthyR.ai          1 ARIMA       Test  0.659 165.  0.754 131.  0.852 0.0150 
6 TidyDensity          2 LM          Test  1.08  133.  0.681 146.  1.21  0.0800 
7 tidyAML              1 ARIMA       Test  0.717 209.  0.863 133.  0.886 0.00507
8 RandomWalker         1 ARIMA       Test  0.711  97.0 0.457 142.  0.842 0.496  
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 [1879|28]> <mdl_tm_t [1 × 5]>
2 healthyR      <tibble>     <tibble>     <split [1872|28]> <mdl_tm_t [1 × 5]>
3 healthyR.ts   <tibble>     <tibble>     <split [1808|28]> <mdl_tm_t [1 × 5]>
4 healthyverse  <tibble>     <tibble>     <split [1763|28]> <mdl_tm_t [1 × 5]>
5 healthyR.ai   <tibble>     <tibble>     <split [1614|28]> <mdl_tm_t [1 × 5]>
6 TidyDensity   <tibble>     <tibble>     <split [1465|28]> <mdl_tm_t [1 × 5]>
7 tidyAML       <tibble>     <tibble>     <split [1071|28]> <mdl_tm_t [1 × 5]>
8 RandomWalker  <tibble>     <tibble>     <split [495|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")