EM algorithm error with while loop

Asked

Viewed 57 times

4

Hello, my project needs the EM algorithm below, where are all the codes. The error is in the loop while, which is where the hope and maximization steps are. The error message is:

"Error in while (abs(Elogv[r] - Elogv[r - 1]) >= 1e-06) { : Missing value Where TRUE/FALSE needed"**.

How do I resolve this error if the loop while does not contain commands TRUE and FALSE and, if I have already checked in detail that there is no error in the commands and there is no value NAs?

Code:

n=100
u<-runif(n)
QUANTIL <- function(u){
  Q <- rep(NA, length(u))
  for (i in 1:length(u)) {
    if(u[i] <  0.2634253829){
      Q[i] <- 1*tan(pi*(0.9490353482*u[i]-0.5))+0
    }
    if(u[i]>=0.2634253829 && u[i] < 0.7365746171){
      Q[i] <-  1*qnorm(1.4428629504*u[i]-0.2214315)+0
    }
    if(u[i]>0.7365746171){
      Q[i] <- 1*tan(pi*(0.9490353482*u[i]-0.4490353))+0
    } 
  }
  return(Q)
}
x<-QUANTIL(u)
y<-c(sort(x))
i<-seq(1,n)
v<-c(i/(n+1))

t<-QUANTIL(v)
mi<-median(y)
s<-c(y[26:73])
sigma<-sqrt(sum((s-mi)^2)/(n-1))
p=0.4731492342

alpha<-(2*t^3)/(1+t^2)^2
beta<-(1-t^2)/(1+t^2)^2
eta<-(t^4-t^2)/(1+t^2)^2
lambda<-2*t/(1+t^2)^2
gama<-(-t^2)
delta<-2*t

k<-((p*0.6930665173/sigma*sqrt(2*pi))*exp((-1/2*sigma^2)*((y-mi)^2)))/(((p*0.6930665173/sigma*sqrt(2*pi))*exp((-1/2*sigma^2)*(y-mi)^2))+((((1-p)*1.0537015317/sigma*pi))*(1/(1+((y-mi)/sigma)^2))))
r<-2
Elogv<-sum(k*((-1/2)*((y-mi)/sigma)^2))-sum(k*log(sigma*sqrt(2*pi)))-sum((1-k)*log(sigma*pi))-sum((1-k)*log(1+((y-mi)/sigma)^2))+sum(k*log(p))+(n-sum(k))*log(1-p)+log(0.6930665173)*sum(k)+log(1.0537015317)*sum(1-k)
Elogv[1]<-0

while (abs(Elogv[r]-Elogv[r-1])>=0.000001) {

  w<-(2*beta-2*k*beta+k)
  q<-k*delta+2*lambda*(1-k)
  sigma<-(sum(y*w)*sum(q)-sum(w)*sum(y*q))/(-2*sum(alpha*(1-k))*sum(q)+sum(w)*sum(k*gama-1)+2*sum(w)*sum(eta*(1-k)))                                                  
  mi<-(sum(y*w)+2*sigma*sum(alpha*(1-k)))/sum(w)
  k<-((p*0.6930665173/sigma*sqrt(2*pi))*exp((-1/2*sigma^2)*((y-mi)^2)))/(((p*0.6930665173/sigma*sqrt(2*pi))*exp((-1/2*sigma^2)*(y-mi)^2))+((((1-p)*1.0537015317/sigma*pi))*(1/(1+((y-mi)/sigma)^2))))
  Elogv[r]<-sum(k*((-1/2)*((y-mi)/sigma)^2))-sum(k*log(sigma*sqrt(2*pi)))-sum((1-k)*log(sigma*pi))-sum((1-k)*log(1+((y-mi)/sigma)^2))+sum(k*log(p))+(n-sum(k))*log(1-p)+log(0.6930665173)*sum(k)+log(1.0537015317)*sum(1-k)
  r<-r+1
  

}
  • 1

    I don’t know a program that interprets algorithms. The name of the program is Algorítimo EM? If that’s where I download and where I find the documentation?

  • This EM algorithm has no documentation, it’s statistical theory, it should have a package ready but it’s not used there, it’s done manually. The central point of the problem is the while loop at the end of the algorithm, which causes the error message. I put all the code so if someone wants to run the while loop, because it would need the codes from the beginning.

  • 2

    I’m no "R" expert, I don’t know anything about ,but I can read documentation and hunt bugs. In the documentation says the error happens because your comparison returns the value NA and an alternative is to use the isTRUE which returns the logical value of the expression and manipulates the result if it returns NA.Would be: while (isTRUE(abs(Elogv[r]-Elogv[r-1])>=0.000001, na=FALSE))

  • 2

    I leave as a comment and not as an answer because I don’t know anything about r and it would be frivolous of me to state an answer. So I leave a comment with my opinion and I hope it helps.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.