Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.
dnorm2.p <- function(x,p)   p[1]*dnorm(x,p[2],p[3]) + (1-p[1])*dnorm(x,p[4],p[5])
f.neglog <- function(p)   -sum(log(dnorm2.p(x,p)))
start.params <- c(0.7,-400,100,600,50)
n2.fit <- optim(start.params,f.neglog)

π = 0.8512394  # weight
μ1 = -229.1567182
σ1 = 174.326821
μ2 = 646.6475601
σ2 = 214.1101274

How can I plot a mixture distribution and compare it to a kernel density. I already know how to plot the kernel density like:

hist(x,freq=F)
lines(density(x),col="purple")
share|improve this question
3  
Please, define your notations a little bit (although I guess weight is the mixing parameter, and the rest stand for mean and SD of the distributions). If the question is purely about R plotting functionalities, it belongs on Stack Overflow. If you are interested in comparing mixture and kernel distributions by any statistical means, you may want to define precisely what you have in mind. – chl Nov 1 '12 at 17:39
You may also want to have a look at the VGAM package, see my answer here: stackoverflow.com/questions/13499969/… – nico Mar 2 at 6:51

closed as off topic by whuber May 1 at 16:33

Questions on Cross Validated are expected to relate to statistics within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

You could try this code:

with(n2.fit, curve(expr=par[1]*dnorm(x,par[2],par[3])+
    (1-par[1])*dnorm(x,par[4],par[5]), add=TRUE))
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.