Skip to contents

This function is used internally by epinowcast to replace default model priors with users specified ones (restricted to normal priors with specified mean and standard deviations). A common use would be extracting the posterior from a previous epinowcast() run (using summary(nowcast, type = fit)) and using this a prior.

Usage

enw_replace_priors(priors, custom_priors)

Arguments

priors

A data.frame with the following variables: variable, mean, sd describing normal priors. Priors in the appropriate format are returned by enw_reference() as well as by other similar model specification functions.

custom_priors

A data.frame with the following variables: variable, mean, sd describing normal priors. Priors in the appropriate format are returned by enw_reference() as well as by other similar model specification functions. Priors in this data.frame replace the default priors. Note that currently vectorised prior names (i.e those of the form variable[n] will be treated as variable).

Value

A data.table of prior definitions (variable, mean and sd).

See also

Functions used to help convert models into the format required for stan enw_formula_as_data_list(), enw_model(), enw_priors_as_data_list(), enw_sample(), remove_profiling(), write_stan_files_no_profile()

Examples

# Update priors from a data.frame
priors <- data.frame(variable = c("x", "y"), mean = c(1, 2), sd = c(1, 2))
custom_priors <- data.frame(variable = "x[1]", mean = 10, sd = 2)
enw_replace_priors(priors, custom_priors)
#>    variable mean sd
#> 1:        y    2  2
#> 2:        x   10  2

# Update priors from a previous model fit
default_priors <- enw_reference(
 distribution = "lognormal",
 data = enw_example("preprocessed"),
)$priors
print(default_priors)
#>             variable
#> 1:     refp_mean_int
#> 2:       refp_sd_int
#> 3: refp_mean_beta_sd
#> 4:   refp_sd_beta_sd
#>                                                       description
#> 1:         Log mean intercept for parametric reference date delay
#> 2: Log standard deviation for the parametric reference date delay
#> 3:    Standard deviation of scaled pooled parametric mean effects
#> 4:      Standard deviation of scaled pooled parametric sd effects
#>             distribution mean sd
#> 1:                Normal  1.0  1
#> 2: Zero truncated normal  0.5  1
#> 3: Zero truncated normal  0.0  1
#> 4: Zero truncated normal  0.0  1

fit_priors <- summary(
 enw_example("nowcast"), type = "fit",
 variables = c("refp_mean_int", "refp_sd_int", "sqrt_phi")
)
fit_priors
#>            variable      mean    median         sd        mad        q5
#> 1: refp_mean_int[1] 1.4065186 1.3627850 0.31867081 0.28263545 0.9784832
#> 2:   refp_sd_int[1] 1.9488911 1.9301000 0.17462563 0.16212231 1.6974820
#> 3:      sqrt_phi[1] 0.3253364 0.3242025 0.03123022 0.03085661 0.2758690
#>          q20       q80       q95     rhat  ess_bulk ess_tail
#> 1: 1.1410640 1.6335340 2.0573970 1.012034  341.9545 526.7013
#> 2: 1.7996520 2.0693360 2.2809700 1.008008  404.0572 428.0146
#> 3: 0.2998764 0.3513736 0.3794238 1.002608 1325.7827 735.9718

enw_replace_priors(default_priors, fit_priors)
#>             variable
#> 1: refp_mean_beta_sd
#> 2:   refp_sd_beta_sd
#> 3:     refp_mean_int
#> 4:       refp_sd_int
#> 5:          sqrt_phi
#>                                                    description
#> 1: Standard deviation of scaled pooled parametric mean effects
#> 2:   Standard deviation of scaled pooled parametric sd effects
#> 3:                                                        <NA>
#> 4:                                                        <NA>
#> 5:                                                        <NA>
#>             distribution      mean         sd
#> 1: Zero truncated normal 0.0000000 1.00000000
#> 2: Zero truncated normal 0.0000000 1.00000000
#> 3:                  <NA> 1.4065186 0.31867081
#> 4:                  <NA> 1.9488911 0.17462563
#> 5:                  <NA> 0.3253364 0.03123022