[R] Help MLE

Ben Bolker bolker at ufl.edu
Fri Oct 10 00:15:33 CEST 2008

LFRC <feliperiehs <at> yahoo.com.br> writes:

> Dear, 
> I'm starting on R language. I would like some help to implement a MLE
> function.
> I wish to obtain the variables values (alpha12, w_g12, w_u12) that maximize
> the function LL = Y*ln(alpha12 + g*w_g12 + u*w_u12).

  You're running into a problem because your linear combination
goes negative in the course of the optimization, which makes
the logarithm NaN, which crashes the optimizer.  The quick
hack would be to calculate the linear combination and then
set it to max(value,some_small_positive_number) -- this will
(probably) work if the solution is not on the boundary.
Alternatively you can use constrOptim (see below),
because you need to impose a linear inequality constraint.


Model = function(alpha12,w_g12,w_u12)
    Y = 1
    u = 0.5
    g = -1
    comb = alpha12 + g*w_g12 + u*w_u12
    r = Y*log(comb)
    cat(alpha12,w_g12,w_u12,comb,log(comb),r,"\n") ## debug

res = mle(minuslog=Model,start=list(alpha12=0.1,w_u12=0.1,w_g12=0.1),
  method = "BFGS")

model2 <- function(p) {

            ui = c(1,-1,0.5),
            ci = rep(0,3))

  Ben Bolker

More information about the R-help mailing list