Mixture Models in R
Victor Medina
Researcher at The University of Edinburgh
# The number of observations
number_of_obs <- 500
# Simulate the coin
coin <- sample(c(0,1), size = number_of_obs,
replace = TRUE, prob = c(0.5, 0.5))
head(coin)
0 1 0 1 0 0
table(coin)
coin
0 1
239 261
# Gaussian 1 "heads" gauss_1 <- rnorm(n = number_of_obs, mean = 5, sd = 2) # Gaussian 2 "tails" gauss_2 <- rnorm(n = number_of_obs)
# Simulate the mixture mixture_simulation <- ifelse(coin, gauss_1, gauss_2) head(cbind(coin, gauss_1, gauss_2, mixture_simulation))
coin gauss_1 gauss_2 mixture_simulation
[1,] 0 7.378712 -0.4559596 -0.4559596
[2,] 1 6.102770 3.3595880 6.1027696
[3,] 0 5.707269 -0.0731496 -0.0731496
[4,] 1 3.592059 -1.2407104 3.5920586
[5,] 0 5.236851 -0.5110058 -0.5110058
[6,] 0 4.152619 -0.5124031 -0.5124031
# Transform to a data frame
mixture_simulation <- data.frame(x = mixture_simulation)
# Create the histogram
ggplot(mixture_simulation) +
geom_histogram(aes(x = x, ..density..), bins = 40)
# Simulate the coin with different proportions
coin <- sample(c(0,1), size = number_of_obs,
replace = TRUE, prob = c(0.8, 0.2))
# Simulate the mixture
mixture_simulation <- data.frame(x = ifelse(coin, gauss_1, gauss_2))
# Create the histogram
ggplot(mixture_simulation) +
geom_histogram(aes(x = x, ..density..), bins = 40)
proportions <- sample(c(0, 1, 2), number_of_obs,
replace = TRUE, prob = c(1/3, 1/3, 1/3))
gauss_3 <- rnorm(n = number_of_obs, mean = 10, sd = 1)
mixture_simulation <- data.frame(x = ifelse(proportions == 0, gauss_1,
ifelse(proportions == 1, gauss_2, gauss_3)))
ggplot(mixture_simulation) +
geom_histogram(aes(x = x, ..density..), bins = 40)
Mixture Models in R