Forum: help
Monitor Forum | Start New ThreadRE: How to solve Error in maxNRCompute [ Reply ] By: Arne Henningsen on 2018-03-04 18:08 | [forum:45732] |
The log-likelihood function seems to be incorrectly specified or programmed. The error message indicates that the optimisation algorithm checks the parameter vector c(-12.077769, -1.010946, 1.451812). The optimization algorithm was so successful that the log-likelihood function becomes infinity: ll(c(-12.077769, -1.010946, 1.451812)) [1] Inf As the log-likelihood function is infinity, the algorithm cannot calculate its gradients / partial derivatives with respect to the parameters and, thus, terminates without success. Hence, you need to correct the specification or the coding of the log-likelihood function. |
How to solve Error in maxNRCompute [ Reply ] By: yap Yap on 2018-01-13 06:27 | [forum:45563] |
i got error (Error in maxNRCompute(fn = function (theta, fnOrig, gradOrig = NULL, hessOrig = NULL, : NA in gradient) when i want to find the maximum likelihood estimates with the following code. i realized i won't get the error with smaller loop but i will get the error when I use "i=2000". beta0<--8 beta1<-0.03 gamma<-0.0105 n<-100 beta0hat<-NULL beta1hat<-NULL gammahat<-NULL for (i in 1:2000) { u<-runif(n) u x<-rnorm(n) x c<-rexp(100,1/1515) c t1<-(1/gamma)*log(1-((gamma/(exp(beta0+beta1*x)))*(log(1-u)))) t1 t<-pmin(t1,c) t delta<-1*(t1>c) delta length(delta) cp<-length(delta[delta==1])/n cp delta[delta==1]<-ifelse(rbinom(length(delta[delta==1]),1,0.5),1,2) delta #0 uncensored #1 right censored #2 interval censored deltae<-ifelse(delta==0, 1,0) deltar<-ifelse(delta==1, 1,0) deltai<-ifelse(delta==2, 1,0) dat=data.frame(t,delta, deltae,deltar,deltai,x) dat$interval[delta==2] <- as.character(cut(dat$t[delta==2], breaks=seq(0, 500, 100))) labs <- cut(dat$t[delta==2], breaks=seq(0, 500, 100)) dat$lower[delta==2]<-as.numeric( sub("\\((.+),.*", "\\1", labs) ) dat$upper[delta==2]<-as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", labs) ) midcut<-function(x,from,to,by){ ## cut the data into bins... x=cut(x,seq(from,to,by),include.lowest=T) ## make a named vector of the midpoints, names=binnames vec=seq(from+by/2,to-by/2,by) names(vec)=levels(x) ## use the vector to map the names of the bins to the midpoint values unname(vec[x]) } dat$midpoint[delta==2]<-midcut(dat$t[delta==2],0,500,100) data0<-dat[which(dat$delta==0),]#uncensored data data1<-dat[which(dat$delta==1),]#right censored data data2<-dat[which(dat$delta==2),]#interval censored data library(maxLik) #without imputataion ll<-function(para) { b0<-para[1] b1<-para[2] g<-para[3] e<-sum((b0+b1*data0$x)+g*data0$t+(1/g)*exp(b0+b1*data0$x)*(1-exp(g*data0$t))) r<-sum((1/g)*exp(b0+b1*data1$x)*(1-exp(g*data1$t))) i<-sum((1/g)*exp(b0+b1*data2$x)*(exp(g*data2$upper)-exp(g*data2$lower))) l<-e+r+i return(l) } est<-maxLik(logLik=ll,start=c(para<-c(-8,0.03,0.0105))) } |