punstress
punstress

Reputation: 1196

R program keeps getting numeric(0) answer

I am a beginner in R. Here's the formula I'm trying to code to find the lambda that maximizes the log likelihood of some bigrams. When the bigrams are not found, the P_b (bigram) function fails, but the P_u (unigram) function should provide the unigram result (lambda = 0).

It works for bigrams that are found. When they're not found, tho, I only get numeric(0), not the unigram result.

enter image description here

p.mix <- function(w2, w1) {

  (1-lambda) * uni.dfrm$prob[uni.dfrm$token==w2] + lambda * p.bi(w2,w1)

}

The p.bi() function looks complicated because of the indexing so I'm reluctant to post it but it does work when the bigrams are found. It just looks up the count of times w' appears after w and divides it by the times w appears, but I have to go through another vector of vocabulary words so it looks ugly.

When w' is never found occurring after w, instead of a zero count, there's no row at all, which is what apparently causes the numeric(0) result. That's what the mixed model is supposed to solve, but I can't get it to work. Any ideas how this can work?

Upvotes: 1

Views: 1456

Answers (1)

agstudy
agstudy

Reputation: 121568

You can add a test for the case where w2 is numeric(0) for example :

p.mix <- function(w2, w1) {
  if(length(w2)>0){
    res <- (1-lambda) * uni.dfrm$prob[uni.dfrm$token==w2] +
                lambda * p.bi(w2,w1)
  }else res <- 0

  res
}

EDIT

p.mix <- function(w2, w1) {
  if(length(w2) && length(uni.dfrm$prob[uni.dfrm$token==w2]) > 0)
    (1-lambda) * uni.dfrm$prob[uni.dfrm$token==w2] + lambda * p.bi(w2,w1)
  else 0
}

Upvotes: 1

Related Questions