This vignette offers a few basic examples to help users get started
with plotmm
.
The package has five functions:
plot_mm()
: The main function of the package,
plot_mm
allows the user to simply input the name of the fit
mixture model, as well as an optional argument to pass the number of
components k
that were used in the original fit.
Note: the function will automatically detect the number of
components if k
is not supplied. The result is a tidy
ggplot of the density of the data with overlaid mixture weight component
curves. Importantly, as the grammar of graphics is the basis of
visualization in this package, all other tidyverse-friendly
customization options work with any of the plotmm
’s
functions (e.g., customizing with ggplot2
’s functions like
labs()
or theme_*()
; or
patchwork
’s plot_annotation()
). There are
examples of these and others below.
plot_cut_point()
: Mixture models are often used to
derive cut points of separation between groups in feature space.
plot_cut_point()
plots the data density with the overlaid
cut point (the mean of the calculated mu
) from the fit
mixture model.
plot_mix_comps()
: A helper function allowing for
expanded customization of mixture model plots. The function superimposes
the shape of the components over a ggplot2
object. This
function is also used to render all plots in the main
plot_mm()
function.
plot_gmm()
: The original function upon which the
package was expanded. It is included in plotmm
for quicker
access to a common mixture model form (univariate Gaussian), as well as
to bridge between the original plotGMM
package.
plot_mix_comps_normal()
: Similarly, this function is
the original basis of plot_mix_comps()
, but for Gaussian
mixture models only. It is included in plotmm
for bridging
between the original plotGMM
package.
The package supports several model objects (from ‘mixtools’, ‘EMCluster’, and ‘flexmix’), as well as many mixture model specifications, including mixtures of:
First, load the stable version from CRAN, along with some additional packages.
if (!requireNamespace("EMCluster", quietly = TRUE)) {
install.packages("EMCluster")
}
if (!requireNamespace("flexmix", quietly = TRUE)) {
install.packages("flexmix")
}
if (!requireNamespace("mixtools", quietly = TRUE)) {
install.packages("mixtools")
}
if (!requireNamespace("ggplot2", quietly = TRUE)) {
install.packages("ggplot2")
}
if (!requireNamespace("plotmm", quietly = TRUE)) {
install.packages("plotmm")
}
library(plotmm)
plot_mm()
First, here is an example for univariate normal mixture model:
library(mixtools)
library(ggplot2)
set.seed(576)
mixmdl <- normalmixEM(iris$Petal.Length, k = 2)
# visualize
plot_mm(mixmdl, 2) +
labs(title = "Univariate Gaussian Mixture Model",
subtitle = "Mixtools Object")
Next is an example of a mixture of linear regressions:
library(mixtools)
library(ggplot2)
# set up the data (replication of mixtools examples for comparability)
data(NOdata)
attach(NOdata)
set.seed(100)
out <- regmixEM(Equivalence, NO, verb = TRUE, epsilon = 1e-04)
df <- data.frame(out$beta)
# visualize
plot_mm(out) +
labs(title = "Mixture of Regressions",
subtitle = "Mixtools Object")
Next is a bivariate Gaussian mixture model (via
EMCluster
). Note: in this case, all plots print by
default for full display of options. Use indexing (e.g.,
plot[1]
) to plot a specific or preferred quantity.
plot_cut_point()
(with the
amerika color
palette)plot_mix_comps()
library(mixtools)
library(magrittr)
library(ggplot2)
# Fit a univariate mixture model via mixtools
set.seed(576)
mixmdl <- normalmixEM(faithful$waiting, k = 2)
# Customize a plot with `plot_mix_comps_normal()`
data.frame(x = mixmdl$x) %>%
ggplot() +
geom_histogram(aes(x, ..density..), binwidth = 1, colour = "black",
fill = "white") +
stat_function(geom = "line", fun = plot_mix_comps_normal, # here is the function
args = list(mixmdl$mu[1], mixmdl$sigma[1], lam = mixmdl$lambda[1]),
colour = "red", lwd = 1.5) +
stat_function(geom = "line", fun = plot_mix_comps_normal, # here again as k = 2
args = list(mixmdl$mu[2], mixmdl$sigma[2], lam = mixmdl$lambda[2]),
colour = "blue", lwd = 1.5) +
ylab("Density")
Anyone is welcome to contribute to the package. Before collaborating, please take a look at and abide by the contributor code of conduct. Here’s a sampling of how to contribute:
Submit an issue reporting a bug, requesting a feature enhancement, etc.
Suggest changes directly via pull request
Reach out directly with ideas if you’re uneasy with public interaction