Hi,
Im stuck with this code, wasted like 5h trying to find solution, thats why Im here asking you for advice. My task is to
1.create loglikelihood function for Poisson distribution (from function with 2 variables)
2. with maxNR (package maxLik) estimate the lambda value
I think function is calculated how it should be, still result looks a bit odd (less concerning)
the worst part is that I cant evaluate the lambda with maxNR becouse i have some red text and I dont know why. I will be very grateful for any response becouse I feel helpless.
library("maxLik")
library("rgl")
x = c(4,4,2,4,5,3,1,1,8,3,2,3,1,4,3,4,3,1,2,2,2,1,2,2,2,3,2,3,2,1,1,2,5,2,3,3,2,2,1,2,4,1,1,
1,1,3,1,6,4,5,1,1,3,1,3,2,2,1,1,2)
N = length(x)
loglik= function(k,lambda){
val = N*(-lambda)+sum(k)*log(lambda)-sum(log(factorial(k))+N*(-lambda)))
return(val)
}
k = seq(from=0, to=7 , by= 0.1)
(g=factorial(k))
lambda= seq(from=0, to=8 , by= 0.1)
fval = matrix(0,nrow=length(k),ncol=length(lambda))
for(i in 1:length(k)) {
for(j in 1:length(lambda)) {
fval[i,j] = loglik(k[i],lambda[j])
}
}
persp3d(k,lambda,exp(fval),col="skyblue")
image(k,lambda,fval)
image(k,lambda,exp(fval)
lnL= function(parametry){
k=param[1]
lambda=param[2]
val = N*(-lambda)+sum(k)*log(lambda)-sum(log(factorial(k))+N*(-lambda))
return(val)
}
Estimate = maxNR(fn=lnL,start = 0,0)
summary(Estimate)