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_get_cache(), enw_model(), enw_priors_as_data_list(), enw_sample(), enw_set_cache(), enw_stan_to_r(), enw_unset_cache(), 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
#>      <char> <num> <num>
#> 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
#>               <char>
#> 1:     refp_mean_int
#> 2:       refp_sd_int
#> 3: refp_mean_beta_sd
#> 4:   refp_sd_beta_sd
#> 5:         refnp_int
#> 6:     refnp_beta_sd
#>                                                       description
#>                                                            <char>
#> 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
#> 5:              Intercept for non-parametric reference date delay
#> 6:     Standard deviation of scaled pooled non-parametric effects
#>             distribution  mean    sd
#>                   <char> <num> <num>
#> 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
#> 5:                Normal   0.0     1
#> 6: 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      q20
#>              <char>     <num>    <num>     <num>      <num>     <num>    <num>
#> 1: refp_mean_int[1] 2.8717659 2.828525 0.6134551 0.60945238 1.9613285 2.330854
#> 2:   refp_sd_int[1] 3.7026616 3.688925 0.2972151 0.30275433 3.2274870 3.448894
#> 3:      sqrt_phi[1] 0.3476026 0.346561 0.0314766 0.03275434 0.2964757 0.320987
#>          q80       q95     rhat  ess_bulk ess_tail
#>        <num>     <num>    <num>     <num>    <num>
#> 1: 3.3737040 3.9624635 1.007791  549.4149 721.2235
#> 2: 3.9502920 4.1949650 1.007210  670.3456 822.6031
#> 3: 0.3735928 0.4007032 1.000115 1126.7382 768.7450

enw_replace_priors(default_priors, fit_priors)
#>             variable
#>               <char>
#> 1: refp_mean_beta_sd
#> 2:   refp_sd_beta_sd
#> 3:         refnp_int
#> 4:     refnp_beta_sd
#> 5:     refp_mean_int
#> 6:       refp_sd_int
#> 7:          sqrt_phi
#>                                                    description
#>                                                         <char>
#> 1: Standard deviation of scaled pooled parametric mean effects
#> 2:   Standard deviation of scaled pooled parametric sd effects
#> 3:           Intercept for non-parametric reference date delay
#> 4:  Standard deviation of scaled pooled non-parametric effects
#> 5:                                                        <NA>
#> 6:                                                        <NA>
#> 7:                                                        <NA>
#>             distribution      mean        sd
#>                   <char>     <num>     <num>
#> 1: Zero truncated normal 0.0000000 1.0000000
#> 2: Zero truncated normal 0.0000000 1.0000000
#> 3:                Normal 0.0000000 1.0000000
#> 4: Zero truncated normal 0.0000000 1.0000000
#> 5:                  <NA> 2.8717659 0.6134551
#> 6:                  <NA> 3.7026616 0.2972151
#> 7:                  <NA> 0.3476026 0.0314766