Instances of DensityModel store the estimated densities for each component and the overall (marginal) estimate of the density. The derived class DensityBatchModel additionally stores the density for each batch / component combination (i.e., if there are 3 components and 10 batches, there are 30 estimated densities). The intended use-case of the DensityModel class is to faciliate visualization of the estimated densities (see examples) as well as to provide an estimate of the number of modes in the overall density. If the number of estimated modes is smaller than the number of components of the best-fitting mixture model, post-hoc merging of components may be useful.

`component`

The component densities.

`overall`

The overall (marginal across batches and components) estimate of the density.

`modes`

A numeric vector providing the estimated modes in the overall density. The modes are defined by a crude estimate of the first derivative of the overall density (see

`findModes`

).`data`

A numeric vector containing the data

`clusters`

A vector providing the k-means clustering of the component means using the modes as centers. If an object of class

`DensityModel`

is instantiated with`merge=FALSE`

, this slot takes values 1, ..., K, where K is the number of components.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | ```
## marginal model
truth <- simulateData(N=2500, p=rep(1/3, 3),
theta=c(-1, 0, 1),
sds=rep(0.1, 3))
dm <- DensityModel(truth)
print(dm)
dm.merged <- DensityModel(truth, merge=TRUE)
print(dm.merged)
## here, because there are 3 distinct modes, specifying merge=TRUE
## does not change the resulting clusters
identical(clusters(dm), clusters(dm.merged))
## These objects can be plotted
plot(dm)
## Note that calling plot on a MixtureModel-derived object returns
## a density object as a side-effect of the plotting
dm2 <- CNPBayes::plot(truth)
identical(dm, dm2)
## batch model
k <- 3
nbatch <- 3
means <- matrix(c(-1.2, -1.0, -0.8,
-0.2, 0, 0.2,
0.8, 1, 1.2), nbatch, k, byrow=FALSE)
sds <- matrix(0.1, nbatch, k)
N <- 1500
truth <- simulateBatchData(N=N,
batch=rep(letters[1:3], length.out=N),
theta=means,
sds=sds,
p=c(1/5, 1/3, 1-1/3-1/5))
dm <- DensityModel(truth)
dm.merged <- DensityModel(truth, merge=TRUE)
print(dm)
dm2 <- CNPBayes::plot(truth)
identical(dm, dm2)
## suppress plotting of the batch-specific densities
CNPBayes::plot(dm2, show.batch=FALSE)
``` |

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.