Title: | Tools for Visualizing Gaussian Mixture Models |
---|---|
Description: | The main function, plot_GMM, is used for plotting output from Gaussian mixture models (GMMs), including both densities and overlaying mixture weight component curves from the fit GMM. The package also include the function, plot_cut_point, which plots the cutpoint (mu) from the GMM over a histogram of the distribution with several color options. Finally, the package includes the function, plot_mix_comps, which is used in the plot_GMM function, and can be used to create a custom plot for overlaying mixture component curves from GMMs. For the plot_mix_comps function, usage most often will be specifying the "fun" argument within "stat_function" in a ggplot2 object. |
Authors: | Philip Waggoner [aut, cre], Fong Chan [aut] |
Maintainer: | Philip Waggoner <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2.2 |
Built: | 2024-10-25 04:47:07 UTC |
Source: | https://github.com/cran/plotGMM |
Returns a plot of the data density (histogram) with an overlaid cut point generated by a Gaussian mixture model
plot_cut_point(m, plot = TRUE, color = c("grayscale", "amerika", "wesanderson"))
plot_cut_point(m, plot = TRUE, color = c("grayscale", "amerika", "wesanderson"))
m |
An object of class |
plot |
A logical argument for generating the plot. If FALSE, only the cut point value from the GMM is returned. If TRUE, histogram with the overlaid cut point is returned. Default is set to TRUE. |
color |
A vector of color options including "amerika" (from package |
Gaussian mixture models are often used to derive cut points, or lines of separation between clusters in feature space (See Benaglia et al. 2009 for more). The plot_cut_point
function plots data densities with the overlaid cut point (the mean of the calculated mu
) from mixEM
objects, which are GMM's fit using the mixtools
package.
Benaglia, T., Chauveau, D., Hunter, D. and Young, D. 2009. mixtools: An R package for analyzing finite mixture models. Journal of Statistical Software, 32(6), pp.1-29.
Ram, K., and Wickham, H. 2015. wesanderson: a Wes Anderson palette generator. R package version 0.3.
mixmdl <- mixtools::normalmixEM(faithful$waiting, k = 2) plot_cut_point(mixmdl, plot = TRUE, color = "amerika") # returns plot, amerika plot_cut_point(mixmdl, plot = TRUE, color = "wesanderson") # returns plot, wesanderson plot_cut_point(mixmdl, plot = FALSE) # returns only the cut point value from the GMM
mixmdl <- mixtools::normalmixEM(faithful$waiting, k = 2) plot_cut_point(mixmdl, plot = TRUE, color = "amerika") # returns plot, amerika plot_cut_point(mixmdl, plot = TRUE, color = "wesanderson") # returns plot, wesanderson plot_cut_point(mixmdl, plot = FALSE) # returns only the cut point value from the GMM
Generates a plot of data densities with overlaid mixture components from a Gaussian mixture model (GMM)
plot_GMM(m, k=NULL)
plot_GMM(m, k=NULL)
m |
An object of class |
k |
The number of components specified in the GMM, |
Uses ggplot2 graphics to plot data densities with overlaid components from mixEM
objects, which are GMM's fit using the mixtools
package.
Note: Users must enter the same component value, k
, in the plot_GMM
function, as that which was specified in the original GMM specification (also k
in mixtools
).
Benaglia, T., Chauveau, D., Hunter, D. and Young, D., 2009. mixtools: An R package for analyzing finite mixture models. Journal of Statistical Software, 32(6), pp.1-29.
Wickham, H., 2016. ggplot2: elegant graphics for data analysis. Springer.
set.seed(235) mixmdl <- mixtools::normalmixEM(faithful$waiting, k = 2) plot_GMM(mixmdl, 2)
set.seed(235) mixmdl <- mixtools::normalmixEM(faithful$waiting, k = 2) plot_GMM(mixmdl, 2)
Plots a mixture component conditioned on a superimposed function
plot_mix_comps(x, mu, sigma, lam)
plot_mix_comps(x, mu, sigma, lam)
x |
Input data |
mu |
Mean of component |
sigma |
Variance of component |
lam |
Mixture weight of component |
Allows for specifying a custom function to be superimposed when plotting a mixture component
set.seed(1) mixmdl <- mixtools::normalmixEM(faithful$waiting, k = 2) x <- mixmdl$x x <- data.frame(x) ggplot2::ggplot(data.frame(x)) + ggplot2::geom_density(ggplot2::aes(x), color="black", fill="black") + ggplot2::stat_function(geom = "line", fun = plot_mix_comps, args = list(mixmdl$mu[1], mixmdl$sigma[1], lam = mixmdl$lambda[1]), colour = "red") + ggplot2::stat_function(geom = "line", fun = plot_mix_comps, args = list(mixmdl$mu[2], mixmdl$sigma[2], lam = mixmdl$lambda[2]), colour = "blue")
set.seed(1) mixmdl <- mixtools::normalmixEM(faithful$waiting, k = 2) x <- mixmdl$x x <- data.frame(x) ggplot2::ggplot(data.frame(x)) + ggplot2::geom_density(ggplot2::aes(x), color="black", fill="black") + ggplot2::stat_function(geom = "line", fun = plot_mix_comps, args = list(mixmdl$mu[1], mixmdl$sigma[1], lam = mixmdl$lambda[1]), colour = "red") + ggplot2::stat_function(geom = "line", fun = plot_mix_comps, args = list(mixmdl$mu[2], mixmdl$sigma[2], lam = mixmdl$lambda[2]), colour = "blue")