######################### # Gamma MLE using nlm() ##### gamma.mlen<- function(xx, shape0, scale0) { negLL <- function(p,x) { a<-p[1] b<-p[2] n<-length(x) ifelse(a <= 0 | b <= 0, 1e+50,-(-n*log(gamma(a))-n*a*log(b)+(a-1)*sum(log(x))-sum(x)/b)) } nlm(negLL, p = c(shape0, scale0), hessian = T, x = xx) } xx <- c(2.799, 5.857, 4.018, 7.461, 3.518, 3.560, 2.304, 4.114, 3.316, 7.430) gamma.mlen(xx,0.1,0.1)