[R] Plotting the Log-Likelihood

enrico.foscolo2 at libero.it enrico.foscolo2 at libero.it
Tue Aug 25 16:46:02 CEST 2009


Dear Users,

I would like to plot the log-likelihood (depending on two 
parameters).

I wrote the following code:





	library(mvtnorm)


	sigma<-
matrix(c(4,2,2,3),ncol=2)

	x<-rmvnorm(n=500,mean=c(1,2),sigma=sigma)


	likel<-
function(param,data){

	
		pos1<-which(data[,1]<0)

		pos2<-which(data[,1]>=0)



		#density


			dens<-rep(0,nrow(data))
	
			dens[pos1]<-dmvnorm(data[pos1,])
*param[1]
	
			dens[pos2]<-dmvnorm(data[pos2,])*param[2]


		#return log-
likelihood


			sum(log(dens))

	
}


	#plotting the log-likelihood

		param1<-
seq(from=0,to=1,length=200)

		param2<-seq(from=0,to=1,length=200)

		combin<-
as.matrix(expand.grid(param1,param2,KEEP.OUT.ATTRS=FALSE))

		likel.values<-
apply(combin,1,likel,data=x)

		matr.likel.values<-matrix(likel.values,200,200)


		persp(param1,param2,matr.likel.values,

			xlab="param1",ylab="param2",

			
zlab="Likelihood",

			theta=5,phi=20,expand=0.5,

			col="lightblue",
ltheta=120,shade=0.75,ticktype="detailed")




It is all ok, but I am searching 
for a new "lighting" way in order to earn time.

I need an alternative function 
for apply(), when I calculate the log-likelihood for every single couple of 
values (param1,param2).

Do you have any ideas?

Thank you in advance,




Enrico Foscolo




More information about the R-help mailing list