data(pollen)
func <- function(x,l){
ifelse(any(l<0),Inf,
lfactorial(sum(x)) -sum(lfactorial(x))
+lgamma(sum(l)) +sum(lgamma (x+l))
-sum(lgamma(l)) -lgamma(sum (x+l))
)
}
start_vec <- c(51.6, 1, 5.3 , 2)
optim(start_vec , function(l){ -sum(apply(pollen , 1, FUN=func,l=l))})
Run the code above in your browser using DataLab