1
$\begingroup$

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?

$\endgroup$
4
  • 2
    $\begingroup$ Welcome to SciComp.SE. Can you add the equations for the objective function and constraints please? As it is right now, it looks like you want someone to debug your code. $\endgroup$
    – nicoguaro
    Feb 17, 2016 at 16:40
  • $\begingroup$ Thank you for comment. I added the equation for the log likelihood function. $\endgroup$
    – S Kim
    Feb 22, 2016 at 14:43
  • $\begingroup$ And what are the constrains? $\endgroup$
    – nicoguaro
    Feb 22, 2016 at 14:58
  • $\begingroup$ Many thanks for your comments. I added the constraints alpha and r. So far I checked the r code for log likelihood function works well. However, I don't know how to deal with optimization procedure. The problem is parameters are always converging at lower limit I defined... $\endgroup$
    – S Kim
    Feb 22, 2016 at 16:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.