[R] Computing Maximum Loglikelihood With "nlm" Problem

Edward Wijaya ewijaya at gmail.com
Thu May 22 04:05:02 CEST 2008


Hi,

I tried to compute maximum likelihood under gamma distribution,
using nlm function. The code is this:

__BEGIN__

vsamples<- c(103.9, 88.5, 242.9, 206.6, 175.7, 164.4)

mlogl <- function(alpha, x) {
    if (length(alpha) > 1) stop("alpha must be scalar")
    if (alpha <= 0) stop("alpha must be positive")
    return(- sum(dgamma(x, shape = alpha, log = TRUE)))
 }

mlogl_out <- nlm(mlogl, mean(vsamples),vsamples=vsamples)
print(mlogl_out)

__END__

However, it gives the following error:

Error in f(x, ...) :
  unused argument(s) (vsamples = c(103.9, 88.5, 242.9, 206.6, 175.7, 164.4))
Calls: nlm -> <Anonymous> -> f
Execution halted


What's wrong in my way of calling 'nlm" function?
Please advice.

- Edward



More information about the R-help mailing list