Skip to content

Commit 456aab8

Browse files
sbfnkgithub-actions[bot]
authored andcommitted
Automatic Vignette update
1 parent 988fea6 commit 456aab8

3 files changed

Lines changed: 70 additions & 65 deletions

File tree

-71 Bytes
Loading
3.58 KB
Loading

vignettes/prior_choice_guide.Rmd

Lines changed: 70 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,27 @@ We load the _EpiNow2_ package and the _posterior_ and _data.table_ packages whic
5050

5151
``` r
5252
library(EpiNow2)
53+
#>
54+
#> Attaching package: 'EpiNow2'
55+
#> The following object is masked from 'package:stats':
56+
#>
57+
#> Gamma
5358
library(posterior)
54-
#> This is posterior version 1.6.1.9000
59+
#> This is posterior version 1.7.0
5560
#>
5661
#> Attaching package: 'posterior'
57-
#> The following objects are masked from 'package:rstan':
58-
#>
59-
#> ess_bulk, ess_tail
6062
#> The following objects are masked from 'package:stats':
6163
#>
6264
#> mad, sd, var
6365
#> The following objects are masked from 'package:base':
6466
#>
6567
#> %in%, match
6668
library(data.table)
69+
#>
70+
#> Attaching package: 'data.table'
71+
#> The following object is masked from 'package:base':
72+
#>
73+
#> %notin%
6774
```
6875

6976
We also set up example data and parameters that will be used in code examples throughout this vignette.
@@ -90,8 +97,18 @@ delay <- example_incubation_period + reporting_delay
9097
# Fast Stan settings for vignette building
9198
# (Use default settings for actual analyses)
9299
stan <- stan_opts(
93-
samples = 100,
94-
warmup = 100,
100+
samples = 500,
101+
warmup = 250,
102+
chains = 2,
103+
control = list(adapt_delta = 0.9)
104+
)
105+
106+
# estimate_secondary() needs a few more post-warmup samples for a stable tail
107+
# effective sample size at these fast settings; the other models are fine with
108+
# `stan` above.
109+
stan_secondary <- stan_opts(
110+
samples = 1000,
111+
warmup = 500,
95112
chains = 2,
96113
control = list(adapt_delta = 0.9)
97114
)
@@ -518,12 +535,6 @@ fit_rw <- estimate_infections(
518535
gp = NULL, # Disable GP when using random walk
519536
stan = stan
520537
)
521-
#> Warning: There were 1 chains where the estimated Bayesian Fraction of Missing Information was low. See
522-
#> https://mc-stan.org/misc/warnings.html#bfmi-low
523-
#> Warning: Examine the pairs() plot to diagnose sampling problems
524-
#> Warning: The largest R-hat is NA, indicating chains have not mixed.
525-
#> Running the chains for more iterations may help. See
526-
#> https://mc-stan.org/misc/warnings.html#r-hat
527538
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
528539
#> Running the chains for more iterations may help. See
529540
#> https://mc-stan.org/misc/warnings.html#bulk-ess
@@ -992,9 +1003,6 @@ fit_renewal <- estimate_infections(
9921003
rt = rt_opts(prior = LogNormal(mean = 2, sd = 0.5)),
9931004
stan = stan
9941005
)
995-
#> Warning: The largest R-hat is NA, indicating chains have not mixed.
996-
#> Running the chains for more iterations may help. See
997-
#> https://mc-stan.org/misc/warnings.html#r-hat
9981006
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
9991007
#> Running the chains for more iterations may help. See
10001008
#> https://mc-stan.org/misc/warnings.html#bulk-ess
@@ -1014,15 +1022,6 @@ fit_nonmech <- estimate_infections(
10141022
#> day, i.e. the reproduction number is the same as the daily growth rate.
10151023
#> ℹ If this was intended then this warning can be silenced by setting `dist =
10161024
#> Fixed(1)`'.
1017-
#> Warning: The largest R-hat is NA, indicating chains have not mixed.
1018-
#> Running the chains for more iterations may help. See
1019-
#> https://mc-stan.org/misc/warnings.html#r-hat
1020-
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
1021-
#> Running the chains for more iterations may help. See
1022-
#> https://mc-stan.org/misc/warnings.html#bulk-ess
1023-
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
1024-
#> Running the chains for more iterations may help. See
1025-
#> https://mc-stan.org/misc/warnings.html#tail-ess
10261025
```
10271026

10281027
## Priors for estimate_secondary()
@@ -1055,7 +1054,7 @@ from primary observations (cases, admissions) with a delay and scaling.
10551054
fit_secondary_fixed <- estimate_secondary(
10561055
secondary_data,
10571056
delays = delay_opts(LogNormal(mean = 14, sd = 5, max = 30)),
1058-
stan = stan
1057+
stan = stan_secondary
10591058
)
10601059

10611060
# Uncertain delay (long time series)
@@ -1068,8 +1067,14 @@ fit_secondary_uncertain <- estimate_secondary(
10681067
max = 30
10691068
)
10701069
),
1071-
stan = stan
1070+
stan = stan_secondary
10721071
)
1072+
#> WARN [2026-09-02 15:53:08] estimate_secondary (chain: 1, 2): Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
1073+
#> Running the chains for more iterations may help. See
1074+
#> https://mc-stan.org/misc/warnings.html#bulk-ess -
1075+
#> WARN [2026-09-02 15:53:08] estimate_secondary (chain: 1, 2): Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
1076+
#> Running the chains for more iterations may help. See
1077+
#> https://mc-stan.org/misc/warnings.html#tail-ess -
10731078
```
10741079

10751080
## Priors for estimate_truncation()
@@ -1110,6 +1115,12 @@ fit_trunc_fast <- estimate_truncation(
11101115
),
11111116
stan = stan
11121117
)
1118+
#> WARN [2026-09-02 15:53:10] estimate_truncation (chain: 1, 2): Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
1119+
#> Running the chains for more iterations may help. See
1120+
#> https://mc-stan.org/misc/warnings.html#bulk-ess -
1121+
#> WARN [2026-09-02 15:53:10] estimate_truncation (chain: 1, 2): Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
1122+
#> Running the chains for more iterations may help. See
1123+
#> https://mc-stan.org/misc/warnings.html#tail-ess -
11131124

