Skip to content

Commit

Permalink
Enable distribution vectors in tibbles (#108)
Browse files Browse the repository at this point in the history
* Enabled the inclusion of  vectors as columns in  data objects, which needs vctrs and tibble as Suggests dependencies

* add internal keyword to vec_proxy/vec_restore documentation
  • Loading branch information
zeileis authored Sep 10, 2024
1 parent 0791b6d commit 0c1507c
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 2 deletions.
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.

0 comments on commit 0c1507c

Please sign in to comment.