I'm trying to figure out the best way of fitting the same negative log-likelihood function to more than one set of data, using mle() from the stats4 package.


Here's what I would have thought would work:

--------------
library(stats4)

## simulate values
r = rnorm(1000,mean=2)

## very basic neg. log likelihood function
mll <- function(mu,logsigma) {
  -sum(dnorm(r,mean=mu,sd=exp(logsigma),log=TRUE))
}


mle(minuslogl=mll,start=list(mu=1,logsigma=0))

r2 = rnorm(1000,mean=3) ## second "data set"
with(list(r=r2),
     mle(minuslogl=mll,start=list(mu=1,logsigma=0))
   )
-------------

but this doesn't work -- it fits to the original data set, not the new one --- presumably because mll() picks up its definition of r when it is *defined* -- so using with() at this point doesn't help.

  If I rm(r) then I get an 'Object "r" not found' error.


I can do something like the following, defining the negative log-likelihood function within the mle() call ...


lf = function(data) {
  mle(minuslogl=function(mu,logsigma) {
    -sum(dnorm(data,mean=mu,sd=exp(logsigma),log=TRUE))
  },start=list(mu=1,logsigma=0))
}

lf(r)
lf(r2)

-------

... and in this case there's no point using with().
can someone help me understand this behavior and to find a clean way to use mle() on a predefined likelihood function that allows substitution of an arbitrary data set?


R 2.0.0 on Gentoo (trying to stick with the package management system so haven't installed 2.0.1 yet)


thanks, Ben Bolker

--
620B Bartram Hall                            [EMAIL PROTECTED]
Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolker
Box 118525                                   (ph)  352-392-5697
Gainesville, FL 32611-8525                   (fax) 352-392-3704

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to