From ripley at stats.ox.ac.uk  Sat Oct 20 12:44:52 2007
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Sat, 20 Oct 2007 11:44:52 +0100 (BST)
Subject: [Rd] Name length of function argument? (PR#10357)
In-Reply-To: <20071019160510.0B602283416A@mail.pubhealth.ku.dk>
References: <20071019160510.0B602283416A@mail.pubhealth.ku.dk>
Message-ID: <Pine.LNX.4.64.0710201129580.523@gannet.stats.ox.ac.uk>

Please study the help page before posting.  In the example code given 'p' 
is partially matching the second argument 'par' of optim(), so the unnamed 
first argument matches to the second argument 'fn'.

In the second case, 'f' matches 'par' and 'pp' matches '...', and 'f' is 
of 'type closure'.

On Fri, 19 Oct 2007, deiwiks at icr.gess.ethz.ch wrote:

> Hi,
> I've just been programming a function to calculate the likelihood in a
> probit model.
> The function looks like this
>
> likelihood <- function(Y,X,p) {
> Z <- p[1] +p[2]*X
> P <- Y*pnorm(Z) + (1-Y)*(1-pnorm(Z))
> -prod(P)
> }
>
> out <- optim(likelihood,p=c(0,0),Y=wells$switch,X=wells$dist, hessian=TRUE)
>
> X and Y are vectors containing column data.
> This works fine, however, if I rename p to pp or anything that is longer
> than one character, I get the following error message:
>
> Error in optim(f, pp = c(0, 0), Y=wells$switch, X=wells$dist,
> hessian=TRUE) :
>  cannot coerce type closure to double vector
>
> This seems like a bug... Or can you help me find the mistake I made?

Yes, it is a bug in your code.  From the R FAQ:

    If a command you are familiar with causes an R error message in a case
    where its usual definition ought to be reasonable, it is probably a
    bug.   If a command does the wrong thing, that is a bug.  But be sure
    you know for certain in what it ought to have done.  If you aren't
    familiar with the command, or don't know for certain how the command is
    supposed to work, then it might actually be working right.

Asking a question on R-bugs is an indication that you do not 'know for 
certain'.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] Name length of function argument? (PR#10357)
Prof Brian Ripley