# [R] How to use mle2 function?

Eric Berger er|cjberger @end|ng |rom gm@||@com
Tue Jun 30 12:03:18 CEST 2020

```Hi Luigi,
I took a quick look.

First error:
You wrote
O = mle2(minuslogl = holling, start = list(a = A, h = B, x = X))

it should be b=B  (h is not an argument of holling())
The error message gave very precise information!

Second error:
You wrote
O = mle2(minuslogl = nll, start = list(a = A, h = B),   data = list(n
= 57200000, k = A))
but the arguments to nll() are p,n,k. Setting start to values for a
and h causes the function to complain.

HTH,
Eric

On Tue, Jun 30, 2020 at 12:45 PM Luigi Marongiu
<marongiu.luigi using gmail.com> wrote:
>
> Hello,
> I would like to optimize the function:
> ```
> holling = function(a, b, x) {
>   y = (a * x^2) / (b^2 + x^2)
>   return(y)
> }
> ```
> I am trying to use the function mle2 from bbmle, but how do I need to
> feed the data?
> If I give `holling` as function to be optimized, passing the starting
> values for `a`, `b`, and `x`, I get:
> ```
> X = 1:60
> A = 3261
> B = 10
> O = mle2(minuslogl = holling, start = list(a = A, h = B, x = X))
> > Error in mle2(minuslogl = holling, start = list(a = A, b = B, x = X)) :
>   some named arguments in 'start' are not arguments to the specified
> log-likelihood function
> ```
> If I pass the negative log-function (assuming a binomial distribution
> of the data, which I am not sure about)
> ```
> nll = function(p, n, k) {
>   # extract parms
>   a = p[1]
>   h = p[2]
>   # calculate probability of attack
>   pred = a/(1+a*h*n)
>   # calc NLL
>   -sum(dbinom(k, prob = pred, size = n, log = TRUE))
> }
> ```
> then I get the same error:
> ```
> > O = mle2(minuslogl = nll, start = list(a = A, h = B),
> +          data = list(n = 57200000, k = A))
> Error in mle2(minuslogl = nll, start = list(a = A, h = B), data =
> list(n = 57200000,  :
>   some named arguments in 'start' are not arguments to the specified
> log-likelihood function
> ```
> but with the disadvantage of working on an assumed function (nll).
> How can I optimize the function `holling` properly?
> Thank you
>
>
>
>
> --
> Best regards,
> Luigi
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help