Skip to content
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

forecast ILI #23

Merged
merged 29 commits into from
Dec 23, 2021
Merged

forecast ILI #23

merged 29 commits into from
Dec 23, 2021

Conversation

stephenturner
Copy link
Contributor

First cut at function to forecast ILI (fixes #7 when closed).

Example usage below. Object of interest in the returned list is ili_bound, which gives you a regular (non-tsibble) tibble with historical recorded data and horizon number of forecasted weeks into the future, bound in a tibble.

library(fiphde)

# Get data
ilidat <- get_cdc_ili(region="national", years=2010:lubridate::year(lubridate::today()))
#> Latest week_start / year / epiweek available:
#> 2021-12-05 / 2021 / 49

# Using data only from march 2020 forward
ilifor_2020 <- forecast_ili(ilidat, horizon=4L, location="US", trim_date="2020-03-01")
head(ilifor_2020$ili_bound)
#> # A tibble: 6 × 5
#>   location  year  week weighted_ili forecasted
#>   <chr>    <dbl> <dbl>        <dbl> <lgl>     
#> 1 US        2020    11         5.67 FALSE     
#> 2 US        2020    12         6.38 FALSE     
#> 3 US        2020    13         4.92 FALSE     
#> 4 US        2020    14         3.79 FALSE     
#> 5 US        2020    15         2.80 FALSE     
#> 6 US        2020    16         2.13 FALSE
tail(ilifor_2020$ili_bound)
#> # A tibble: 6 × 5
#>   location  year  week weighted_ili forecasted
#>   <chr>    <dbl> <dbl>        <dbl> <lgl>     
#> 1 US        2021    48         2.45 FALSE     
#> 2 US        2021    49         2.68 FALSE     
#> 3 US        2021    50         2.67 TRUE      
#> 4 US        2021    51         2.85 TRUE      
#> 5 US        2021    52         2.86 TRUE      
#> 6 US        2022     1         2.87 TRUE
ilifor_2020$ili_fit
#> # A mable: 1 x 2
#> # Key:     location [1]
#>   location          arima
#>   <chr>           <model>
#> 1 US       <ARIMA(0,2,3)>
ilifor_2020$ili_fit %>% focustools::extract_arima_params()
#> # A tibble: 3 × 22
#>   location abbreviation location_name .model     p     d     q     P     D     Q
#>   <chr>    <chr>        <chr>         <chr>  <int> <int> <int> <dbl> <dbl> <dbl>
#> 1 US       US           US            arima      0     2     3     0     0     0
#> 2 US       US           US            arima      0     2     3     0     0     0
#> 3 US       US           US            arima      0     2     3     0     0     0
#> # … with 12 more variables: constant <lgl>, period <dbl>, term <chr>,
#> #   estimate <dbl>, std.error <dbl>, statistic <dbl>, p.value <dbl>,
#> #   sigma2 <dbl>, log_lik <dbl>, AIC <dbl>, AICc <dbl>, BIC <dbl>
ilifor_2020$ili_forecast
#> # A fable: 4 x 5 [1W]
#> # Key:     location, .model [1]
#>   location .model    yweek  weighted_ili .mean
#>   <chr>    <chr>    <week>        <dist> <dbl>
#> 1 US       arima  2021 W50 N(2.7, 0.048)  2.67
#> 2 US       arima  2021 W51  N(2.9, 0.16)  2.85
#> 3 US       arima  2021 W52  N(2.9, 0.43)  2.86
#> 4 US       arima  2022 W01   N(2.9, 0.7)  2.87

# Using all the data we have (2010-forward, in this example)
ilifor_2010 <- forecast_ili(ilidat, horizon=4L, location="US")
head(ilifor_2010$ili_bound)
#> # A tibble: 6 × 5
#>   location  year  week weighted_ili forecasted
#>   <chr>    <dbl> <dbl>        <dbl> <lgl>     
#> 1 US        2010    40         1.11 FALSE     
#> 2 US        2010    41         1.24 FALSE     
#> 3 US        2010    42         1.26 FALSE     
#> 4 US        2010    43         1.26 FALSE     
#> 5 US        2010    44         1.43 FALSE     
#> 6 US        2010    45         1.52 FALSE
tail(ilifor_2010$ili_bound)
#> # A tibble: 6 × 5
#>   location  year  week weighted_ili forecasted
#>   <chr>    <dbl> <dbl>        <dbl> <lgl>     
#> 1 US        2021    48         2.45 FALSE     
#> 2 US        2021    49         2.68 FALSE     
#> 3 US        2021    50         2.77 TRUE      
#> 4 US        2021    51         2.76 TRUE      
#> 5 US        2021    52         2.72 TRUE      
#> 6 US        2022     1         2.69 TRUE

See help for ?fiphde::forecast_ili.

Currently only tested with location="US". Possible we could use location as a key and retrieve forecasted ILI for all states. Keeping this in draft until I get something tested there.

@vpnagraj
Copy link
Collaborator

@stephenturner fyi i just ported over the glm functions code from fluforce-init

also moved the example code from fluforce-init into scratch (saw you mention something similar in a commit on this PR so wanted you to have a heads up):

https://github.com/signaturescience/fiphde/blob/main/scratch/worfklow.R

all seems to be working by using glm functions now in fiphde package

@stephenturner
Copy link
Contributor Author

Nice, thanks for that. I'm running through the old code in fluforce-init and it's taking forever. I'm going to merge in main on this branch but it looks like we have a merge conflict I'll work out

@stephenturner
Copy link
Contributor Author

I updated workflow.R to use the package functions for forecasting ILI, and replaced the hard-coded values.

image

@stephenturner stephenturner mentioned this pull request Dec 21, 2021
@stephenturner
Copy link
Contributor Author

Brought in state-level forecasting in #31. Now using unweighted ILI, and removes states with too much missing data (now, only FL). When merged this will fix #27

@vpnagraj
Copy link
Collaborator

issue with the workflow.R script should be fixed now in 8d85d50

@vpnagraj
Copy link
Collaborator

oh and note column renaming (year=>epiyear, week=>epiweek ) in 8d85d50

@stephenturner
Copy link
Contributor Author

I'm a little unhappy with the ili forecast's sensitivity to trim dates. Left constrained, right unconstrained, different dates in rows.
image

@vpnagraj
Copy link
Collaborator

still some rough edges to polish with docs / code cleanup. but overall this looks good enough to merge to main to me. i did make one change to add a "param_space" named list arg to forecast_ili(). so when you have a chance @stephenturner take a look and confirm that is functional on your end. then lets go ahead and merge this branch to main.

@stephenturner
Copy link
Contributor Author

I think this works after a small change in e11a52c. One thing to note, you must specify all 6 params even if you only want to restrict one. But works, and rcmdchecks OK

@stephenturner stephenturner changed the title Draft: forecast ILI forecast ILI Dec 23, 2021
@stephenturner stephenturner merged commit d60ccd4 into main Dec 23, 2021
@stephenturner stephenturner deleted the 7-forcast-ili branch December 23, 2021 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

forecast ili data
2 participants