[R] How to use mle2 function?
peter dalgaard
pd@|gd @end|ng |rom gm@||@com
Wed Jul 1 11:38:10 CEST 2020
The basic problem is that holling() is not a (negative) loglikelihood function. nll() _is_ a negative loglikelihood, but it is not clear for what. You appear to be very confused as to what a likelihood even is (what is k? apparently your response variable? Then how can it be a scalar if X is a vector? etc.), so I think you need to read up a bit.
-pd
> On 30 Jun 2020, at 14:22 , Luigi Marongiu <marongiu.luigi using gmail.com> wrote:
>
> No, I got the same. I reckon the problem is with X: this was I scalar,
> I was providing a vector with the actual values.
> Ho can mle2 optimize without knowing what are the actual data? and
> what values should I give for X?
> Thank you
>
> On Tue, Jun 30, 2020 at 2:06 PM Eric Berger <ericjberger using gmail.com> wrote:
>>
>> I have no problem with the following code:
>>
>> library(bbmle)
>> holling <- function( a, b, x ) {
>> a*x^2 / (b^2 + x^2)
>> }
>> A=3261
>> B=10
>> X=30
>> foo <- mle2( minuslogl=holling, start=list(a=A,b=B,x=X) )
>>
>> foo
>>
>> # Call:
>> # mle2(minuslogl = holling, start = list(a = A, b = B, x = X))
>>
>> # Coefficients:
>> # a b x
>> # 3.260044e+03 7.315124e+01 -2.332448e-14
>>
>> # Log-likelihood: 0
>>
>>
>> Does this code create a problem for you?
>>
>> On Tue, Jun 30, 2020 at 3:00 PM Luigi Marongiu <marongiu.luigi using gmail.com> wrote:
>>>
>>> Sorry for the typo, but I have the same error if using b instead of h:
>>> ```
>>>> O = mle2(minuslogl = holling, start = list(a = A, b = B))
>>>> Error in minuslogl(a = 3261, b = 10) :
>>> argument "x" is missing, with no default
>>> # let's add x
>>> X = c(8, 24, 39, 63, 89, 115, 153, 196, 242, 287, 344, 408, 473,
>>> 546, 619, 705, 794, 891, 999, 1096, 1242, 1363, 1506, 1648, 1753,
>>> 1851, 1987, 2101, 2219, 2328, 2425, 2575, 2646, 2698, 2727, 2771, 2818,
>>> 2853, 2895, 2926, 2964, 2995, 3025, 3053, 3080, 3102, 3119, 3141, 3152,
>>> 3159, 3172, 3182, 3196, 3209, 3220, 3231, 3239, 3246, 3252, 3261)
>>> O = mle2(minuslogl = holling, start = list(a = A, b = 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
>>> ```
>>> And even if I use the log-likelihood function:
>>> ```
>>> O = mle2(minuslogl = nll, start = list(p = c(A, B), n = 57200000, x = X))
>>>> Error in mle2(minuslogl = nll, start = list(p = c(A, B), n = 57200000, :
>>> some named arguments in 'start' are not arguments to the specified
>>> log-likelihood function
>>> ```
>>>
>>> On Tue, Jun 30, 2020 at 12:03 PM Eric Berger <ericjberger using gmail.com> wrote:
>>>>
>>>> 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
>>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>>
>>>
>>> --
>>> Best regards,
>>> Luigi
>
>
>
> --
> Best regards,
> Luigi
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk Priv: PDalgd using gmail.com
More information about the R-help
mailing list