Re: [R] problem using uniroot with integrate

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Fri 11 Mar 2005 - 02:56:04 EST

On Wed, 9 Mar 2005, Ken Knoblauch wrote:

> Thank you very much. Yes, that was the problem, partial matching.
> I saw a warning about that in integrate and some discussion from 1999
> in the archives and so added the m0 for integrate but somehow
> I wasn't bright enough to see that I had the same problem
> in uniroot.

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'.

> Sorry about no working example, but I'm not sure what I could
> have added, if I understand what you mean by working example,
> because my function wasn't working.

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

> best,
>
> ken
>
>
> Quoting Sundar Dorai-Raj <sundar.dorai-raj@pdf.com>:
>
>>
>>
>> Ken Knoblauch wrote on 3/9/2005 10:27 AM:
>>> Hi,
>>>
>>> I'm trying to calculate the value of the variable, dp, below, in the
>>> argument to the integral of dnorm(x-dp) * pnorm(x)^(m-1). This
>>> corresponds to the estimate of the sensitivity of an observer in an
>>> m-alternative forced choice experiment, given the probability of
>>> a correct response, Pc, a Gaussian assumption for the noise and
>>> no bias. The function that I wrote below gives me an error:
>>>
>>> Error in f(x, ...) : recursive default argument reference
>>>
>>> The problem seems to be at the statement using uniroot,
>>> because the furntion est.dp works fine outside of the main function.
>>> I've been using R for awhile but there are still many nuances
>>> about the scoping and the use of environments that I'm weak on
>>> and would like to understand better. I would appreciate any
>>> suggestions or solutions that anyone might offer for fixing
>>> my error. Thank you.
>>>
>>> dprime.mAFC <- function(Pc, m) {
>>> est.dp <- function(dp, Pc = Pc, m = m) {
>>>
>>> pr <- function(x, dpt = dp, m0 = m) {
>>> dnorm(x - dpt) * pnorm(x)^(m0 - 1)
>>> }
>>>
>>> Pc - integrate(pr, lower = -Inf, upper = Inf,
>>> dpt = dp, m0 = m)$value
>>> }
>>>
>>> dp.res <- uniroot(est.dp, interval = c(0,5), Pc = Pc, m = m)
>>> dp.res$root
>>> }
>>>
>>
>> Ken,
>>
>> Look at the argument list for ?uniroot and think "partial matching".
>> You're "m" is being interpretted for "maxiter". Simply change to
>>
>> dp.res <- uniroot(est.dp, interval = c(0,5), Pc = Pc, m0 = m)
>>
>> and in other places for consistency and the error goes away. Of course,
>> since you gave no working example, I'm not sure if other errors are
>> present that I'm missing.
>>
>> --sundar
>>
>
>
>
> ____________________
> Ken Knoblauch
> Inserm U 371
> Cerveau et Vision
> 18 avenue du Doyen Lepine
> 69675 Bron cedex
> France
> tel: +33 (0)4 72 91 34 77
> fax: +33 (0)4 72 91 34 61
> portable: 06 84 10 64 10
>
> ______________________________________________
> 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
>

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley@u.washington.edu	University of Washington, Seattle

______________________________________________
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 Received on Fri Mar 11 03:01:52 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:42 EST