[R] How to use mle2 function?

Luigi Marongiu m@rong|u@|u|g| @end|ng |rom gm@||@com
Tue Jun 30 14:36:01 CEST 2020


Addendum:
the optimization actually got a worse outcome than the original
eyeball estimation:
```
actual <- 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)
# a = 3261, b = 10
raw_estim <- c(32.28713,  125.42308,  269.25688,  449.79310,  652.20000,
               863.20588, 1072.40940, 1272.58537,
1459.34254, 1630.50000, 1785.43439, 1924.52459, 2048.73234,
2159.31081, 2257.61538, 2344.98876,
2422.69666, 2491.89623, 2553.62473, 2608.80000, 2658.22736,
2702.60959, 2742.55803, 2778.60355,
2811.20690, 2840.76804, 2867.63450, 2892.10860, 2914.45377,
2934.90000, 2953.64844, 2970.87544,
2986.73591, 3001.36624, 3014.88679, 3027.40401, 3039.01225,
3049.79534, 3059.82788, 3069.17647,
3077.90062, 3086.05365, 3093.68343, 3100.83301, 3107.54118,
3113.84296, 3119.77003, 3125.35108,
3130.61216, 3135.57692, 3140.26694, 3144.70185, 3148.89962,
3152.87666, 3156.64800, 3160.22744,
3163.62765, 3166.86028, 3169.93605, 3172.86486)
# a = 3260.044, b = 73.15124
opt_estim <- c(38.52979,  316.81330,  721.54423, 1388.30154,
1945.64544, 2320.94319,
               2653.48033, 2861.46076,
2987.10641, 3061.17472, 3119.00396, 3158.51140, 3183.89232,
3202.55891, 3215.14235, 3225.31935,
3232.60583, 3238.21701, 3242.65745, 3245.58576, 3248.77411,
3250.68076, 3252.37050, 3253.63342,
3254.37708, 3254.96034, 3255.63152, 3256.09680, 3256.50500,
3256.82832, 3257.08020, 3257.41517,
3257.55425, 3257.64923, 3257.69986, 3257.77366, 3257.84871,
3257.90221, 3257.96386, 3258.00768,
3258.05952, 3258.10037, 3258.13871, 3258.17347, 3258.20611,
3258.23207, 3258.25176, 3258.27676,
3258.28907, 3258.29683, 3258.31112, 3258.32198, 3258.33703,
3258.35083, 3258.36237, 3258.37379,
3258.38203, 3258.38919, 3258.39528, 3258.40437)
plot(1:60, actual, lty = 1 , type = "l", lwd = 2,
     xlab = "Index", ylab = "Values")
points(1:60, raw_estim, lty = 2, type = "l")
points(1:60, opt_estim, lty = 3, type = "l")
legend("bottomright",
       legend = c("Actual values", "Raw estimate", "Optimized values"),
       lty = c(1, 2, 3), lwd = c(2, 1,1))
```

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



More information about the R-help mailing list