cmu-delphi / epipredict

Tools for building predictive models in epidemiology.
https://cmu-delphi.github.io/epipredict/
Other
10 stars 10 forks source link

`quantile_reg()` with single level raises errors, at least with `method = "fnc"` #419

Closed brookslogan closed 1 week ago

brookslogan commented 3 weeks ago
library(epipredict)
#> Loading required package: epiprocess
#> Registered S3 method overwritten by 'tsibble':
#>   method               from 
#>   as_tibble.grouped_df dplyr
#> 
#> Attaching package: 'epiprocess'
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> Loading required package: parsnip
#> Registered S3 method overwritten by 'epipredict':
#>   method            from   
#>   print.step_naomit recipes

# Make a model spec that expects no predictor columns and outputs a fixed
# (rate) prediction. Based on combining two linear inequalities.
fixed_rate_prediction <- 2e-6
model_spec <- quantile_reg(quantile_levels = 0.5, method = "fnc") %>%
  set_engine(
    "rq",
    R = matrix(c(1, -1), 2, 1), r = c(1, -1) * fixed_rate_prediction,
    eps = fixed_rate_prediction * 1e-6 # prevent early stop
  )

# Here's the typical setup
dat1 <- tibble::tibble(geo_value = 1:2, time_value = 1, y = c(3 * 5, 7 * 11)) %>%
  as_epi_df()
pop1 <- tibble::tibble(geo_value = 1:2, population = c(5e6, 11e6))
ewf1 <- epi_workflow(
  epi_recipe(dat1) %>%
    step_population_scaling(y, df = pop1, df_pop_col = "population") %>%
    step_epi_ahead(y_scaled, ahead = 0),
  model_spec,
  frosting() %>%
    layer_predict() %>%
    layer_population_scaling(.pred, df = pop1, df_pop_col = "population", create_new = FALSE)
)
forecast(fit(ewf1, dat1)) %>%
  pivot_quantiles_wider(.pred)
#> Error in dist_quantiles(unname(as.list(x)), object$quantile_levels): Assertion on 'quantile_levels' failed: Must be of type 'numeric', not 'NULL'.

Created on 2024-10-24 with reprex v2.1.1

Bundling a fix in #418.