[R] wrapping mle()

Gabor Grothendieck ggrothendieck at gmail.com
Sat Dec 30 06:30:23 CET 2006


Add the line marked ### so that the environment of loglik.fun is reset to
the environment within fit.mle so that it can find y there:

library(stats4)
ll <- function(ymax=15, xhalf=6) {
   -sum(stats::dpois(y, lambda=ymax/(1+x/xhalf), log=TRUE))
}
fit.mle <- function(FUN, x, y) {
   loglik.fun <- match.fun(FUN)
   environment(loglik.fun) <- environment() ###
   mle(loglik.fun, method="L-BFGS-B", lower=c(0, 0))
}
fit.mle("ll", x=0:10, y=c(26, 17, 13, 12, 20, 5, 9, 8, 5, 4, 8))



On 12/30/06, Sebastian P. Luque <spluque at gmail.com> wrote:
> Hi,
>
> How can we set the environment for the minuslog function in mle()?  The
> call in this code fails because the "ll" function cannot find the object
> 'y'.  Modifying from the example in ?mle:
>
>
> library(stats4)
> ll <- function(ymax=15, xhalf=6) {
>    -sum(stats::dpois(y, lambda=ymax/(1+x/xhalf), log=TRUE))
> }
> fit.mle <- function(FUN, x, y) {
>    loglik.fun <- match.fun(FUN)
>    mle(loglik.fun, method="L-BFGS-B", lower=c(0, 0))
> }
> fit.mle("ll", x=0:10, y=c(26, 17, 13, 12, 20, 5, 9, 8, 5, 4, 8))
>
>
> How should "fit.mle" be constructed so that "ll" works on the appropriate
> environment?  Thanks in advance for any advice on this.
>
>
> --
> Seb
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> 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.
>



More information about the R-help mailing list