Skip to content

Instantly share code, notes, and snippets.

@seabbs
Last active August 21, 2020 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seabbs/07035fa4019a0c0117e28f9037593f28 to your computer and use it in GitHub Desktop.
Save seabbs/07035fa4019a0c0117e28f9037593f28 to your computer and use it in GitHub Desktop.
Evaluates Rt over two fixed time points with Rt assumed to vary based on a random walk in-between the two time points and after a period of time.
library(EpiNow2)
library(data.table)
# Get example case counts
reported_cases <- EpiNow2::example_confirmed[1:60]
# Add a dummy breakpoint (used only when optionally estimating breakpoints)
reported_cases <- reported_cases[, breakpoint := data.table::fifelse((date >= as.Date("2020-03-16") & date <= as.Date("2020-03-29")) | date >= as.Date("2020-04-11"),
1, 0)]
# Set up example generation time
generation_time <- list(mean = EpiNow2::covid_generation_times[1, ]$mean,
mean_sd = EpiNow2::covid_generation_times[1, ]$mean_sd,
sd = EpiNow2::covid_generation_times[1, ]$sd,
sd_sd = EpiNow2::covid_generation_times[1, ]$sd_sd,
max = 30)
# Set delays between infection and case report
# (any number of delays can be specifed here)
incubation_period <- list(mean = EpiNow2::covid_incubation_period[1, ]$mean,
mean_sd = EpiNow2::covid_incubation_period[1, ]$mean_sd,
sd = EpiNow2::covid_incubation_period[1, ]$sd,
sd_sd = EpiNow2::covid_incubation_period[1, ]$sd_sd,
max = 30)
reporting_delay <- list(mean = log(5),
mean_sd = log(2),
sd = log(2),
sd_sd = log(1.5),
max = 30)
# Run model with default settings
def <- estimate_infections(reported_cases, family = "negbin",
generation_time = generation_time,
delays = list(incubation_period, reporting_delay),
samples = 1000, warmup = 200, cores = 4,
horizon = 0, chains = 4, estimate_rt = TRUE,
verbose = TRUE, return_fit = TRUE)
# Plot output
plots <- report_plots(summarised_estimates = def$summarised,
reported = reported_cases)
plots$summary
# Run model with breakpoints but otherwise static Rt
# This formulation may increase the apparent effect of the breakpoint but needs to be tested using
# model fit criteria (i.e LFO).
fbkp <- estimate_infections(reported_cases, family = "negbin",
generation_time = generation_time,
delays = list(incubation_period, reporting_delay),
samples = 1000, warmup = 200, cores = 4,
chains = 4, estimate_breakpoints = TRUE, fixed = TRUE, horizon = 0,
verbose = TRUE, return_fit = TRUE)
# Plot output
plots <- report_plots(summarised_estimates = fbkp$summarised,
reported = reported_cases)
plots$summary
# Pull out breakpoint summary
fbkp$summarised[variable == "breakpoints"]
# Pull out R summary
fbkp$summarised[variable == "R"]
@seabbs
Copy link
Author

seabbs commented Aug 21, 2020

Default smooth variation over time

Screenshot 2020-08-21 at 18 18 03

Fixed, smooth intervention period, fixed (with a breakpoint after intervention defined as over)

Screenshot 2020-08-21 at 18 17 54

@seabbs
Copy link
Author

seabbs commented Aug 21, 2020

Breakpoints

> fbkp$summarised[variable == "breakpoints"]
    date    variable strat type    bottom       top     lower     upper    median      mean         sd
 1: <NA> breakpoints     1 <NA> 0.7184323 0.9448626 0.7542409 0.8546214 0.8229805 0.8284250 0.07134650
 2: <NA> breakpoints     2 <NA> 0.7299843 0.9809953 0.7874522 0.8960190 0.8611210 0.8616640 0.07796272
 3: <NA> breakpoints     3 <NA> 0.7671999 1.0261384 0.8150642 0.9189434 0.8848340 0.8896843 0.07939538
 4: <NA> breakpoints     4 <NA> 0.7778229 1.0612418 0.8363687 0.9461817 0.9111816 0.9162515 0.08657045
 5: <NA> breakpoints     5 <NA> 0.7871807 1.0858288 0.8780703 1.0025742 0.9318813 0.9398570 0.09230524
 6: <NA> breakpoints     6 <NA> 0.7961855 1.1002891 0.8795017 1.0014766 0.9501674 0.9545234 0.09397374
 7: <NA> breakpoints     7 <NA> 0.8145902 1.0933885 0.9000130 1.0101090 0.9674788 0.9712042 0.08529753
 8: <NA> breakpoints     8 <NA> 0.8349446 1.1198205 0.9327519 1.0489880 0.9798645 0.9805619 0.08870146
 9: <NA> breakpoints     9 <NA> 0.8482785 1.1476188 0.9488036 1.0766093 1.0049255 1.0040295 0.09407015
