Skip to content

Commit

Permalink
Added family(<distribution>) methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchelloharawild committed Nov 6, 2021
1 parent eb5a6d3 commit 0b7414a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ S3method(dim,dist_default)
S3method(dim,dist_multinomial)
S3method(dim,dist_mvnorm)
S3method(dimnames,distribution)
S3method(family,dist_default)
S3method(family,dist_wrap)
S3method(family,distribution)
S3method(format,dist_bernoulli)
S3method(format,dist_beta)
S3method(format,dist_binomial)
Expand Down Expand Up @@ -522,6 +525,7 @@ importFrom(grDevices,col2rgb)
importFrom(grDevices,rgb)
importFrom(lifecycle,deprecate_soft)
importFrom(stats,density)
importFrom(stats,family)
importFrom(stats,median)
importFrom(stats,quantile)
importFrom(utils,.DollarNames)
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Generics

* Added `parameters()` generic for obtaining the distribution's parameters.
* Added `family(<distribution>)` for getting the distribution's family name.
* Added `covariance()` to return the covariance of a distribution.
* Added `support()` to identify the distribution's region of support (#8).

Expand Down
5 changes: 5 additions & 0 deletions R/default.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ parameters.dist_default <- function(x, ...) {
})
}

#' @export
family.dist_default <- function(x, ...) {
substring(class(x)[1], first = 6)
}

#' @export
support.dist_default <- function(x, ...) {
new_support_region(
Expand Down
7 changes: 6 additions & 1 deletion R/dist_wrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,10 @@ generate.dist_wrap <- function(x, times, ...){
#' @export
parameters.dist_wrap <- function(x, ...) {
# All parameters except distribution environment
x[-2]
x[-2L]
}

#' @export
family.dist_wrap <- function(x, ...) {
x[[1L]]
}
23 changes: 23 additions & 0 deletions R/distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,29 @@ parameters.distribution <- function(x, ...) {
x <- lapply(x, function(z) data_frame(!!!z, .name_repair = "minimal"))
vec_rbind(!!!x)
}

#' Extract the name of the distribution family
#'
#' \lifecycle{experimental}
#'
#' @param x The distribution(s).
#' @param ... Additional arguments used by methods.
#'
#' @examples
#' dist <- c(
#' dist_normal(1:2),
#' dist_poisson(3),
#' dist_multinomial(size = c(4, 3),
#' prob = list(c(0.3, 0.5, 0.2), c(0.1, 0.5, 0.4)))
#' )
#' family(dist)
#'
#' @importFrom stats family
#' @export
family.distribution <- function(x, ...) {
vapply(vec_data(x), family, character(1L))
}

#' Region of support of a distribution
#'
#' \lifecycle{experimental}
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ reference:
- hilo.distribution
- hdr.distribution
- support.distribution
- family.distribution
- parameters.distribution
- mean.distribution
- median.distribution
Expand Down

0 comments on commit 0b7414a

Please sign in to comment.