[Rd] Behaviour of mle and environments or calling mle inside a function

Iago Mosqueira iago.mosqueira at gmail.com
Tue Jun 26 18:06:38 CEST 2007


Dear all,

I would appreciate some help understanding the following behaviour
when stats4::mle is called inside a function. mle seems to look for
its arguments in R_GlobalEnv and not the environment from which it is
called.


library(stats4)

lkhd <- function(alpha=1, beta=0.1, sigma=0.1)
	- sum(dnorm(log(rec), log(alpha*ssb/(beta+ssb)), sqrt(sigma), TRUE))

object <- list(lkhd=lkhd, rec=1:10, ssb=1:10)

foo <- function(x)
{
	rec <- x$rec
	ssb <- x$ssb

	mle(x$lkhd)
}

foo(object)


This fails with

Error in log(rec) : object "rec" not found


rec <- object$rec
ssb <- object$ssb

foo(object)

and this works.

Using R version 2.5.0 (2007-04-23), on Linux (Ubuntu) 2.6.17

Many thanks,


Iago Mosqueira



More information about the R-devel mailing list