epiverse-trace / epiparameter

R package with library of epidemiological parameters for infectious diseases and functions and classes for working with parameters
https://epiverse-trace.github.io/epiparameter
Other
33 stars 11 forks source link

Improve access to `<distribution>` names #398

Closed joshwlambert closed 1 month ago

joshwlambert commented 1 month ago

This PR improves the access to distribution names in <distribution> objects from the {distributional} package by replacing the .get_mixture_dist() function with the more general .distributional_family() and enhancing family.epiparameter().

Using the new version of the <epiparameter> family() method, calls to stats::family() have been removed from the package, and is_truncated() has been simplified (#274).

The PR improves functions that were manually extracting the information from <distribution> objects by having to subset objects several times (e.g. discretise.epiparameter()) by now calling .distributional_family().

New tests are added for family.epiparameter() and .distributional_family().

joshwlambert commented 1 month ago

The improvements in the PR have also fixed a previously unknown bug when discretising <epiparameter> objects with a truncated lognormal distribution (I think the bug extended to any truncated probability distribution where the class name in the <distribution> object does not match the distribution name in R).

Here is a reprex using code from the main branch.

library(epiparameter)
ep = epiparameter(
  disease = "Disease", 
  epi_name = "SI", 
  prob_distribution = create_prob_distribution(
    prob_distribution = "lnorm", 
    prob_distribution_params = c(meanlog = 2, sdlog = 2), 
    truncation = 10)
)
#> Citation cannot be created as author, year, journal or title is missing
discretise(ep)
#> Warning: Discretising a truncated continuous distribution, returning
#> non-truncated discretised distribution
#> Error: Invalid parameterisation for lognormal distribution

Created on 2024-10-21 with reprex v2.1.0

Here is a reprex using code from the get_dist_class branch.

library(epiparameter)
ep = epiparameter(
  disease = "Disease", 
  epi_name = "SI", 
  prob_distribution = create_prob_distribution(
    prob_distribution = "lnorm", 
    prob_distribution_params = c(meanlog = 2, sdlog = 2), 
    truncation = 10)
)
#> Citation cannot be created as author, year, journal or title is missing
discretise(ep)
#> Warning: Discretising a truncated continuous distribution, returning
#> non-truncated discretised distribution
#> Disease: Disease
#> Pathogen: NA
#> Epi Parameter: si
#> Study: (????). "No citation."
#> Distribution: discrete lnorm
#> Parameters:
#>   meanlog: 2.000
#>   sdlog: 2.000

Created on 2024-10-21 with reprex v2.1.0