Skip to contents

stat_normalmix_line() fits a Normal mixture model, by default with normalmixEM(). Predicted values are computed and, by default, plotted.

Usage

stat_normalmix_line(
  mapping = NULL,
  data = NULL,
  geom = "line",
  position = "identity",
  ...,
  method = "normalmixEM",
  se = NULL,
  seed = NA,
  fm.values = FALSE,
  n = min(100 + 50 * k, 300),
  fullrange = TRUE,
  level = 0.95,
  method.args = list(),
  k = 2,
  free.mean = TRUE,
  free.sd = TRUE,
  components = "all",
  n.min = 10L * k,
  na.rm = FALSE,
  orientation = "x",
  show.legend = NA,
  inherit.aes = TRUE
)

Arguments

mapping

The aesthetic mapping, usually constructed with aes. Only needs to be set at the layer level if you are overriding the plot defaults.

data

A layer specific dataset, only needed if you want to override the plot defaults.

geom

The geometric object to use display the data

position

The position adjustment to use for overlapping points on this layer.

...

other arguments passed on to layer. This can include aesthetics whose values you want to set, not map. See layer for more details.

method

function or character If character, "normalmixEM" or the name of a model fit function are accepted, possibly followed by the fit function's method argument separated by a colon. The function must return a model fit object of class mixEM.

se

Currently ignored.

seed

RNG seed argument passed to set.seed(). Defaults to NA, which means that set.seed() will not be called.

fm.values

logical Add parameter estimates and their standard errors to the returned values (`FALSE` by default.)

n

Number of points at which to evaluate the model prediction.

fullrange

Should the prediction span the combined range of the scale and of the fitted distributions, or just span the range of the data?

level

Level of confidence interval to use (0.95 by default).

method.args

named list with additional arguments.

k

integer Number of mixture components to fit.

free.mean, free.sd

logical If TRUE, allow the fitted mean and/or fitted sd to vary among the component Normal distributions.

components

character One of "all", "sum", or members select which densities are returned.

n.min

integer Minimum number of distinct values in the mapped variable for fitting to the attempted.

na.rm

a logical indicating whether NA values should be stripped before the computation proceeds.

orientation

character Either "x" or "y", the mapping of the values to which the mixture model is to be fitetd. NOT YET IMPLEMENTED!

show.legend

logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders.

Value

The value returned by the statistic is a data frame, with n rows of predicted density for each component of the mixture plus their sum and the corresponding vector of x values. Optionally it will also include additional values related to the model fit.

Details

This statistic is similar to stat_density but instead of fitting a single distribution it can fit a mixture of two or more Normal distributions, using an approach related to clustering. Defaults are consistent between stat_normalmix_line() and stat_normalmix_eq(). Parameter seed if not NA is used in a call to set.seed() immediately before calling the model fit function. As the fitting procedure makes use of the (pseudo-)random number generator (RNG), convergence can depend on it, and in such cases setting seed to the same value in stat_normalmix_line() and in stat_normalmix_eq() can ensure consistency, and more generally, reproducibility.

A mixture model as described above, is fitted for k >= 2, while k == 1 is treated as a special case and a Normal distribution fitted with function fitdistr(). In this case the SE values are exact estimates.

Computed variables

stat_normalmix_line() provides the following variables, some of which depend on the orientation:

x

the n values for the quantiles

component

A factor indexing the components and/or their sum

If fm.values = TRUE is passed then columns with diagnosis and parameters estimates are added, with the same value in each row within a group:

n

numeric the number of x values

.size

numeric the number of density values

fm.class

character the most derived class of the fitted model object

fm.method

character the method, as given by the ft field of the fitted model objects

This is wasteful and disabled by default, but provides a simple and robust approach to achieve effects like colouring or hiding of the model fit line by group depending on the outcome of model fitting.

Aesthetics

stat_normalmix_eq expects observations mapped to x from a numeric variable. A new grouping is added by mapping component to the group aesthetic. Additional aesthetics as understood by the geom ("geom_line" by default) can be set.

See also

Other ggplot statistics for mixture model fits.: stat_normalmix_eq()

Examples

ggplot(faithful, aes(x = waiting)) +
  stat_normalmix_line()

#> number of iterations= 27 

# ggplot(faithful, aes(y = waiting)) +
#  stat_normalmix_line(orientation = "y")

ggplot(faithful, aes(x = waiting)) +
  stat_normalmix_line(components = "sum")

#> number of iterations= 25 

ggplot(faithful, aes(x = waiting)) +
  stat_normalmix_line(components = "members")

#> number of iterations= 45 

ggplot(faithful, aes(x = waiting)) +
 geom_histogram(aes(y = after_stat(density)), bins = 20) +
 stat_normalmix_line(aes(colour = after_stat(component),
                         fill = after_stat(component)),
                     geom = "area", linewidth = 1, alpha = 0.25, se = FALSE)

#> number of iterations= 39 

ggplot(faithful, aes(x = waiting)) +
 stat_normalmix_line(aes(colour = after_stat(component),
                         fill = after_stat(component)),
                     geom = "area", linewidth = 1, alpha = 0.25,
                     components = "members", se = FALSE)

#> number of iterations= 55 

ggplot(faithful, aes(x = waiting)) +
 stat_normalmix_line(geom = "area", linewidth = 1, alpha = 0.25,
                     colour = "black", outline.type = "upper",
                     components = "sum", se = FALSE)

