The likelihood function is a product. Thus, the log likelihood function is a sum. Your log.lik statement, however, fails to compute the sum, which it should minimize. Hence your optim statement does not know what to optimize because log.lik is a vector of the length of the number of observations in your data (100) when it should be of length (1), i.e., the sum of the former. If you define log.lik as the appropriate sum, you should be successful.
HTH. Daniel djbanana wrote: > > I am trying to run this code and obtain the MLEs for my parameters. > However I am getting this error at the end. > > Error in optim(c(1.4, 1.1, 0.8, 0.92, 0.4), poisson.lik, v = v) : > objective function in *optim evaluates to length 100 not 1* > > Code: > poisson.lik <- function(theta,v){ > v=matrix(c(1,3,2,0),nrow=2) > n<-nrow(v) > alpha1 <- theta[1] > alpha2 <- theta[2] > beta1 <- theta[3] > beta2 <- theta[4] > gamma <- theta[5] > log.lik <- > v[1]*log(alpha1*beta2*gamma)-alpha1*beta2*gamma-log(factorial(v[1]))+y*log(alpha2*beta1)-alpha2*beta1-log(factorial(v[2])) > return(-log.lik) > } > optim(c(1.4,1.1,0.8,0.92,0.4),poisson.lik,v=v) > > Can anyone let me know what I am doing wrong please? > > Thanks, > DJ > -- View this message in context: http://r.789695.n4.nabble.com/Error-in-optim-tp3948980p3949006.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.