Get startedGet started for free

Visualizing Univariate Gaussian Mixture Model

Since you fitted the model into fit_mix_example and extracted the parameters into comp_1, comp_2 and comp_3 (as well as the proportions), let's now plot the corresponding clusters with the density histogram.

To facilitate this last purpose, the fun_prop() function has been defined in the environment. This function provides the density values for a Gaussian distribution, like dnorm, but is extended to also accept the proportions.

This exercise is part of the course

Mixture Models in R

View Course

Exercise instructions

  • Plot the density histogram together with the density of each cluster. Remember that the data frame is called mix_example.
  • Use the function stat_function() with the argument fun equals fun_prop to draw the density distribution for each cluster.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

ggplot(___) + ___(aes(x = x, y = ..density..)) + 
  stat_function(geom = "line", fun = fun_prop, 
                args = list(mean = ___[1], sd = ___[2], 
                proportion = proportions[1])) +
  stat_function(geom = "line", fun = fun_prop, 
                args = list(mean = comp_2[1], sd = comp_2[2], 
                proportion = ___[2]))+
  stat_function(geom = "line", fun = ___, 
                args = list(mean = comp_3[1], sd = comp_3[2], 
                proportion = proportions[3]))
Edit and Run Code