#> number of iterations= 31 

# special case of no mixture
ggplot(subset(faithful, waiting > 66), aes(x = waiting)) +
  stat_normalmix_line(k = 1)
#> With k = 1 one Normal distribution is fitted. Irrelevant parameters ignored!


# Inspecting the returned data using geom_debug()
gginnards.installed <- requireNamespace("gginnards", quietly = TRUE)

if (gginnards.installed)
  library(gginnards)

if (gginnards.installed)
  ggplot(faithful, aes(x = waiting)) +
    stat_normalmix_line(geom = "debug", components = "all")

#> number of iterations= 20 
#> [1] "PANEL 1; group(s) comp.1  , comp.2  , comp.sum; 'draw_function()' input 'data' (head):"
#>          x component      density flipped_aes PANEL    group            y
#> 1 35.29541  comp.sum 1.092383e-04       FALSE     1 comp.sum 1.092383e-04
#> 2 35.29541    comp.1 1.092383e-04       FALSE     1   comp.1 1.092383e-04
#> 3 35.29541    comp.2 9.599551e-15       FALSE     1   comp.2 9.599551e-15
#> 4 35.61753  comp.sum 1.306554e-04       FALSE     1 comp.sum 1.306554e-04
#> 5 35.61753    comp.1 1.306554e-04       FALSE     1   comp.1 1.306554e-04
#> 6 35.61753    comp.2 1.457516e-14       FALSE     1   comp.2 1.457516e-14
#>   orientation
#> 1           x
#> 2           x
#> 3           x
#> 4           x
#> 5           x
#> 6           x

if (gginnards.installed)
  ggplot(faithful, aes(x = waiting)) +
    stat_normalmix_line(geom = "debug", components = "sum")

#> number of iterations= 18 
#> [1] "PANEL 1; group(s) comp.sum; 'draw_function()' input 'data' (head):"
#>          x component      density flipped_aes PANEL    group            y
#> 1 35.29551  comp.sum 0.0001092388       FALSE     1 comp.sum 0.0001092388
#> 2 35.61764  comp.sum 0.0001306561       FALSE     1 comp.sum 0.0001306561
#> 3 35.93977  comp.sum 0.0001558029       FALSE     1 comp.sum 0.0001558029
#> 4 36.26190  comp.sum 0.0001852310       FALSE     1 comp.sum 0.0001852310
#> 5 36.58403  comp.sum 0.0002195556       FALSE     1 comp.sum 0.0002195556
#> 6 36.90616  comp.sum 0.0002594587       FALSE     1 comp.sum 0.0002594587
#>   orientation
#> 1           x
#> 2           x
#> 3           x
#> 4           x
#> 5           x
#> 6           x

if (gginnards.installed)
  ggplot(faithful, aes(x = waiting)) +
    stat_normalmix_line(geom = "debug", components = "members")

#> number of iterations= 16 
#> [1] "PANEL 1; group(s) comp.1, comp.2; 'draw_function()' input 'data' (head):"
#>          x component      density flipped_aes PANEL  group            y
#> 1 35.29541    comp.1 1.092383e-04       FALSE     1 comp.1 1.092383e-04
#> 2 35.29541    comp.2 9.599803e-15       FALSE     1 comp.2 9.599803e-15
#> 3 35.61754    comp.1 1.306554e-04       FALSE     1 comp.1 1.306554e-04
#> 4 35.61754    comp.2 1.457553e-14       FALSE     1 comp.2 1.457553e-14
#> 5 35.93967    comp.1 1.558017e-04       FALSE     1 comp.1 1.558017e-04
#> 6 35.93967    comp.2 2.206367e-14       FALSE     1 comp.2 2.206367e-14
#>   orientation
#> 1           x
#> 2           x
#> 3           x
#> 4           x
#> 5           x
#> 6           x

if (gginnards.installed)
  ggplot(faithful, aes(x = waiting)) +
    stat_normalmix_line(geom = "debug", fm.values = TRUE)

#> number of iterations= 22 
#> [1] "PANEL 1; group(s) comp.1  , comp.2  , comp.sum; 'draw_function()' input 'data' (head):"
#>          x component      density converged   n .size fm.class   fm.method
#> 1 35.29549  comp.sum 1.092387e-04      TRUE 272   600    mixEM normalmixEM
#> 2 35.29549    comp.1 1.092387e-04      TRUE 272   600    mixEM normalmixEM
#> 3 35.29549    comp.2 9.604483e-15      TRUE 272   600    mixEM normalmixEM
#> 4 35.61762  comp.sum 1.306560e-04      TRUE 272   600    mixEM normalmixEM
#> 5 35.61762    comp.1 1.306560e-04      TRUE 272   600    mixEM normalmixEM
#> 6 35.61762    comp.2 1.458255e-14      TRUE 272   600    mixEM normalmixEM
#>   flipped_aes PANEL    group            y orientation
#> 1       FALSE     1 comp.sum 1.092387e-04           x
#> 2       FALSE     1   comp.1 1.092387e-04           x
#> 3       FALSE     1   comp.2 9.604483e-15           x
#> 4       FALSE     1 comp.sum 1.306560e-04           x
#> 5       FALSE     1   comp.1 1.306560e-04           x
#> 6       FALSE     1   comp.2 1.458255e-14           x