R/plot_cut_point.R

Defines functions plot_cut_point

Documented in plot_cut_point

#'Plots Cut Point from Gaussian Mixture Models
#'
#'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"))
#'@param m An object of class \code{mixEM} corresponding with the fit GMM
#'@param 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.
#'@param color A vector of color options including "amerika" (from package \code{amerika}), "wesanderson" (from package \code{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 \code{plot_cut_point} function plots data densities with the overlaid cut point (the mean of the calculated \code{mu}) from \code{mixEM} objects, which are GMM's fit using the \code{mixtools} package.
#'@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
#'@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.
#'@references Ram, K., and Wickham, H. 2015. wesanderson: a Wes Anderson palette generator. R package version 0.3.
#'
#'@export
plot_cut_point <- function(m, plot = TRUE, color = c("grayscale", "amerika", "wesanderson")) {
  if (!requireNamespace("ggplot2", quietly = TRUE)) {
    stop("Package \"ggplot2\" needed for this function to work. Please install it.",
         call. = FALSE)
  }
  if (!requireNamespace("mixtools", quietly = TRUE)) {
    stop("Package \"mixtools\" needed for this function to work. Please install it.",
         call. = FALSE)
  }
  if (!requireNamespace("methods", quietly = TRUE)) {
    stop("Package \"methods\" needed for this function to work. Please install it.",
         call. = FALSE)
  }
  if (!requireNamespace("amerika", quietly = TRUE)) {
    stop("Package \"amerika\" needed for this function to work. Please install it.",
         call. = FALSE)
  }
  if (!requireNamespace("wesanderson", quietly = TRUE)) {
    stop("Package \"wesanderson\" needed for this function to work. Please install it.",
         call. = FALSE)
  }

  m <- try(methods::as(m, "mixEM", strict=TRUE))
  if (!inherits(m, "mixEM")){
    stop("must be a mixEM object", call. = FALSE)
  }

  x <- m$x
  p <- ggplot2::ggplot(data.frame(m$x))

  if(plot == TRUE){
    if (color == "amerika"){
      a <- p + ggplot2::geom_histogram(ggplot2::aes(m$x),
                                       colour = "darkgray",
                                       fill = amerika::amerika_palette(n = 30, name = "Dem_Ind_Rep7", type = "continuous")) +
        ggplot2::geom_vline(xintercept = mean(m$mu)) +
        ggplot2::theme_bw() +
        ggplot2::labs(x = "x",
                      y = "Density",
                      title = "Cutpoint from Gaussian Mixture Model")
      a
    } else if (color == "wesanderson"){
      w <- p + ggplot2::geom_histogram(ggplot2::aes(m$x),
                                       colour = "darkgray",
                                       fill = wesanderson::wes_palette(n = 30, name = "Rushmore1", type = "continuous")) +
        ggplot2::geom_vline(xintercept = mean(m$mu)) +
        ggplot2::theme_bw() +
        ggplot2::labs(x = "x",
                      y = "Density",
                      title = "Cutpoint from Gaussian Mixture Model")
      w
    } else {
      g <- p + ggplot2::geom_histogram(ggplot2::aes(m$x),
                                       colour = "darkgray",
                                       fill = "lightgray") +
        ggplot2::geom_vline(xintercept = mean(m$mu)) +
        ggplot2::theme_bw() +
        ggplot2::labs(x = "x",
                      y = "Density",
                      title = "Cutpoint from Gaussian Mixture Model")
      g
    }

  } else mean(m$mu)

}

Try the plotGMM package in your browser

Any scripts or data that you put into this service are public.

plotGMM documentation built on July 8, 2020, 7:26 p.m.