[Rd] strategies for incorporating a data= argument
Ben Bolker
bolker at zoo.ufl.edu
Thu Feb 8 15:46:01 CET 2007
As I've mentioned here, before, I'm working on an extended
version of mle(), a function from the stats4 package that's
a wrapper for optim().
I'd like (against the advice of Peter Dalgaard -- sorry) to
incorporate a "data" argument, similar to the arguments in
lm, nls, nlme, etc., that would allow the log-likelihood function
to be evaluated with different sets of data. (Peter's advice
was to use closures, writing a function-to-generate-likelihood-functions
such as
fn0 <- function(x) {
function(mu,sd) { -sum(dnorm(x,mu,sd,log=TRUE) }
}
mle(minuslogl=fn0(x),...)
My feeling is that this will be somewhat mysterious to
the intermediate R users who are my target audience.)
I have three thoughts on how to allow different data
sets to be substituted in the same objective function,
and I'm not sure which is best.
1. passed in ... as in optim()
advantages -- simple, not a lot of mucking around
with environments etc..
disadvantages -- have to separate out arguments that
are not intended for the objective function, either by
messing with the argument string (e.g. matching against
formals(fn)) or by isolating optim args in
an optim.args list
e.g.
fn <- function(mu,sd,x) {
-sum(dnorm(x,mu,sd))
}
mle(minuslogl=fn,...,x=x)
2. passed as a separate argument (data=), where
elements of data are taken as additional arguments to
the function (e.g. do.call("fn",c(args,data)))
e.g.
fn <- function(mu,sd,x) {
-sum(dnorm(x,mu,sd))
}
mle(minuslogl=fn,...,data=list(x=x))
3. passed as a separate argument, (data=), function
BODY is evaluated in an environment containing the
elements of data (or attach(data) before evaluating
function; or with(data,...))
advantages: works well for a formula interface
is there a better way to add objects from a list
to an environment than
mapply(function(name,obj) { assign(name,obj,envir=myenv) },
names(mylist),mylist)
?
e.g.
fn <- function(mu,sd) {
-sum(dnorm(x,mu,sd))
}
mle(minuslogl=fn,...,data=list(x=x))
For anyone who has read this far: right now I am calling my
extended function mle(), but that seems to be asking for trouble
[i.e. confused questions from users who don't know they're using
bbmle::mle and not stats4::mle]. Any recommendations for what to
call it? mle2? mlex ("extended mle") ? mlx?
thanks for any input,
Ben Bolker
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 254 bytes
Desc: OpenPGP digital signature
Url : https://stat.ethz.ch/pipermail/r-devel/attachments/20070208/4006ad2f/attachment.bin
More information about the R-devel
mailing list