MulaiMulai sekarang secara gratis

Mixture of three Gaussian distributions

What will change if we incorporate another distribution into our simulation? You will see that increasing the number of components will spread the mass density to include the extra distribution, but the logic still follows from the previous exercise.

Latihan ini adalah bagian dari kursus

Mixture Models in R

Lihat Kursus

Petunjuk latihan

  • Create assignments, which takes the values 0, 1 and 2 with a probability of 0.3, 0.4 and 0.3, respectively.
  • The data frame mixture samples from a Gaussian with a mean of 5 and sd of 2, when assignments is 1. If assignments is 2, the mean is 10 and sd is 1. Otherwise, is a standard normal distribution.
  • Plot the histogram with 50 bins.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

number_observations <- 1000

# Create the assignment object
assignments <- sample(
	c(0,1,2), size = number_observations, replace = TRUE, prob = c(0.3, ___, 0.3)
)

# Simulate the GMM with 3 distributions
mixture <- data.frame(
	x = ifelse(___ == 1, rnorm(n = number_observations, mean = ___, sd = ___), ifelse(assignments == 2, rnorm(n = number_observations, mean = ___, sd = ___), rnorm(n = ___)))
)

# Plot the mixture
mixture %>% 
  ggplot() + ___(aes(x = x, y = ..density..), ___ = ___)
Edit dan Jalankan Kode