density: Density class

Description Usage Arguments Value Examples

Description

Promotion of an S3 class to virtual S4

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
density(x, ...)

## S4 method for signature 'numeric'
density(
  x,
  n = 4096,
  from,
  to,
  ...,
  lower = -Inf,
  upper = Inf,
  normalize = TRUE,
  eps = .Machine$double.eps
)

## S4 method for signature 'formula'
density(x, data = parent.frame(2), ...)

Arguments

x

numeric object or formula

...

additional arguments to be passed on to methods

n

integer: number of points to use in the output

from, to

lower and upper values of x to use in the output

lower, upper

lower and upper known limits to the support, and hence points at which to fold the density

eps

just the tiniest bit, so tiny you would never see it in a million years...

data

data frame or environment from which to get the data

normalise

logical: should the result be re-normalized to have total area unity?

Value

a density object

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
set.seed(123)
oldPar <- par(bg = "beige", font = 2, mar = c(5,5,4,1)+0.1, las = 1)
for(N in c(50, 500, 5000)) {
  x <- rexp(N, 2)
  dx <- density(x)
  dx0 <- update(dx, lower = 0)
  plot(dx0, col = "red", lwd = 2, bty = "n", main = "KDE Estimation",
       xlab = "x", sub = paste("Sample size =", N),
       ylab = expression(f[X](x)*',  '*X*' ~ '*exponential(2)),
       xlim = xrange(dx, dx0),
       ylim = yrange(dexp(0, 2), dx, dx0))
  lines(dx, col = "grey", lwd = 2)
  curve(dexp(x, 2), add = TRUE, n = 1000, col = "dark green", xpd = NA)
  lines(density_histogram(x, binWidth(0.125)), col = alpha("blue", 0.5))
  rug(x, col = alpha("red", (order(order(x))/N)^0.25))
  grid()
  legend("center", text.font = 2,
         c("naive KDE", "KDE folded at 0", "naive density histogram", "true density"),
         col = c("grey", "red", alpha("blue", 0.5), "dark green"),
         lty = "solid", lwd = 2, cex = 0.8, bty = "n",
         title = "Method")
}
par(oldPar)

BillVenables/WWRGraphics documentation built on Feb. 1, 2021, 12:13 p.m.