@@ -148,10 +148,23 @@ compute_density <- function(x, w, from, to, bw = "nrd0", adjust = 1,
148148 bw <- precompute_bw(x , bw )
149149 # Decide whether to use boundary correction
150150 if (any(is.finite(bounds ))) {
151- dens <- stats :: density(x , weights = w , bw = bw , adjust = adjust ,
152- kernel = kernel , n = n )
151+ # To prevent discontinuities, we widen the range before calling the
152+ # unbounded estimator (#5641).
153+ bounds <- sort(bounds )
154+ range <- range(from , to )
155+ width <- diff(range )
156+ range [1 ] <- range [1 ] - width * as.numeric(is.finite(bounds [1 ]))
157+ range [2 ] <- range [2 ] + width * as.numeric(is.finite(bounds [2 ]))
158+ n <- n * (sum(is.finite(bounds )) + 1 )
153159
154- dens <- reflect_density(dens = dens , bounds = bounds , from = from , to = to )
160+ dens <- stats :: density(
161+ x , weights = w , bw = bw , adjust = adjust ,
162+ kernel = kernel , n = n , from = range [1 ], to = range [2 ]
163+ )
164+ dens <- reflect_density(
165+ dens = dens , bounds = bounds ,
166+ from = range [1 ], to = range [2 ]
167+ )
155168 } else {
156169 dens <- stats :: density(x , weights = w , bw = bw , adjust = adjust ,
157170 kernel = kernel , n = n , from = from , to = to )
0 commit comments