Get startedGet started for free

Plot the estimated clusters

At this point, you have discovered the two cluster in the data frame gaussian_sample. In this exercise, you will visualize how the estimated clusters for the iteration 10 fit the data. The vectors means_iter10 and props_iter10 are already saved in the environment.

To this end, you will use the ggplot2 function called stat_function(), which lets you superimpose a function on top of an existing plot. The function you will use a created curve function called fun_gaussian() which takes as arguments the mean and the proportion of the Gaussian.

This exercise is part of the course

Mixture Models in R

View Course

Exercise instructions

Plot the histogram in density mode of the variable x and add the estimated curves using stat_function() in combination with the function fun_gaussian().

Hands-on interactive exercise

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

___ %>% 
  ggplot() + geom_histogram(aes(x = ___, y = ___), bins = 200) +
  stat_function(geom = "line", fun = fun_gaussian,
                args = list(mean = means_iter10[1], proportion = ___[1])) +
  stat_function(geom = "line", fun = fun_gaussian,
                args = list(mean = ___[2], proportion = props_iter10[2]))
Edit and Run Code