Skip to content

cv_revision_forecast fold selection can produce uneven amounts of test_lag data, including 0 #8

Description

@brookslogan

When rows are in a certain order, fold assignment can place uneven amounts of test_lag data into the different folds, even placing 0 rows in a fold. This seems to be in part due to assigning folds before filtering to test_lag. (There's also some row reordering and/or exclusion that seems to be happening somewhere that's making it hard to understand why exactly this is happening when the number of lags is not a multiple of the number of folds.)

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(vctrs)
#> 
#> Attaching package: 'vctrs'
#> The following object is masked from 'package:dplyr':
#> 
#>     data_frame
library(DelphiRF)
withr::with_tempdir({
  dir.create("receiving")
  tibble(report_date = as.Date("2020-01-01") + 7L * (0:21)) %>%
    reframe(
      .by = report_date,
      reference_date = seq(as.Date("2020-01-01"), report_date, by = "week")
    ) %>%
    transmute(
      reference_date,
      lag = as.numeric(report_date - reference_date)
    ) %>%
    filter(lag <= 7 * 10) %>%
    # filter(! (reference_date == unique(reference_date)[[6]] & lag == 0L)) %>%
    # ^ excluding this row makes the problem crop up first in fold 3
    # rather than fold 1
    mutate(value = 100/(1+exp(-lag)) + rnorm(n())) %>%
    DelphiRF::data_preprocessing(
      value_col = "value",
      refd_col = "reference_date",
      lag_col = "lag",
      ref_lag = 7L * 6L,
      temporal_resol = "weekly",
      smoothed = TRUE
    ) %>%
    DelphiRF::add_weights_related() %>%
    DelphiRF::cv_revision_forecast(
      test_lag = 7L,
      # ^ basing parameter selections solely on this lag.
      # TODO: revisit
      taus = (1:10 - 0.5)/10,
      smoothed_target = TRUE,
      # default lagged_term_list
      temporal_resol = "weekly",
      # default lambda, gamma candidates
      lag_pad_candidates = 0L,
      lp_solver = "glpk", # XXX
      geo = "network-available",
      indicator = "covid-net",
      signal = "n",
      geo_level = "national",
      # no training_end_date
      training_days = 90
      # default n_folds
    ) %>%
    {}
})
#> Error in `z[i, ]`:
#> ! subscript out of bounds

Created on 2026-07-20 with reprex v2.1.1

Potential workaround: add this before data_preprocessing:

    complete(lag, reference_date) %>%
    arrange(lag, reference_date) %>%

This ensures that each lag appears consecutively N times, once for each of the N reference dates, at least in the input to data_preprocessing, so unless whatever reordering/exclusion is happening in the above also messes this up and/or NA exclusion removes the completed row here and causes a problem, fold selection should still look something like 3:5, 1:5, 1:5, ..., 1:5, 1:5, 1:2 when filtering to the test_lag later on.

Potential fix: filter to test_lag before assigning folds.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions