0

I created a function which calculate the probability of observing an event. Here is the code:

eq1 <- function(theta1, theta2, lambda){
        vektorN = (1:100)
        loopVerdi = 0
            if(deltakere[i]>=2){        
                for (N in deltakere[i]:20000) {
                    density = (
                        (N) *
                        (N-1) * 
                        (pWEI2(bud[i], theta1, theta2))^(N-2) *
                        pWEI2(bud[i], theta1, theta2, lower.tail = FALSE) *
                        dWEI2(bud[i], theta1, theta2)) /
                        (1-(pWEI2(r, theta1, theta2))^N)

                    ngittN =
                        dbinom(deltakere[i], size = N, 
                               prob = pWEI2(r, theta1, theta2, lower.tail = FALSE))
                    sN = 
                        dpois(N, lambda)
                    loopVerdi = loopVerdi + (density * ngittN * sN)
                }
            return(loopVerdi)
            }
       }

I want to run the function for all my observation, which is fine when I do a simple for loop

ola <- 1:length(bud)
for (i in 1:length(bud)){
    ola[i] <- eq1(theta1=1, theta2=2, lambda=7)
}

Since I want to do a maximum likelihood method on my likelihood function, I need the loop above to be a function itself. But when I define the new function, it yields the same probability value for all my observations (which I know is wrong). What is wrong with the code below?

hello <- function(theta1, theta2, lambda) {
    ola <- 1:length(bud)
        for (i in 1:length(bud)){
            ola[i] <- eq1(theta1, theta2, lambda)
        }
    return(ola)
    }

test <- hello(theta1=1,theta2=2,lambda=7)
Ola
  • 81
  • 1
  • 8
  • can you provide a minimal reproducible example please - https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Bulat Feb 24 '19 at 17:50
  • It is not clear why you expect different results if you are always calling your function with the same parameters: `theta1`, `theta2`, `lambda` – Bulat Feb 24 '19 at 17:53
  • But observation i changes? I want to return a vector of probabilities form the vector ola. – Ola Feb 24 '19 at 20:28
  • you don't pass `i` here `eq1(theta1, theta2, lambda)` – Bulat Feb 24 '19 at 20:35
  • My god, I missed that! Thank you so much. I didn't even consider looking at my eq1 as I thought it was good. – Ola Feb 24 '19 at 21:48

0 Answers0