Skip to content

Commit

Permalink
Fix equicoordinate quantile for mvnorm when p%in%c(0,1)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchelloharawild committed Sep 15, 2024
1 parent 1410cd1 commit 143d764
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
results correctly as matrices.
* The `cdf()` method for `dist_multivariate_normal()` now gives the P(X <= q)
rather than P(X > q) for consistency with all other `cdf()` methods.
* The `quantile()` method for `dist_multivariate_normal()` now correctly gives
the boundaries when `p=0` or `p=1` when `type="equicoordinate"`.

## New features

Expand All @@ -30,7 +32,6 @@
* `dist_mixture()` now displays the components of the mixture when the output
width is sufficiently wide (@statasaurus, #112).


## Breaking changes

* The `quantile()` method for `dist_multivariate_normal()` now defaults to
Expand Down
6 changes: 5 additions & 1 deletion R/dist_multivariate_normal.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#'
#' @examples
#' dist <- dist_multivariate_normal(mu = list(c(1,2)), sigma = list(matrix(c(4,2,2,3), ncol=2)))
#' dimnames(dist) <- c("x", "y")
#' dist
#'
#' @examplesIf requireNamespace("mvtnorm", quietly = TRUE)
Expand Down Expand Up @@ -62,7 +63,10 @@ quantile.dist_mvnorm <- function(x, p, type = c("equicoordinate", "marginal"),
sd = rep(diag(sqrt(x[["sigma"]])), each = length(p)), ...)
} else {
require_package("mvtnorm")
vapply(p, function(...) mvtnorm::qmvnorm(...)$quantile, numeric(1L), mean = x[["mu"]], sigma = x[["sigma"]], ...)
vapply(p, function(p, ...) {
if (p == 0) return(-Inf) else if (p == 1) return(Inf)
mvtnorm::qmvnorm(p, ...)$quantile
}, numeric(1L), mean = x[["mu"]], sigma = x[["sigma"]], ...)
}

matrix(q, nrow = length(p), ncol = dim(x))
Expand Down

0 comments on commit 143d764

Please sign in to comment.