Skip to content

Commit

Permalink
handle exceptions with NA limits
Browse files Browse the repository at this point in the history
  • Loading branch information
venpopov committed Apr 1, 2024
1 parent 8ef186c commit 10700c5
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions R/transformed.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ support.dist_transformed <- function(x, ...) {
support <- support(x[["dist"]])
lim <- field(support, "lim")[[1]]
lim <- SW(x[['transform']](lim))
lim <- sort(lim)
if (all(!is.na(lim))) {
lim <- sort(lim)
}
field(support, "lim")[[1]] <- lim
support
}
Expand All @@ -54,15 +56,10 @@ density.dist_transformed <- function(x, at, ...){
d <- density(x[["dist"]], inv(at)) * abs(jacobian)
limits <- field(support(x), "lim")[[1]]
closed <- field(support(x), "closed")[[1]]
if (closed[1]) {
d[which(at < limits[1])] <- 0
} else {
d[which(at <= limits[1])] <- 0
}
if (closed[2]) {
d[which(at > limits[2])] <- 0
} else {
d[which(at >= limits[2])] <- 0
if (!any(is.na(limits))) {
`%op1%` <- if (closed[1]) `<` else `<=`
`%op2%` <- if (closed[2]) `>` else `>=`
d[which(at %op1% limits[1] | at %op2% limits[2])] <- 0
}
d
}
Expand All @@ -72,8 +69,10 @@ cdf.dist_transformed <- function(x, q, ...){
inv <- function(v) SW(x[["inverse"]](v))
p <- cdf(x[["dist"]], inv(q), ...)
limits <- field(support(x), "lim")[[1]]
p[q <= limits[1]] <- 0
p[q >= limits[2]] <- 1
if (!any(is.na(limits))) {
p[q <= limits[1]] <- 0
p[q >= limits[2]] <- 1
}
p
}

Expand Down

0 comments on commit 10700c5

Please sign in to comment.