Skip to content

Commit

Permalink
Fix indexing of transformed distributions by Math and Ops
Browse files Browse the repository at this point in the history
Resolves #73
  • Loading branch information
mitchelloharawild committed Nov 7, 2021
1 parent 212cc13 commit 9dc1556
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 22 deletions.
4 changes: 2 additions & 2 deletions R/default.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ invert_fail <- function(...) stop("Inverting transformations for distributions i
Math.dist_default <- function(x, ...) {
if(dim(x) > 1) stop("Transformations of multivariate distributions are not yet supported.")
trans <- new_function(exprs(x = ), body = expr((!!sym(.Generic))(x, !!!dots_list(...))))
dist_transformed(wrap_dist(list(x)), trans, invert_fail)[[1]]
vec_data(dist_transformed(wrap_dist(list(x)), trans, invert_fail))[[1]]
}

#' @method Ops dist_default
Expand All @@ -180,5 +180,5 @@ Ops.dist_default <- function(e1, e2) {
new_function(exprs(x = ), body = expr((!!sym(.Generic))(!!e1, (!!e2$transform)(x))))
}

dist_transformed(wrap_dist(list(e1,e2)[which(is_dist)]), trans, invert_fail)[[1]]
vec_data(dist_transformed(wrap_dist(list(e1,e2)[which(is_dist)]), trans, invert_fail))[[1]]
}
4 changes: 2 additions & 2 deletions R/transformed.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ covariance.dist_transformed <- function(x, ...){
#' @export
Math.dist_transformed <- function(x, ...) {
trans <- new_function(exprs(x = ), body = expr((!!sym(.Generic))((!!x$transform)(x), !!!dots_list(...))))
dist_transformed(wrap_dist(list(x[["dist"]])), trans, invert_fail)[[1]]
vec_data(dist_transformed(wrap_dist(list(x[["dist"]])), trans, invert_fail))[[1]]
}

#' @method Ops dist_transformed
Expand All @@ -101,5 +101,5 @@ Ops.dist_transformed <- function(e1, e2) {
} else {
new_function(exprs(x = ), body = expr((!!sym(.Generic))(!!e1, (!!e2$transform)(x))))
}
dist_transformed(wrap_dist(list(list(e1,e2)[[which(is_dist)[1]]][["dist"]])), trans, invert_fail)[[1]]
vec_data(dist_transformed(wrap_dist(list(list(e1,e2)[[which(is_dist)[1]]][["dist"]])), trans, invert_fail))[[1]]
}
24 changes: 6 additions & 18 deletions tests/testthat/test-transformations.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,20 @@ test_that("LogNormal distributions", {

# quantiles
expect_equal(
quantile(dist, 0.1),
qlnorm(0.1, 0, 0.5)
)
expect_equal(
quantile(dist, 0.5),
qlnorm(0.5, 0, 0.5)
quantile(dist, c(0.1, 0.5)),
list(qlnorm(c(0.1, 0.5), 0, 0.5))
)

# pdf
expect_equal(
density(dist, 1),
dlnorm(1, 0, 0.5)
)
expect_equal(
density(dist, 20),
dlnorm(20, 0, 0.5)
density(dist, c(1, 20)),
list(dlnorm(c(1, 20), 0, 0.5))
)

# cdf
expect_equal(
cdf(dist, 4),
plnorm(4, 0, 0.5)
)
expect_equal(
cdf(dist, 90),
plnorm(90, 0, 0.5)
cdf(dist, c(4, 90)),
list(plnorm(c(4, 90), 0, 0.5))
)

# F(Finv(a)) ~= a
Expand Down

0 comments on commit 9dc1556

Please sign in to comment.