Mixtures in Madrid (3)

[This article was first published on Xi'an's Og » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

For my second lecture today, I need to plot a likelihood surface for a basic two-component mixture with only the means unknown: here is the R code to speed up things

llsurf=function(trumyn=2.,wayt=.3,var2=1.,ssiz=500){
# draws the log-likelihood surface and a random sample
sd2=sqrt(var2)
parti=(runif(ssiz)>wayt)
sampl=(1-parti)*rnorm(ssiz)+parti*(trumyn+sd2*rnorm(ssiz))
mu2=mu1=seq(min(sampl),max(sampl),.1)
mo1=mu1%*%t(rep(1,length(mu2)))
mo2=(rep(1,length(mu2)))%*%t(mu2)
ca1=-0.5*mo1*mo1
ca2=-0.5*mo2*mo2
like=.1*(ca1+ca2) # log prior N(0,10)
for (i in 1:ssiz)
like=like+log(wayt*dnorm(sampl[i],mo1,sd2)+(1-wayt)*dnorm(sampl[i],mo2,1))
par(mar=c(4,4,1,1))
image(mu1,mu2,like,xlab=expression(mu[1]),ylab=expression(mu[2]),col=heat.colors(250))
contour(mu1,mu2,like,levels=seq(min(like),max(like),(max(like)-min(like))/50),add=T)
}

resulting in an outcome (very) similar to the above.


Filed under: R, Statistics, University life Tagged: log-likelihood, mixtures, R

To leave a comment for the author, please follow the link and comment on their blog: Xi'an's Og » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)