April 14, 2011
By

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),ylab=expression(mu),col=heat.colors(250))
}
```

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

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.

Tags: , , , ,