Re: [R] maximizing a likelihood function containing an integral
Sam Wong yahoo.com> writes: > > > Hi, R Users; > > I am trying to maximize a likelihood function which > contains an integral. The integral contains the > unknown parameter as well. I am trying to use the > following code to do the maximization: > [snip] > The error message is: > Error in log(x) : Non-numeric argument to mathematical > function > The only obvious (?) mistake in the code is that you refer to integrate(...)$integral -- the help page for ?integrate says that $value is the estimate of the integral, so you might be getting a NULL here. However, the code you gave us is not reproducible/self-contained -- since I don't know what z1, z2, n1, rho are, I can't run it and see if that's the only problem. cheers Ben Bolker __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] maximizing a likelihood function containing an integral
Hi, R Users; I am trying to maximize a likelihood function which contains an integral. The integral contains the unknown parameter as well. I am trying to use the following code to do the maximization: ll<-function(b.vec){ b0<-b.vec[1] b1<-b.vec[2] b2<-b.vec[3] p<-1/(1+exp(-b0-b1*z1-b2*x2)) lik1<-p^y*(1-p)^(1-y)*exp(-(z1^2+x2^2-2*rho*z1*x2)/(2*(1-rho^2))) log.lik1<-sum(log(lik1[1:n1])) log.lik2<-0 for(j in (n1+1):n){ integrand<-function(u,B0,B1,B2){ exp(-y[j]*(B0+B1*u+B2*x2[j])-(u-rho*x2[j])^2/2)/(1+exp(B0+B1*u+B2*x2[j])) } log.lik2<-log.lik2+log(integrate(integrand,lower=1,upper=Inf,B0=b0,B1=b1,B2=b2)$integral) } log.lik<-log.lik1+log.lik2 } start<-c(0,0,0) nlminb(start,ll) The error message is: Error in log(x) : Non-numeric argument to mathematical function Suggestions are welcome. Thanks Ming Ji __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.