DensityOps: Arithmetic operations on density estimates

Description Usage Arguments Value Examples

Description

This facility is mainly intended for generating mixtures of KDEs

Usage

1
2
## S3 method for class 'density'
Ops(e1, e2)

Arguments

e1

either "numeric" or "density" object

e2

either "numeric" or "density" object

Value

A KDE, or KDE-like object representing the operation.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
set.seed(1234)
y1 <- rnorm(90000, 0.0, 1.0)
y2 <- rnorm(10000, 2.5, 0.5)
d1 <- density(y1)
d2 <- density(y2)
d12 <- 0.9*d1 + 0.1*d2  ## 5% contaminated normal
plot(d12, main = "Normal with 5% contamination")
grid(lty = "dashed")
lines(0.9*d1, col = "dark green", lwd = 1.5)
lines(0.1*d2, col = "red", lwd = 1.5)
curve(0.9*dnorm(x, 0, 1) + 0.1*dnorm(x, 2.5, 0.5),
      col = "purple", lty = "dashed", add = TRUE)
dyy <- density(c(y1, y2))
plot(dyy, type = "n",
     main = "Density of mixture -vs- mixture of densities")
grid(lty = "dashed")
polygon(density_histogram(c(y1, y2), binWidth(0.3)),
        fill = alpha("sky blue", 0.5),
        colour = "transparent")
lines(dyy, col = "dark green")
lines(d12, col = "red", lty = "dashed", lwd = 2)

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