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)