10: <NA> breakpoints    10 <NA> 0.8521385 1.1524926 0.9266578 1.0473304 1.0060516 1.0123790 0.09315835
11: <NA> breakpoints    11 <NA> 0.8639003 1.1735322 0.9431898 1.0689608 1.0108137 1.0181225 0.09710003
12: <NA> breakpoints    12 <NA> 0.8821359 1.1731399 0.9710531 1.0849796 1.0200879 1.0242674 0.09143071
13: <NA> breakpoints    13 <NA> 0.8834980 1.1755286 0.9467024 1.0628853 1.0231593 1.0282019 0.09146208
14: <NA> breakpoints    14 <NA> 0.8813254 1.1873960 0.9776351 1.0875547 1.0321665 1.0367937 0.09238645
15: <NA> breakpoints    15 <NA> 0.8489028 1.1608301 0.9215926 1.0447059 0.9899859 0.9986267 0.09445889

Rt estimates:

> fbkp$summarised[variable == "R"]
          date variable strat                           type    bottom       top     lower     upper    median      mean         sd
 1: 2020-02-22        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
 2: 2020-02-23        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
 3: 2020-02-24        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
 4: 2020-02-25        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
 5: 2020-02-26        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
 6: 2020-02-27        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
 7: 2020-02-28        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
 8: 2020-02-29        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
 9: 2020-03-01        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
