Package 'plotGMM'

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

Help Index


Plots Cut Point from Gaussian Mixture Models

Description

Returns a plot of the data density (histogram) with an overlaid cut point generated by a Gaussian mixture model

Usage

plot_cut_point(m, plot = TRUE, color = c("grayscale", "amerika", "wesanderson"))

Arguments

m

An object of class mixEM corresponding with the fit GMM

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 amerika), "wesanderson" (from package wesanderson), and "grayscale", which is the default option.

Details

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.

References

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.

Examples

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

Plots Mixture Components from Gaussian Mixture Models

Description

Generates a plot of data densities with overlaid mixture components from a Gaussian mixture model (GMM)

Usage

plot_GMM(m, k=NULL)

Arguments

m

An object of class mixEM corresponding with the fit GMM

k

The number of components specified in the GMM, m

Details

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).

References

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.

Examples

set.seed(235)
mixmdl <- mixtools::normalmixEM(faithful$waiting, k = 2)

plot_GMM(mixmdl, 2)

Custom Function for Overlaying Mixture Components

Description

Plots a mixture component conditioned on a superimposed function

Usage

plot_mix_comps(x, mu, sigma, lam)

Arguments

x

Input data

mu

Mean of component

sigma

Variance of component

lam

Mixture weight of component

Details

Allows for specifying a custom function to be superimposed when plotting a mixture component

Examples

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")