Hello,
I have the following error:
Error in while (abs(loglik[k] - loglik[k - 1]) >= 1e-04) { :
missing value where TRUE/FALSE needed
And not sure what is wrong?
here is my code:
while(abs(loglik[k]-loglik[k-1]) >= 0.0001) {
# E step
tau1<-pi1*dnorm(dat,mean=mu1,sd=sigma1)/(pi1*dnorm(x,mean=mu1,sd=sigma1)+pi2*dnorm(dat,mean=mu2,sd=sigma2)+pi3*dnorm(dat,mean=mu2,sd=sigma3)+pi4*dnorm(dat,mean=mu4,sd=sigma4))
tau2<-pi2*dnorm(dat,mean=mu2,sd=sigma2)/(pi1*dnorm(x,mean=mu1,sd=sigma1)+pi2*dnorm(dat,mean=mu2,sd=sigma2)+pi3*dnorm(dat,mean=mu2,sd=sigma3)+pi4*dnorm(dat,mean=mu4,sd=sigma4))
tau3<-pi3*dnorm(dat,mean=mu3,sd=sigma3)/(pi1*dnorm(x,mean=mu1,sd=sigma1)+pi2*dnorm(dat,mean=mu2,sd=sigma2)+pi3*dnorm(dat,mean=mu2,sd=sigma3)+pi4*dnorm(dat,mean=mu4,sd=sigma4))
tau4<-pi4*dnorm(dat,mean=mu4,sd=sigma4)/(pi1*dnorm(x,mean=mu1,sd=sigma1)+pi2*dnorm(dat,mean=mu2,sd=sigma2)+pi3*dnorm(dat,mean=mu2,sd=sigma3)+pi4*dnorm(dat,mean=mu4,sd=sigma4))
tau1[is.na(tau1)] <- 0.5
tau2[is.na(tau2)] <- 0.5
tau3[is.na(tau2)] <- 0.5
tau4[is.na(tau2)] <- 0.5
# M step
pi1<-mysum(tau1)/length(dat)
pi2<-mysum(tau2)/length(dat)
pi3<-mysum(tau3)/length(dat)
pi4<-mysum(tau4)/length(dat)
mu1<-mysum(tau1*x)/mysum(tau1)
mu2<-mysum(tau2*x)/mysum(tau2)
mu3<-mysum(tau3*x)/mysum(tau3)
mu4<-mysum(tau4*x)/mysum(tau4)
sigma1<-mysum(tau1*(x-mu1)^2)/mysum(tau1)
sigma2<-mysum(tau2*(x-mu2)^2)/mysum(tau2)
sigma3<-mysum(tau3*(x-mu3)^2)/mysum(tau3)
sigma4<-mysum(tau4*(x-mu4)^2)/mysum(tau4)
# loglik[k]<-sum(tau1*(log(pi1)+log(dnorm(x,mu1,sigma1))))+sum(tau2*(log(pi2)+log(dnorm(x,mu2,sigma2))))
loglik[k+1]<-mysum(tau1*(log(pi1)+logdnorm(x,mu1,sigma1)))+mysum(tau2*(log(pi2)+logdnorm(x,mu2,sigma2)))+mysum(tau3*(log(pi3)+logdnorm(x,mu3,sigma3)))+mysum(tau4*(log(pi4)+logdnorm(x,mu4,sigma4)))
k<-k+1
}
Thank you soo much in advance for any help!! Would be much appreciated