10: 2020-03-02        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
11: 2020-03-03        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
12: 2020-03-04        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
13: 2020-03-05        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
14: 2020-03-06        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
15: 2020-03-07        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
16: 2020-03-08        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
17: 2020-03-09        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
18: 2020-03-10        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
19: 2020-03-11        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
20: 2020-03-12        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
21: 2020-03-13        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
22: 2020-03-14        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
23: 2020-03-15        R  <NA>                       estimate 1.6229954 1.7049730 1.6582797 1.6922513 1.6703114 1.6691387 0.02523126
24: 2020-03-16        R  <NA>                       estimate 1.1913740 1.5491442 1.2900082 1.4491832 1.3755092 1.3821003 0.11295550
25: 2020-03-17        R  <NA>                       estimate 0.9985109 1.3554728 1.1033569 1.2562641 1.1808398 1.1873777 0.11227383
26: 2020-03-18        R  <NA>                       estimate 0.8653293 1.2376494 0.9591665 1.1075643 1.0457497 1.0536002 0.11332290
27: 2020-03-19        R  <NA>                       estimate 0.7851320 1.1359600 0.8797646 1.0202230 0.9550124 0.9615575 0.10753577
28: 2020-03-20        R  <NA>                       estimate 0.7412762 1.0630038 0.8313083 0.9617075 0.8937357 0.8993709 0.10041219
29: 2020-03-21        R  <NA>                       estimate 0.7038559 1.0126346 0.7803446 0.9039182 0.8438652 0.8544923 0.09747694
30: 2020-03-22        R  <NA>                       estimate 0.6764468 0.9748821 0.7549331 0.8769790 0.8189115 0.8265666 0.09244133
31: 2020-03-23        R  <NA>                       estimate 0.6645458 0.9670163 0.7484348 0.8751893 0.7997669 0.8076478 0.09425787
32: 2020-03-24        R  <NA>                       estimate 0.6546243 0.9565019 0.7250452 0.8446799 0.7999754 0.8072158 0.09307308
33: 2020-03-25        R  <NA>                       estimate 0.6839714 0.9612741 0.7524730 0.8625964 0.8062774 0.8132966 0.08997928
34: 2020-03-26        R  <NA>                       estimate 0.6929421 0.9728689 0.7609478 0.8816302 0.8208108 0.8241912 0.08962846
35: 2020-03-27        R  <NA>                       estimate 0.7090285 0.9679610 0.7881070 0.8933225 0.8378399 0.8398763 0.08198008
36: 2020-03-28        R  <NA>                       estimate 0.7357354 0.9684348 0.8070636 0.9032873 0.8582898 0.8591889 0.07252142
37: 2020-03-29        R  <NA>                       estimate 0.8125842 0.9499249 0.8582622 0.9095175 0.8848453 0.8851085 0.04135362
38: 2020-03-30        R  <NA>                       estimate 0.8125842 0.9499249 0.8582622 0.9095175 0.8848453 0.8851085 0.04135362
39: 2020-03-31        R  <NA>                       estimate 0.8125842 0.9499249 0.8582622 0.9095175 0.8848453 0.8851085 0.04135362
40: 2020-04-01        R  <NA>                       estimate 0.8125842 0.9499249 0.8582622 0.9095175 0.8848453 0.8851085 0.04135362
41: 2020-04-02        R  <NA>                       estimate 0.8125842 0.9499249 0.8582622 0.9095175 0.8848453 0.8851085 0.04135362
42: 2020-04-03        R  <NA>                       estimate 0.8125842 0.9499249 0.8582622 0.9095175 0.8848453 0.8851085 0.04135362
43: 2020-04-04        R  <NA>                       estimate 0.8125842 0.9499249 0.8582622 0.9095175 0.8848453 0.8851085 0.04135362
44: 2020-04-05        R  <NA>                       estimate 0.8125842 0.9499249 0.8582622 0.9095175 0.8848453 0.8851085 0.04135362
45: 2020-04-06        R  <NA>                       estimate 0.8125842 0.9499249 0.8582622 0.9095175 0.8848453 0.8851085 0.04135362
46: 2020-04-07        R  <NA>                       estimate 0.8125842 0.9499249 0.8582622 0.9095175 0.8848453 0.8851085 0.04135362
47: 2020-04-08        R  <NA>                       estimate 0.8125842 0.9499249 0.8582622 0.9095175 0.8848453 0.8851085 0.04135362
48: 2020-04-09        R  <NA>                       estimate 0.8125842 0.9499249 0.8582622 0.9095175 0.8848453 0.8851085 0.04135362
49: 2020-04-10        R  <NA>                       estimate 0.8125842 0.9499249 0.8582622 0.9095175 0.8848453 0.8851085 0.04135362
50: 2020-04-11        R  <NA> estimate based on partial data 0.7534634 1.0088976 0.8149202 0.9228872 0.8799057 0.8825871 0.07944632
51: 2020-04-12        R  <NA> estimate based on partial data 0.7534634 1.0088976 0.8149202 0.9228872 0.8799057 0.8825871 0.07944632
52: 2020-04-13        R  <NA> estimate based on partial data 0.7534634 1.0088976 0.8149202 0.9228872 0.8799057 0.8825871 0.07944632
53: 2020-04-14        R  <NA> estimate based on partial data 0.7534634 1.0088976 0.8149202 0.9228872 0.8799057 0.8825871 0.07944632
54: 2020-04-15        R  <NA> estimate based on partial data 0.7534634 1.0088976 0.8149202 0.9228872 0.8799057 0.8825871 0.07944632
55: 2020-04-16        R  <NA> estimate based on partial data 0.7534634 1.0088976 0.8149202 0.9228872 0.8799057 0.8825871 0.07944632
56: 2020-04-17        R  <NA> estimate based on partial data 0.7534634 1.0088976 0.8149202 0.9228872 0.8799057 0.8825871 0.07944632
57: 2020-04-18        R  <NA> estimate based on partial data 0.7534634 1.0088976 0.8149202 0.9228872 0.8799057 0.8825871 0.07944632
58: 2020-04-19        R  <NA> estimate based on partial data 0.7534634 1.0088976 0.8149202 0.9228872 0.8799057 0.8825871 0.07944632
59: 2020-04-20        R  <NA> estimate based on partial data 0.7534634 1.0088976 0.8149202 0.9228872 0.8799057 0.8825871 0.07944632
60: 2020-04-21        R  <NA> estimate based on partial data 0.7534634 1.0088976 0.8149202 0.9228872 0.8799057 0.8825871 0.07944632
          date variable strat                           type    bottom       top     lower     upper    median      mean         sd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment