[R] Trouble with optim function

Ian Fiske ianfiske at gmail.com
Thu Feb 18 14:20:25 CET 2010


You're minimizing the log likelihood, but you want to minimize the *negative*
log likelihood.  Also, optimize() is better than optim() if you have a
function of only one argument.

Replace


Jon Moroney wrote:
> 
> #Create the log likelihood function
> LL<-function(x) {(trials*log(x))-(x*sumvect)}
> 
> optim(1,LL,method="BFGS")
> 

with

LL<-function(x) {-trials*log(x)+x*sumvect}
optimize(LL,c(0,1e5))

or better yet, to avoid errors and make generalizeable, do this

LL<-function(x) {-sum(dexp(vector,x,log=TRUE))}
optimize(LL,c(0,1e5)) 

-- 
View this message in context: http://n4.nabble.com/Trouble-with-optim-function-tp1559382p1560118.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list