[R] Function accepted by optim but not mle2 (?)

Medimel medimelenglish at gmail.com
Thu Jan 12 14:58:35 CET 2012


Dear Sir/ Madam,

I'm having trouble de-bugging the following - which works perfectly
well with optim or optimx - but not with mle2.
I'd be really grateful if someone could show me what is wrong. Many
thanks in advance. JSC:

gompertz<- function (x,t=data)
{
        a3<-x[1]
        b3<-x[2]
        shift<-data[1]
        h.t<-a3*exp(b3*(t-shift))
        S.t<-exp(a3/b3*(1-exp(b3*(t-shift))))
	PF<-1-(exp(a3/b3*(1-exp(b3*(data[1]-shift)))))
	QF<-1-(exp(a3/b3*(1-exp(b3*(data["length"(data)]-shift)))))
	return(S.t*h.t/(QF-PF))
}

## GOMPERTZ - maximum likelihood
gompertz2<-function (x)
{
lnlk<-sum(log(gompertz(x,t=data)))
return(-lnlk)
}

## this works:
A<-optimx(c(0.1,0.1),gompertz2, method=c("Nelder-Mead"),control=list(fnscale=1))

## this doesn't:
A <- mle2(gompertz2(x),start=list(a3=0.03,b3=0.1),data=data)
## Error in gompertz2(x) : object 'x' not found



More information about the R-help mailing list