I am a beginner user of R
. I am trying to maximize log likelihood function with the bounded parameters. The function is a kind of gamma mixture model which try to capture unobserved heterogeneity across individual. I chose the optim's L-BFGS-B
given that the function contains two parameters (i.e., alpha
and r
) which should have positive values. However, the optimization does not take place and returns the initial parameters with the message below.
#Sample data
data<-matrix(c(1,36,547,2,54,464,3,92,415,4,114,1106,5,10,1038),ncol=3,byrow=TRUE)
colnames(data)<-c("ID","freq","time")
\begin{align} &\min_{r, \alpha}\; r \log \alpha + \sum_{j=0}^{n_i-1}\log(r+j) - (n_i + r)\log(t_{is} + t_{ic} + \alpha)\\ &\text{such that}\\ &\qquad r > 0\quad \text{(shape parameter)}\\ &\qquad \alpha > 0\quad \text{(scale parameter)} \end{align}
In the code below I use following terms for function.
ni = data[i,2] ## ni is the frequency of individual i
tis+tic = data[i,3] ## tis+tic is the total time of individual i
#Likelihood function I want to optimize
ll <- function(theta){
alpha<-theta[1] #scale parameter >0
r<-theta[2] #shape parameter >0
ll_i=c()
s=c()
for(i in 1:5){
for(j in 0:data[i,2]-1){
s[j+1]<-log(r+j+1)
}
ll_i[i]<-r*log(alpha)+sum(s)-(data[i,2]+r)*log(alpha+data[i,3])
s=c()
}
print(ll_i)
ll<-sum(ll_i)
return(-ll)
}
# Initial parameters
init<-c(alpha=1,r=5)
# L-BFGS-B optimization
ans<-optim(par=init,fn=ll,method="L-BFGS-B",lower=c(1e-10,1e-10),upper=c(Inf,Inf))
$message
[1] "CONVERGENCE: NORM OF PROJECTED GRADIENT <= PGTOL"
Any hint towards a successful optimization?