Skip to contents

Loads examples of nowcasts produce using example scripts. Used to streamline examples, in package tests and to enable users to explore package functionality without needing to install cmdstanr.

Usage

enw_example(
  type = c("nowcast", "preprocessed_observations", "observations", "script")
)

Arguments

type

A character string indicating the example to load. Supported options are

Value

Depending on type, a data.table of the requested output OR the file name(s) to generate these outputs (type = "script")

See also

Package data sets germany_covid19_hosp

Examples

# Load the nowcast
enw_example(type = "nowcast")
#>                    obs          new_confirm              latest
#> 1: <data.table[671x9]> <data.table[630x11]> <data.table[41x10]>
#>     missing_reference  reporting_triangle      metareference
#> 1: <data.table[41x6]> <data.table[41x22]> <data.table[41x9]>
#>             metareport          metadelay time snapshots by groups max_delay
#> 1: <data.table[60x12]> <data.table[20x4]>   41        41         1        20
#>      max_date               fit       data  fit_args samples max_rhat
#> 1: 2021-08-22 <CmdStanMCMC[32]> <list[94]> <list[6]>    1000     1.02
#>    divergent_transitions per_divergent_transitions max_treedepth
#> 1:                     0                         0             8
#>    no_at_max_treedepth per_at_max_treedepth run_time
#> 1:                 198                0.198    103.8

# Load the preprocessed observations
enw_example(type = "preprocessed_observations")
#>                    obs          new_confirm              latest
#> 1: <data.table[671x9]> <data.table[630x11]> <data.table[41x10]>
#>     missing_reference  reporting_triangle      metareference
#> 1: <data.table[41x6]> <data.table[41x22]> <data.table[41x9]>
#>             metareport          metadelay time snapshots by groups max_delay
#> 1: <data.table[60x12]> <data.table[20x4]>   41        41         1        20
#>      max_date
#> 1: 2021-08-22

# Load the latest observations
enw_example(type = "observations")
#>     reference_date location age_group confirm report_date
#>  1:     2021-08-02       DE       00+      59  2021-10-01
#>  2:     2021-08-03       DE       00+     156  2021-10-01
#>  3:     2021-08-04       DE       00+     183  2021-10-01
#>  4:     2021-08-05       DE       00+     147  2021-10-01
#>  5:     2021-08-06       DE       00+     155  2021-10-01
#>  6:     2021-08-07       DE       00+     159  2021-10-01
#>  7:     2021-08-08       DE       00+     119  2021-10-01
#>  8:     2021-08-09       DE       00+      65  2021-10-01
#>  9:     2021-08-10       DE       00+     204  2021-10-01
#> 10:     2021-08-11       DE       00+     275  2021-10-01
#> 11:     2021-08-12       DE       00+     273  2021-10-01
#> 12:     2021-08-13       DE       00+     270  2021-10-01
#> 13:     2021-08-14       DE       00+     262  2021-10-01
#> 14:     2021-08-15       DE       00+     192  2021-10-01
#> 15:     2021-08-16       DE       00+     140  2021-10-01
#> 16:     2021-08-17       DE       00+     323  2021-10-01
#> 17:     2021-08-18       DE       00+     409  2021-10-01
#> 18:     2021-08-19       DE       00+     370  2021-10-01
#> 19:     2021-08-20       DE       00+     361  2021-10-01
#> 20:     2021-08-21       DE       00+     339  2021-10-01
#> 21:     2021-08-22       DE       00+     258  2021-10-01
#>     reference_date location age_group confirm report_date

# Load the script used to generate these examples
# Optionally source this script to regenerate the example
readLines(enw_example(type = "script"))
#>  [1] "# Load epinowcast and data.table"                                                    
#>  [2] "library(epinowcast)"                                                                 
#>  [3] "library(data.table)"                                                                 
#>  [4] ""                                                                                    
#>  [5] "# Load and filter germany hospitalisations"                                          
#>  [6] "nat_germany_hosp <- germany_covid19_hosp[location == \"DE\"][age_group %in% \"00+\"]"
#>  [7] "nat_germany_hosp <- enw_filter_report_dates("                                        
#>  [8] "  nat_germany_hosp,"                                                                 
#>  [9] "  latest_date = \"2021-10-01\""                                                      
#> [10] ")"                                                                                   
#> [11] ""                                                                                    
#> [12] "# Make sure observations are complete"                                               
#> [13] "nat_germany_hosp <- enw_complete_dates("                                             
#> [14] "  nat_germany_hosp,"                                                                 
#> [15] "  by = c(\"location\", \"age_group\")"                                               
#> [16] ")"                                                                                   
#> [17] ""                                                                                    
#> [18] "# Make a retrospective dataset"                                                      
#> [19] "retro_nat_germany <- enw_filter_report_dates("                                       
#> [20] "  nat_germany_hosp,"                                                                 
#> [21] "  remove_days = 40"                                                                  
#> [22] ")"                                                                                   
#> [23] "retro_nat_germany <- enw_filter_reference_dates("                                    
#> [24] "  retro_nat_germany,"                                                                
#> [25] "  include_days = 40"                                                                 
#> [26] ")"                                                                                   
#> [27] ""                                                                                    
#> [28] "# Get latest observations for the same time period"                                  
#> [29] "latest_obs <- enw_latest_data(nat_germany_hosp)"                                     
#> [30] "latest_obs <- enw_filter_reference_dates("                                           
#> [31] "  latest_obs,"                                                                       
#> [32] "  remove_days = 40, include_days = 20"                                               
#> [33] ")"                                                                                   
#> [34] ""                                                                                    
#> [35] "# Preprocess observations"                                                           
#> [36] "pobs <- enw_preprocess_data(retro_nat_germany, max_delay = 20)"                      
#> [37] ""                                                                                    
#> [38] "# Reference date model"                                                              
#> [39] "reference_module <- enw_reference(~1, data = pobs)"                                  
#> [40] ""                                                                                    
#> [41] "# Report date model"                                                                 
#> [42] "report_module <- enw_report(~ (1 | day_of_week), data = pobs)"                       
#> [43] ""                                                                                    
#> [44] "# Compile nowcasting model using multi-threading"                                    
#> [45] "model <- enw_model(threads = TRUE)"                                                  
#> [46] ""                                                                                    
#> [47] "# Fit nowcast model and produce a nowcast"                                           
#> [48] "# Note that we have reduced samples for this example to reduce runtimes"             
#> [49] "nowcast <- epinowcast(pobs,"                                                         
#> [50] "  reference = reference_module,"                                                     
#> [51] "  report = report_module,"                                                           
#> [52] "  fit = enw_fit_opts("                                                               
#> [53] "    save_warmup = FALSE, pp = TRUE,"                                                 
#> [54] "    chains = 2, threads_per_chain = 2,"                                              
#> [55] "    iter_warmup = 500, iter_sampling = 500"                                          
#> [56] "  ),"                                                                                
#> [57] "  model = model"                                                                     
#> [58] ")"