Skip to content

Flatline residuals in tidymodels framework are wrong #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dajmcdon opened this issue Jun 9, 2023 · 0 comments · Fixed by #197 or #200
Closed

Flatline residuals in tidymodels framework are wrong #196

dajmcdon opened this issue Jun 9, 2023 · 0 comments · Fixed by #197 or #200
Labels
bug Something isn't working

Comments

@dajmcdon
Copy link
Contributor

dajmcdon commented Jun 9, 2023

Below, the .pred column should be the same (it is), but the residuals should be different.

library(epipredict)
library(purrr)
library(dplyr)
library(tidyr)

jhu <- case_death_rate_subset %>% 
  filter(time_value >= "2021-11-01", geo_value %in% c("pa", "ca", "ny"))

f7 <- flatline_forecaster(jhu, "death_rate", flatline_args_list(ahead = 7))
f14 <- flatline_forecaster(jhu, "death_rate", flatline_args_list(ahead = 14))

f7$predictions %>%
  mutate(q = nested_quantiles(.pred_distn)) %>% 
  unnest(q) %>% 
  pivot_wider(names_from = tau, values_from = q) %>%
  select(geo_value:.pred, target_date, `0.05`, `0.95`)
#> # A tibble: 3 × 6
#>   geo_value time_value .pred target_date `0.05` `0.95`
#>   <chr>     <date>     <dbl> <date>       <dbl>  <dbl>
#> 1 ca        2021-12-31 0.142 2022-01-07   0      0.392
#> 2 ny        2021-12-31 0.422 2022-01-07   0.172  0.672
#> 3 pa        2021-12-31 0.732 2022-01-07   0.482  0.982

f14$predictions %>%
  mutate(q = nested_quantiles(.pred_distn)) %>% 
  unnest(q) %>% 
  pivot_wider(names_from = tau, values_from = q) %>%
  select(geo_value:.pred, target_date, `0.05`, `0.95`)
#> # A tibble: 3 × 6
#>   geo_value time_value .pred target_date `0.05` `0.95`
#>   <chr>     <date>     <dbl> <date>       <dbl>  <dbl>
#> 1 ca        2021-12-31 0.142 2022-01-14   0      0.392
#> 2 ny        2021-12-31 0.422 2022-01-14   0.172  0.672
#> 3 pa        2021-12-31 0.732 2022-01-14   0.482  0.982

Created on 2023-06-09 with reprex v2.0.2

Further tests suggest it is happening deep in the tidymodels processing of the workflow. It can't be replicated by calling flatline() directly.

jhu1 <- jhu %>% mutate(ld7 = lead(death_rate, 7), ld14 = lead(death_rate, 14))
f7a <- flatline(ld7 ~ geo_value + death_rate, jhu1)
f14a <- flatline(ld14 ~ geo_value + death_rate, jhu1)

quantile(residuals(f7a)$.resid, probs = c(0.05, 0.95), na.rm = TRUE)
#>         5%        95% 
#> -0.6987016  0.5442913
quantile(residuals(f14a)$.resid, probs = c(0.05, 0.95), na.rm = TRUE)
#>         5%        95% 
#> -0.5408296  0.7069717

Created on 2023-06-09 with reprex v2.0.2

@dajmcdon dajmcdon added the bug Something isn't working label Jun 9, 2023
@dajmcdon dajmcdon mentioned this issue Jun 10, 2023
@dajmcdon dajmcdon linked a pull request Jun 16, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant