From iago.mosqueira at gmail.com  Tue Jun 26 18:06:38 2007
From: iago.mosqueira at gmail.com (Iago Mosqueira)
Date: Tue, 26 Jun 2007 18:06:38 +0200
Subject: [Rd] Behaviour of mle and environments or calling mle inside a
	function
Message-ID: <dba1e14b0706260906x70a7e017l6334a387cb05b83d@mail.gmail.com>

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



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