# [R] Help MLE

LFRC feliperiehs at yahoo.com.br
Fri Oct 10 14:32:45 CEST 2008

```Dear Ben Bolker,

Thanks a lot for your help.

I have two more questions:

1) My goal is maximize the function (>     r = Y*log(comb)) but the
parameters found, minimized the function (r = Y*log(comb)).

2) What this function do?
> model2 <- function(p) {
>   do.call("Model",as.list(p))
> }

Best regards,
LFRC

Ben Bolker wrote:
>
> 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.
>
>  rm(list=ls())
>  ls()
>  library(stats4)
>
> 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
>     r
>   }
>
> res = mle(minuslog=Model,start=list(alpha12=0.1,w_u12=0.1,w_g12=0.1),
>   method = "BFGS")
>
> model2 <- function(p) {
>   do.call("Model",as.list(p))
> }
>
> constrOptim(theta=c(0.1,0.1,0.1),f=model2,
>             ui = c(1,-1,0.5),
>             ci = rep(0,3))
>
>
>   Ben Bolker
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help