Re: [Rd] Name length of function argument? (PR#10357)

From: Prof Brian Ripley <>
Date: Sat, 20 Oct 2007 11:44:52 +0100 (BST)

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, 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,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ mailing list
Received on Sat 20 Oct 2007 - 10:47:43 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 25 Oct 2007 - 11:37:11 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.