Skip to content
Prev 26482 / 398502 Next

Help on R commands

You're not too far off.
  You need to do something like

objfun <- function(p) {
  mu <- p[1]
  sigma <- p[2]
  -sum(dnorm(x,mean=mu,sd=sigma,log=TRUE))
}

optim(par=c(0,1),fn=objfun)  # check ?optim for correct argument names

Some points:
  - the main thing is that you have to define mu and sigma within your 
objective function
  - you're better off with the log-likelihood than the likelihood; by 
convention people usually try to minimize the negative log-likelihood 
rather than maximizing the likelihood
  - "nlmin" is an S-PLUS function.  The analogues in R are nlm and optim.
  - your setting negative values to zero complicates your problem 
considerably.  Can you use a gamma model instead?
  - the ML estimate of normal data (given that you haven't messed with 
negative values) is mu=mean(x), sd=sd(x)*sqrt(length(x)/(length(x)-1)) = 
sqrt(mean((x-mean(x))^2)) 

  A lot of these questions are pretty basic; you really need to try to get
some help off-list.  You may not get too much more help here unless you
explain what your situation is (are you doing this for a class,
independent project, etc.) and give some indication that you've exhausted
local resources ...

 cheers,
  Ben Bolker
On Wed, 18 Dec 2002 Jameshutton25 at aol.com wrote: