mitchelloharawild / distributional

Vectorised distributions for R
https://pkg.mitchelloharawild.com/distributional
GNU General Public License v3.0
94 stars 15 forks source link

Easily create distributions from existing implementations? #30

Closed mjskay closed 4 years ago

mjskay commented 4 years ago

Is there an easy/exposed way in the API to wrap existing distributions? It seems a shame to have to re-implement everything in this package when R already supports so many distributions across many packages. Something like this would be very cool:

dist_("norm", ...)
# or
dist_("norm", ..., package = "stats")
# or
dist_("stats::norm", ...)

Which would simply use the "d", "p", "q", and "r" functions with that suffix from the specified package. Then with a class like c("dist_wrap_stats__norm", "dist_wrap", ...) you could give default implementations to most functions for dist_wrap (maybe even including using numerical integration for mean/etc, and inferring support using the quantile function and data type of a single random variate, or something) but allow specific implementations by implementing {function}.dist_wrap_{name of dist}.

This would make the package even more attractive, since it would allow users to make use of whatever existing distributions they have with little effort. My (slight) ulterior motive in this is that it would also make it even easier for me to use with ggdist, as it would save me special-casing some things --- I would just convert anything to a distribution object on the way in if it isn't one already.

mjskay commented 4 years ago

PS sorry for the flood of issues, I think that's it for now :)

mitchelloharawild commented 4 years ago

Some great suggestions - thanks for the issues. dist_wrap() could be added to achieve this. The documentation for it should probably guide them toward creating an issue to add a new distribution.

For {ggdist}, are you thinking of converting character inputs to use dist_wrap()?

mjskay commented 4 years ago

For {ggdist}, are you thinking of converting character inputs to use dist_wrap()?

It could be useful since it would probably simplify some things on my side and reduce a bit of duplication, so I would probably switch to that internally eventually.

mitchelloharawild commented 4 years ago

Added, thanks for the suggestion. I would also like to have the option for customising p/d/q/r function names, in case the {stats} convention isn't followed. This would be very inefficient until #25 is done, so I'll hold off on this for now.

mjskay commented 4 years ago

Awesome, thanks!