Skip to content
Prev 65709 / 398506 Next

problem using uniroot with integrate

On Wed, 9 Mar 2005, Ken Knoblauch wrote:

            
It is perhaps worth pointing out that in R you usually don't need to pass 
these extra arguments down

  dprime.mAFC <- function(Pc, m) {
              est.dp <- function(dp) {

                pr <- function(x) {
                      dnorm(x - dp) * pnorm(x)^(m - 1)
                          }

                Pc - integrate(pr, lower = -Inf, upper = Inf)$value
              }

      dp.res <- uniroot(est.dp, interval = c(0,5))
      dp.res$root
      }

Because pr() is defined inside est.dp, which is defined inside 
dprime.mAFC, the arguments are already accessible.

In most cases, passing extra arguments through a higher-order function is 
not needed in R, as lexical scope allows the function to have access to 
the information directly.  You also don't have to keep thinking up 
different names for 'm'.
A set of arguments at which you wanted to evaluate the function, so that 
a) we could get the same error that you did
b) we could tell when it worked.

 	-thomas
Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle