p
}
#starting values
#
p=p.0<-list(p1=1/3,p2=1/3,p3=1/3,m1=c(45,2),m2=c(60,4),m3=c(70,8),
s1=matrix(c(1,0,0,1),2,2),s2=matrix(c(1,0,0,1),2,2),
s3=matrix(c(1,0,0,1),2,2))
#--------------------------------------
lh=NULL
P=matrix(0,20,length(p))
for(i in 1:20){
p<-emiteration(Z,p,i)
lh[i]=sum(log(apply(Z,1,f.hat,p)))
}
X11()
plot(lh,type='o',xlab='iterations',ylab='likelihood')
#---------------------------------------
#---Graph the mixture:
n.l=m.l=30
x=seq(30,70,length= n.l)
y=seq(0,10,length = m.l)
xy=as.matrix(expand.grid(x,y))
# 1 Number of iterations
p=p.0
p<-emiteration(Z,p,1)
z=apply(xy,1,f.hat,p)
z.mat1=matrix(z,n.l,m.l)
#postrior
X11()
class.lab1=round(apply(Z,1,Estep1,p))
class.lab2=round(apply(Z,1,Estep2,p))
plot(Z, col=class.lab1+2)
contour(x,y,z.mat1,label="", add=T)
title(main=list(paste('iteration =', 1, sep=" "),font=3))
# 2 Number of iterations
p=p.0
p<-emiteration(Z,p,2)
z=apply(xy,1,f.hat,p)
z.mat1=matrix(z,n.l,m.l)
#postrior
X11()
class.lab1=round(apply(Z,1,Estep1,p))
class.lab2=round(apply(Z,1,Estep2,p))
plot(Z, col=class.lab1+2)
contour(x,y,z.mat1,label="", add=T)
title(main=list(paste('iteration =', 2, sep=" "),font=3))
# 5 Number of iterations
p=p.0
p<-emiteration(Z,p,5)
z=apply(xy,1,f.hat,p)
z.mat1=matrix(z,n.l,m.l)
#postrior
X11()
class.lab1=round(apply(Z,1,Estep1,p))
class.lab2=round(apply(Z,1,Estep2,p))
plot(Z, col=class.lab1+2)
contour(x,y,z.mat1,label="", add=T)
title(main=list(paste('iteration =', 5, sep=" "),font=3))