Skip to content

Commit

Permalink
Add +-Inf boundary cases for density of sample distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchelloharawild committed Sep 13, 2024
1 parent 66e41df commit 21f6a63
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions R/dist_sample.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,19 @@ density.dist_sample <- function(x, at, ..., na.rm = TRUE){
)
)
}
z <- numeric(length(at))
zi <- is.finite(at)
at <- at[zi]
zl <- vec_size(at)

# Shortcut if only one point in density is needed
if(vec_size(at) == 1){
return(density(x[["x"]], from = at, to = at, n = 1)$y)
if(zl == 1){
z[zi] <- density(x[["x"]], from = at, to = at, n = 1)$y
} else if (zl > 1) {
d <- density(x[["x"]], from = min(at), to = max(at), ..., na.rm=na.rm)
z[zi] <- stats::approx(d$x, d$y, xout = at)$y
}
d <- density(x[["x"]], from = min(at), to = max(at), ..., na.rm=na.rm)
stats::approx(d$x, d$y, xout = at)$y
z
}


Expand Down

0 comments on commit 21f6a63

Please sign in to comment.