Mixtures in Madrid (3)

April 14, 2011
By

(This article was first published on Xi'an's Og » R, and kindly contributed to R-bloggers)

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , , ,

Comments are closed.

Sponsors

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)