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 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. Can someone please help? Thanks! Colin -- View this message in context: http://r.789695.n4.nabble.com/In-optim-function-second-parameter-in-par-missing-tp3791391p3791391.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2011-Sep-05 15:41 UTC
[R] In optim() function, second parameter in par() missing
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