11141125
# Slower reporting (e.g., deaths, 7-14 days)
11151126
fit_trunc_slow <- estimate_truncation(
@@ -1123,6 +1134,12 @@ fit_trunc_slow <- estimate_truncation(
11231134
),
11241135
stan = stan
11251136
)
1137+
#> WARN [2026-09-02 15:53:12] estimate_truncation (chain: 1, 2): Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
1138+
#> Running the chains for more iterations may help. See
1139+
#> https://mc-stan.org/misc/warnings.html#bulk-ess -
1140+
#> WARN [2026-09-02 15:53:13] estimate_truncation (chain: 1, 2): Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
1141+
#> Running the chains for more iterations may help. See
1142+
#> https://mc-stan.org/misc/warnings.html#tail-ess -
11261143
```
11271144

11281145
# Practical workflow for prior specification
@@ -1140,9 +1157,6 @@ estimates <- estimate_infections(
11401157
delays = delay_opts(example_incubation_period + reporting_delay),
11411158
stan = stan
11421159
)
1143-
#> Warning: The largest R-hat is NA, indicating chains have not mixed.
1144-
#> Running the chains for more iterations may help. See
1145-
#> https://mc-stan.org/misc/warnings.html#r-hat
11461160
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
11471161
#> Running the chains for more iterations may help. See
11481162
#> https://mc-stan.org/misc/warnings.html#bulk-ess
@@ -1175,15 +1189,9 @@ estimates_r0 <- estimate_infections(
11751189
rt = rt_opts(prior = LogNormal(mean = 2.5, sd = 0.5)),
11761190
stan = stan
11771191
)
1178-
#> Warning: The largest R-hat is NA, indicating chains have not mixed.
1179-
#> Running the chains for more iterations may help. See
1180-
#> https://mc-stan.org/misc/warnings.html#r-hat
11811192
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
11821193
#> Running the chains for more iterations may help. See
11831194
#> https://mc-stan.org/misc/warnings.html#bulk-ess
1184-
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
1185-
#> Running the chains for more iterations may help. See
1186-
#> https://mc-stan.org/misc/warnings.html#tail-ess
11871195

11881196
# Compare results
11891197
plot(estimates)
@@ -1226,50 +1234,47 @@ fit <- estimates_r0$fit
12261234

12271235
# Check Rhat (should be < 1.01)
12281236
summarise_draws(fit, "rhat")
1229-
#> # A tibble: 522 × 2
1237+
#> # A tibble: 496 × 2
12301238
#> variable rhat
12311239
#> <chr> <dbl>
1232-
#> 1 params[1] 1.02
1240+
#> 1 params[1] 1.01
12331241
#> 2 params[2] 1.00
1234-
#> 3 params[3] 1.01
1235-
#> 4 eta[1] 0.995
1236-
#> 5 eta[2] 0.995
1237-
#> 6 eta[3] 1.06
1238-
#> 7 eta[4] 0.991
1239-
#> 8 eta[5] 1.03
1240-
#> 9 eta[6] 1.00
1241-
#> 10 eta[7] 1.02
1242-
#> # ℹ 512 more rows
1242+
#> 3 params[3] 1.000
1243+
#> 4 eta[1] 1.00
1244+
#> 5 eta[2] 1.01
1245+
#> 6 eta[3] 1.00
1246+
#> 7 eta[4] 1.01
1247+
#> 8 eta[5] 1.02
1248+
#> 9 eta[6] 1.01
1249+
#> 10 eta[7] 1.000
1250+
#> # ℹ 486 more rows
12431251

12441252
# Check effective sample size (should be > 400 for reliable inference)
12451253
summarise_draws(fit, "ess_bulk", "ess_tail")
1246-
#> Warning: The ESS has been capped to avoid unstable estimates.
1247-
#> Warning: The ESS has been capped to avoid unstable estimates.
1248-
#> Warning: The ESS has been capped to avoid unstable estimates.
1249-
#> # A tibble: 522 × 3
1254+
#> # A tibble: 496 × 3
12501255
#> variable ess_bulk ess_tail
12511256
#> <chr> <dbl> <dbl>
1252-
#> 1 params[1] 129. 71.9
1253-
#> 2 params[2] 104. 117.
1254-
#> 3 params[3] 66.3 60.2
1255-
#> 4 eta[1] 110. 70.2
1256-
#> 5 eta[2] 101. 67.7
1257-
#> 6 eta[3] 93.4 79.9
1258-
#> 7 eta[4] 129. 116.
1259-
#> 8 eta[5] 73.0 53.8
1260-
#> 9 eta[6] 144. 41.9
1261-
#> 10 eta[7] 83.6 58.0
1262-
#> # ℹ 512 more rows
1257+
#> 1 params[1] 305. 243.
1258+
#> 2 params[2] 463. 334.
1259+
#> 3 params[3] 414. 440.
1260+
#> 4 eta[1] 339. 320.
1261+
#> 5 eta[2] 287. 332.
1262+
#> 6 eta[3] 258. 355.
1263+
#> 7 eta[4] 460. 391.
1264+
#> 8 eta[5] 532. 315.
1265+
#> 9 eta[6] 525. 398.
1266+
#> 10 eta[7] 707. 341.
1267+
#> # ℹ 486 more rows
12631268

12641269
# You can also use the summary method which includes these diagnostics
12651270
summary(estimates_r0)
12661271
#> measure estimate
12671272
#> <char> <char>
1268-
#> 1: New infections per day 2139 (1415 -- 3150)
1273+
#> 1: New infections per day 2195 (1355 -- 3636)
12691274
#> 2: Expected change in reports Likely decreasing
1270-
#> 3: Effective reproduction no. 0.88 (0.71 -- 1)
1271-
#> 4: Rate of growth -0.033 (-0.09 -- 0.023)
1272-
#> 5: Doubling/halving time (days) -21 (30 -- -7.7)
1275+
#> 3: Effective reproduction no. 0.88 (0.7 -- 1.1)
1276+
#> 4: Rate of growth -0.03 (-0.096 -- 0.036)
1277+
#> 5: Doubling/halving time (days) -23 (19 -- -7.2)
12731278
```
12741279

12751280
Key diagnostics to check:

0 commit comments

Comments
 (0)