[R] In optim() function, second parameter in par() missing

David Winsemius dwinsemius at comcast.net
Mon Sep 5 17:41:58 CEST 2011


On Sep 5, 2011, at 10:30 AM, colstat wrote:

> Hi,
> First time using the optim(), can someone please tell me what I am  
> doing
> wrong?  The error looks like this
>
> Error in .Internal(pnorm(q, mean, sd, lower.tail, log.p)) :
>  'sd' is missing
>

You should be using a textbook. Which one are you consulting?

>
> An example of the error
> dat = c(20, 19, 9, 8, 7, 4, 3, 2)
> dat_mu=mean(dat)
> dat_s=sd(dat)
>
> max.func = function(dat, mu, sd) {
>    pnorm(dat, mu, sd)
> }
>
> optim(fn=max.func, dat=dat, par=c(mu=dat_mu, s=dat_s))
>
> I get sd is missing error.  If I wrote par=c(s=dat_s, mu=dat_mu) ,  
> then it
> tells me mu is missing.

You constructed a function with two arguments but optim only passes  
two argument to the objective function. Build your objective function  
a) so that it accepts two arguments and b) so that it returns one  
value. At the moment it does not do either.


> Can someone please help?
>

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list