Search code examples
rdensity-plotmixture

How do I find a true mixture density in R?


I don't understand how to find a plot of the true mixture density. In the following, I have provided the code which creates a function for the normal mixture. And I understand how to create each individual density plot, but I do not know how to find the true density.

NormalMix <- function(n,omega,mu1,mu2,sigma1,sigma2) {
z <- sample(2,n,replace=T,prob=c(omega,1-omega))
n1 <- sum(z==1)
n2 <- sum(z==2)
z[z==1] <- rnorm(n1,mu1,sigma1)
z[z==2] <- rnorm(n2,mu2,sigma2)
z
}

Simulated Data


Solution

  • I and my professor wrote this paper: D. S. Young, X. Chen, D. C. Hewage, and R. N. Poyanco (2018). “Finite Mixture-of-Gamma Preparation Distributions: Estimation, Inference, and Model-Based Clustering.”

    And he also has an R library, which is called mixtools: https://cran.r-project.org/web/packages/mixtools/vignettes/mixtools.pdf

    You can use the wait1 <- normalmixEM(waiting, lambda = .5, mu = c(55, 80), sigma = 5) for example, on page 6, to extimate the parameters.

    This library doesn't need you to provide the mean and standard deviation, it will estimate for you based on the range you provided. And you can use native R plot function to generate nice plots.

    I hope it helps.