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-tp3948980p3948980.html
Sent from the R help mailing list archive at Nabble.com.
Error in optim
4 messages · djbanana, Daniel Malter, michael.weylandt at gmail.com (R. Michael Weylandt
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.
I understand that the likelihood function is a product and hence the log likelihood function is a sum. However I can't figure out what the problem is. Here's the likelihood function: [(alpha1*beta2*gamma)^v1 exp^(-alpha1*beta2*gamma)]/v1! * [(alpha2*beta1)^v2 exp^(-alpha2*beta1)]/v2! Isn't the log-likelihood function as I calculated before? Thanks, DJ -- View this message in context: http://r.789695.n4.nabble.com/Error-in-optim-tp3948980p3949218.html Sent from the R help mailing list archive at Nabble.com.
Why don't you try substituting your vector of values and see what comes out...once you figure out what happened, the sum() command will solve your problems. Michael
On Oct 28, 2011, at 5:10 PM, djbanana <karl79264219 at gmail.com> wrote:
I understand that the likelihood function is a product and hence the log likelihood function is a sum. However I can't figure out what the problem is. Here's the likelihood function: [(alpha1*beta2*gamma)^v1 exp^(-alpha1*beta2*gamma)]/v1! * [(alpha2*beta1)^v2 exp^(-alpha2*beta1)]/v2! Isn't the log-likelihood function as I calculated before? Thanks, DJ -- View this message in context: http://r.789695.n4.nabble.com/Error-in-optim-tp3948980p3949218.html Sent from the R help mailing list archive at Nabble.com.
______________________________________________ R-help at 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.