Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update <epiparameter> constructor function and internal refactor #381

Merged
merged 50 commits into from
Sep 30, 2024

Conversation

joshwlambert
Copy link
Member

This is a relatively large PR with several aspects of refactoring. The aim of this sequence of developments is to simplify the code base of the package, to make the construction of <epiparameter> objects more modular and remove ambiguity in function arguments.

This PR contains Breaking changes.

The epiparameter() function has a new function signature where the arguments for constructing a probability distribution object have been removed and moved to the create_prob_distribution() function. This was previously named create_prob_dist() but has been renamed as have several arguments that used prob_dist to remove any ambiguity to _dist (as requested by a package user).

The new design of epiparameter() and create_prob_distribution() have helped greatly simply new_epiparameter().

The handling of uncertainty when constructing <epiparameter> objects has been enhanced to now work when probability distribution objects are constructed (assuming auto_calc_params = TRUE) to match the parameters, whereas previously this would have caused a mismatch between the parameters and the parameter uncertainty.

The as_epiparameter() method for converting from {epireview} tables has been updated to create parameterised <epiparameter> objects when adequate summary statistics are provided and a user-specified probability distribution is supplied via ....

The prob_dist_params argument has been removed from .calc_dist_params() thanks to changes made in #379. It also calls the convert_summary_stats_to_params() function for converting mean and dispersion parameters of lognormal distribution, fixing a likely bug in the previous conversion implemented in .calc_dist_params().

This PR adds the %||% operator to the package.

Note: many of the diffs in this PR are changing prob_dist to prob_distribution inside functions. This is a non-functional change. Many of these can be skimmed as they should not make any functional change to the package. Although spotting instances where I have missed some prob_dist or updated them incorrectly would of course be appreciated.

Unit tests, snapshots and documentation have been updated where necessary.

…all convert_summary_stats_to_params internally
… can be done in epiparameter() before calling new_epiparameter
…e_prob_distribution as default for prob_distribution arg
@joshwlambert joshwlambert added the enhancement New feature or request label Sep 25, 2024
Copy link
Member

@Bisaloo Bisaloo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change is going in the right direction. In particular, I see how it opens up the potential for more interoperability with other ways of defining probability distributions. We could for example imagine at some point accepting distributions from EpiNow2 in prob_distribution, which was more difficult with the previous structure.

On the topic of renaming arguments, it would be very helpful in the future if you can do it in a single commit. This way, it's really possible to review while ignoring the non-functional changes.

#' )
is_epiparameter_params <- function(prob_dist, prob_dist_params) {
if (is.na(prob_dist) || anyNA(prob_dist_params)) {
is_epiparameter_params <- function(prob_distribution,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My intuition would have been to have this as an internal helper. I've been quite removed from the codebase and may likely miss some subtleties. Happy for you to make the final call but please take a moment to consider if it should really be exported.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I exported it was I thought it might be useful to some users to check if the parameters they want to input are valid for parameterising an <epiparameter> as not every possible parameterisation is supported.

If you think this should be made internal please let me know and I'll unexport it.

# return prob_dist object
prob_dist
# return prob_distribution object
prob_distribution
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have a class (only used internally)? It may ultimately allow us to get objects from other origins in epiparameter() call.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrapping the probability distribution object in a subclass? We could do this, but it might have complications for <distribution> objects from {distributional} as its built on {vctrs} which is a OOP framework I'm less familiar with.

Please open an issue stating the benefits of giving this object a class and I can implement in a follow-up PR.

meanlog: 0.660
sdlog: 1.205
meanlog: 1.386
sdlog: 0.593
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these supposed to change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are changing due to a bug fix in #378 which are now updating the automatically calculated parameters of these database entries, so this change is expected.

@adamkucharski
Copy link
Member

Thanks for putting together, these changes look sensible, especially if open up more flexibility. Quick question - does the %||% operator involve a new dependency?

@joshwlambert
Copy link
Member Author

Thanks both for your comments.

@adamkucharski %||% does not add a dependency. It is an operator that has recently been aded to R in one of the recent versions (~v.4.4) but I add it to the package to make sure it is available for older versions of R.

@joshwlambert
Copy link
Member Author

This PR is failing the tests workflow on GHA due to a change in {epireview} which is loade in the as_epiparameter() tests. I will merge this PR now and fix the broken test in a follow-up PR.

@joshwlambert joshwlambert merged commit f2585e1 into main Sep 30, 2024
8 of 9 checks passed
@joshwlambert joshwlambert deleted the constructor_sig branch September 30, 2024 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants