On Sat, 30 Dec 2006, Sebastian P. Luque wrote: > On Sat, 30 Dec 2006 15:46:01 -0600 (CST), > Luke Tierney <[EMAIL PROTECTED]> wrote: > >> It is much cleaner to do this sort of thing with lexical scope. For >> example, > >> mkll <- function(x, y) { >> function(ymax=15, xhalf=6) { >> -sum(stats::dpois(y, lambda=ymax/(1+x/xhalf), log=TRUE)) >> } >> } > >> creates a log-likelihood likelyhood function for data x,y that can >> then be used by > >> fit.mle <- function(mkfun, x, y) { >> loglik.fun <- mkfun(x, y) >> mle(loglik.fun, method="L-BFGS-B", lower=c(0, 0)) >> } > >> as in > > >> > fit.mle(mkll, x=0:10, y=c(26, 17, 13, 12, 20, 5, 9, 8, 5, 4, 8)) > >> Call: >> mle(minuslogl = loglik.fun, method = "L-BFGS-B", lower = c(0, >> 0)) > >> Coefficients: >> ymax xhalf >> 24.999420 3.055779 > > Thanks Luke, this looks excellent. > > >> It is not clear why you want to be able to pass ll as a character string >> or why you want to assume that the thing passed in will refer to >> variables named 'x' and 'y', both usually bad ideas, so this specific >> approach may not apply, but something variant should. > > In the real case, I need to provide two different log likelihood > functions, and then tell fit.mle() which one to use in a given call. I > was actually defining 'x' and 'y' as formal arguments to fit.mle(). > Wouldn't that ensure that the original ll() would refer to the correct > variables?
No, since ll was defined at top level lexical scoping rules mean that free variables in the definition of ll are top level variables, regardless of where ll is called. > In any case, it was easy to use your suggestion almost by > direct analogy, which makes the code much more readable. Thanks a lot. > > In the case I describe though, why would it be a bad idea to use a string > to refer to the function, and then use match.fun()? I actually picked up > the idea from functions such as apply() and friends. Not bad just not necessary. Calling fit.mle(ll ...) will do and you then don't need match.call, which makes your code simpler. Best, luke > >> The ability to use environment(f)<-env to change the environment of a >> function is one of the most dubious language features of R (maybe the >> most dubious, though there are a couple of other strong contenders) and >> should not be used except in very rare circumstances. > > Keeping the lexical scoping technique you showed in mind should help stay > away from that. > > > Cheers, > > -- Luke Tierney Chair, Statistics and Actuarial Science Ralph E. Wareham Professor of Mathematical Sciences University of Iowa Phone: 319-335-3386 Department of Statistics and Fax: 319-335-3017 Actuarial Science 241 Schaeffer Hall email: [EMAIL PROTECTED] Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu ______________________________________________ R-help@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.