[R] why isn't integrate function working in a likelihood
stats12
skarmv at gmail.com
Mon Oct 29 19:56:26 CET 2012
Dear R users,
I have been trying to solve for mle's of a function that involves an
integral and I keep getting an error. I created an example to work on first
and even the simple example doesn't give me the mle's. I am getting the
error "Error in integrate(integrand, 0, Inf) : non-finite function value".
I divided my likelihood function into two parts, one part involves integral
the other part doesn't. I don't see anything wrong in my code. Any hint
would be greatly appreciated. Thank you.
Z<-rbinom(20,1,0.5) #covariate
time<-rgamma(20,shape=2,scale=0.5) #failure time
del<-c(0,0,1,1,0,1,1,1,0,1,0,0,1,1,0,1,1,1,0,1) #event indicator
ll<-function(p){
loglik1<-del*log(exp(Z*p[3])) #log of the first part of the likelihood
function
haz<-(time*exp(Z*p[3]))*p[1]
ll2<-vector("numeric",length(1:20))
for (s in 1:20){
integrand<-function(x)
x^(del[s]+1/p[2]-1)*exp(-x/p[2])*exp(-x*haz[s])/(gamma(1+1/p[2])*p[2]^(1/p[2]))
ll2[s]<-log(integrate(integrand,0,Inf)$value)
}
loglik2<-as.vector(ll2) #second part of the likelihood function
lik<-sum(loglik1+loglik2)
-lik
}
initial<-c(1,1,1)
t<-nlm(ll,initial)
Error in integrate(integrand, 0, Inf) : non-finite function value
ll(c(1,1,1))
[1] 26.65748
--
View this message in context: http://r.789695.n4.nabble.com/why-isn-t-integrate-function-working-in-a-likelihood-tp4647787.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list