f = function (a,x) {alpha=a[1] ; beta = a[2] ; -sum(dgamma(x,shape=alpha,scale=beta,log=T)) } rx=rgamma(30,shape=10,scale=4) nlm(f,p=c(8,2),x=rx)