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

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.9050494 2.8662550 0.6298971 0.60792530 1.906698 2.374370
#> 2:   refp_sd_int[1] 3.7061514 3.6991850 0.3012037 0.30680183 3.237901 3.458442
#> 3:      sqrt_phi[1] 0.3465375 0.3448305 0.0314694 0.03032955 0.294676 0.321174
#>          q80       q95      rhat ess_bulk ess_tail
#>        <num>     <num>     <num>    <num>    <num>
#> 1: 3.4255460 4.0286135 0.9998312 856.6469 768.7502
#> 2: 3.9609120 4.2112490 0.9990012 900.2061 844.7802
#> 3: 0.3735868 0.4001852 1.0005785 873.5007 771.9195

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.9050494 0.6298971
#> 6:                  <NA> 3.7061514 0.3012037
#> 7:                  <NA> 0.3465375 0.0314694