[R] question mle again

Bert Gunter gunter.berton at gene.com
Mon Feb 7 18:40:01 CET 2011


On Mon, Feb 7, 2011 at 9:21 AM, Joshua Wiley <jwiley.psych at gmail.com> wrote:
>
> Hi,
> When a function cannot find a variable inside its own environment, it
> will look to its parent environment.


This is false. It will "look to" its **enclosing environment" /
"enclosure" . See
?environment

(Note: This is fundamental to R scoping)

-- Bert


>
> If you define a function in the
> global environment, the global environment is its parent environment.
> However, if you define a function in the global environment, but then
> proceed to use lapply() with another function, the actual variable
> ll() needs to access is neither passed to II (so it is not in its
> environment) nor is it in the global environment (II's parent
> environment).  It is in the function in lapply's environment, which is
> inaccessible to II.  I have made some small changes to your code that
> gets around this, but I am still not convinced this is really doing
> what you want, but that is a whole other question/problem.
>
> Also, for future reference, you are more likely to get a response/help
> if you mention the required packages.  I made educated guesses, that
> you are using mle() from "stats4" and count() from "plyr" (I realize
> you may not even be aware that those functions came from non-default
> loading packages).
>
> HTH,
>
> Josh
>
> Here are my edits to your code:
>
> foo <- function(x) {
>  ## load required packages (guessing here)
>  require(stats4)
>  require(plyr)
>
>  ## define ll function _inside_ foo
>  ## this is important if you want it to have access
>  ## to arguments in foo
>  ll <- function(lambda = 1) {
>    cat("x in ll()", x, "\n")
>    y.fit <- dpois(x, lambda)
>    sum( (y - y.fit)^2 )
>  }
>
>  ## Your calculations
>  ## (though I'm not convinced this is what you really want)
>  raw.data <- rpois(100, lambda.data[x])
>  freqTab <- count(raw.data)
>  x <- freqTab$x
>  y <- freqTab$freq / sum(freqTab$freq)
>
>  cat("x in lapply", x, "\n")
>  fit <- mle(ll)
>  coef(fit)
> }
>
> ## Data
> lambda.data <- runif(10, 0.5, 10)
> ## Run it through lapply for x = 1:10
> lapply(1:10, FUN = foo)
>
>
> >
> > Here is a little example which show my problem:
> >
> > # R-code ---------------------------------
> >
> > lambda.data <- runif(10,0.5,10)
> >
> > ll <- function(lambda = 1) {
> >       cat("x in ll()",x,"\n")
> >       y.fit <- dpois(x, lambda)
> >
> >       sum( (y - y.fit)^2 )
> >
> >       }
> >
> > lapply(1:10, FUN = function(x){
> >
> >       raw.data <- rpois(100,lambda.data[x])
> >
> >       freqTab <- count(raw.data)
> >       x <- freqTab$x
> >       y <- freqTab$freq / sum(freqTab$freq)
> >       cat("x in lapply", x,"\n")
> >       fit <- mle(ll)
> >
> >       coef(fit)
> >       })
> >
> > Can anybody help?
> >
> > Antje
> >
> > ______________________________________________
> > R-help at r-project.org 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.
> >
>
>
>
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> University of California, Los Angeles
> http://www.joshuawiley.com/
>
> ______________________________________________
> R-help at r-project.org 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.



--
Bert Gunter
Genentech Nonclinical Biostatistics



More information about the R-help mailing list