deiwiks at icr.gess.ethz.ch
2007-Oct-19 16:05 UTC
[Rd] Name length of function argument? (PR#10357)
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? Thanks, Christa -- Christa Deiwiks International Conflict Research ETH Zurich Seilergraben 49 (SEI E.3) 8092 Zurich Ph: +41 44 632 67 60 http://www.icr.ethz.ch/people/deiwiks
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? >See ?optim. The first argument should be par, not fn. Duncan Murdoch
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