The main function, plot_GMM, is used for plotting output from Gaussian mixture models, including both densities and overlaying mixture weight component curves from the fit GMM. The package includes another function, plot_mix_comps, which is used in the plot_GMM function, and can be used in any plot generated by the user for overlaying mixture component curves from Gaussian mixture models. For the plot_mix_comps function, usage most often will be specifying the "fun" argument within "stat_function" in a ggplot2 object.
In collaboration with Fong Chun Chan @tinyheero,
plotGMM provides a custom function,
plot_mix_comps, for users interested in overlaying the components from a Gaussian mixture model. This allows for clean, precise plotting constraints, including mean (
mu), variance (
sigma), and mixture weight (
lambda) of the components. Specifically, the function superimposes the shape of the components over a
ggplot2 object. We demonstrate this both in the example in the package documentation, as well as below.
library(magrittr)library(ggplot2)library(mixtools)set.seed(1)mixmdl <- normalmixEM(faithful$waiting, k = 2)# Plot mixture components using the `plot_mix_comps` functiondata.frame(x = mixmdl$x) %>%ggplot() +geom_histogram(aes(x, ..density..), binwidth = 1, colour = "black",fill = "white") +stat_function(geom = "line", fun = plot_mix_comps,args = list(mixmdl$mu, mixmdl$sigma, lam = mixmdl$lambda),colour = "red", lwd = 1.5) +stat_function(geom = "line", fun = plot_mix_comps,args = list(mixmdl$mu, mixmdl$sigma, lam = mixmdl$lambda),colour = "blue", lwd = 1.5) +ylab("Density")
This package is the first iteration of a broader project that will eventually include several other plotting and fitting tools for Gaussian mixture models and the Expectation-Maximization (EM) algorithm.