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

Enable distribution vectors in tibbles #108

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ Suggests:
PoissonBinomial,
revdbayes (>= 1.3.5),
rmarkdown,
testthat (>= 3.0.0)
testthat (>= 3.0.0),
tibble,
vctrs
VignetteBuilder:
knitr
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Config/testthat/edition: 3
Config/testthat/parallel: true
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ S3method(variance,ZINegativeBinomial)
S3method(variance,ZIPoisson)
S3method(variance,ZTNegativeBinomial)
S3method(variance,ZTPoisson)
S3method(vctrs::vec_proxy,distribution)
S3method(vctrs::vec_restore,distribution)
export(Bernoulli)
export(Beta)
export(Binomial)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
using `match()` for distribution objects.
- Added a `duplicated()` method which relies on the corresponding method for the `data.frame`
of parameters in a distribution.
- Enabled the inclusion of `distribution` vectors as columns in `tibble` data objects, see
`?vec_proxy.distribution` for further details and a practical example.
- Fixed errors in notation of cumulative distribution function in the documentation of
`HurdlePoisson()` and `HurdleNegativeBinomial()` (by @dkwhu in #94 and #96).
- The `prodist()` method for `glm` objects can now also handle `family` specifications from
Expand Down
61 changes: 61 additions & 0 deletions R/tibble-vctrs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#' Methods for including distributions as vctrs in tibbles
#'
#' Methods for \code{\link[vctrs]{vec_proxy}} and \code{\link[vctrs]{vec_restore}}
#' from \pkg{vctrs} in order to include \code{distribution} objects in
#' \code{\link[tibble]{tibble}} objects.
#'
#' @details The methods for \code{\link[vctrs]{vec_proxy}} and
#' \code{\link[vctrs]{vec_restore}} from \pkg{vctrs} are needed so that
#' \code{distribution} objects can be included as a vector column in
#' (and extracted from) \code{\link[tibble]{tibble}} data frames.
#' \code{vec_proxy} simply adds the class \code{data.frame} which is the
#' actual underlying data structure used by \code{distribution} objects.
#' This way the number of rows etc. can be correctly determined. Conversely,
#' \code{vec_restore} strips off the additional \code{data.frame} class and
#' restores the original \code{distribution} classes. Users typically do not
#' need to call \code{vec_proxy} and \code{vec_restore} directly.
#'
#' @param x,to Objects inheriting from \code{distribution}.
#' @param ... Currently not used.
#'
#' @return The `vec_proxy` method returns a `distribution` object which
#' additionally inherits of `data.frame` while the `vec_restore` method
#' restores the original `distribution` classes.
#'
#' @examples
#' \dontshow{ if(!requireNamespace("tibble")) {
#' if(interactive() || is.na(Sys.getenv("_R_CHECK_PACKAGE_NAME_", NA))) {
#' stop("not all packages required for the example are installed")
#' } else q() }
#' }
#' ## Poisson GLM for FIFA 2018 goals data
#' data("FIFA2018", package = "distributions3")
#' m <- glm(goals ~ difference, data = FIFA2018, family = poisson)
#'
#' ## Predict fitted Poisson distributions for teams with ability differences
#' ## of -1, 0, 1 (out-of-sample) using the new data as a data.frame
#' nd <- data.frame(difference = -1:1)
#' nd$dist <- prodist(m, newdata = nd)
#' nd
#'
#' ## Do the same using the new data as a tibble
#' library("tibble")
#' nt <- tibble(difference = -1:1)
#' nt$dist <- prodist(m, newdata = nt)
#' nt
#'
#' @keywords internal

#' @rdname vec_proxy.distribution
#' @exportS3Method vctrs::vec_proxy distribution
vec_proxy.distribution <- function(x, ...) {
class(x) <- c(class(x), "data.frame")
return(x)
}

#' @rdname vec_proxy.distribution
#' @exportS3Method vctrs::vec_restore distribution
vec_restore.distribution <- function(x, to, ...) {
class(x) <- class(to)
return(x)
}
62 changes: 62 additions & 0 deletions man/vec_proxy.distribution.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading