title: R for optimizing a function which involves the gamma function
body: <p>I am trying to optimize a function which involves a gamma function. my data is a censored data. The error, that I am having is:
   <strong>"Error in fn(par, ...) : attempt to apply non-function"</strong>
  the R code is:</p>
  
  <pre><code>logB&lt;-function(theta, data){
  #theta=(lambda, rho, tau)
  #hpa = the data with first column X, second column Age, third column t,fourth column group and fifth column delta
  data&lt;-hpa
  n&lt;-length(data[,1])
  t&lt;-data[,2]
  ep&lt;-(t&lt;=theta[3])
  delta&lt;-data[,4]
  D&lt;-sum(delta*ep)
  d2&lt;-sum(ep)
  d3&lt;-sum(delta)
  w1&lt;-sum(delta*ep*t+(1-delta)*ep*t)
  w2&lt;-sum(delta*(1-ep)*t)+ sum((1-delta)*(1-ep)*t)
  d&lt;-(d3-d1)
  b&lt;-((lgamma(d)+ lgamma(D))- d*log(w2-theta[3](n-d2))- D*log(w1+theta[3](n-d2)))
  return(b)
    }
  optim(c(0.4,0.9,96),logB, method="Nelder-Mead")
  </code></pre>
  
  <p>Error in fn(par, ...) : attempt to apply non-function</p>
  
  <p>Thank you in advance for your help.</p>