1

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: "Error in fn(par, ...) : attempt to apply non-function" the R code is:

logB<-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<-hpa
n<-length(data[,1])
t<-data[,2]
ep<-(t<=theta[3])
delta<-data[,4]
D<-sum(delta*ep)
d2<-sum(ep)
d3<-sum(delta)
w1<-sum(delta*ep*t+(1-delta)*ep*t)
w2<-sum(delta*(1-ep)*t)+ sum((1-delta)*(1-ep)*t)
d<-(d3-d1)
b<-((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")

Error in fn(par, ...) : attempt to apply non-function

Thank you in advance for your help.

noah
  • 11
  • 1
  • 6
  • 1
    Your code doesn't run. What is `logL` in the `optim` call? Why does `logB` have 2 parameters when you're passing a length-3 vector to `optim`? What is in `data`? – Hong Ooi Jul 07 '13 at 17:17
  • 1
    On another note; you're multiplying two gamma functions together, which is likely to result in numerical overflow even for relatively small inputs. Consider working on the log scale. – Hong Ooi Jul 07 '13 at 17:27
  • Thank you, it was a typo I changed it to logB, and I have changed it to lgamma function the same error. – noah Jul 07 '13 at 18:11

1 Answers1

3

In

d*log(w2-theta[3](n-d2))- D*log(w1+theta[3](n-d2))

you probably want

d*log(w2-theta[3]*(n-d2))- D*log(w1+theta[3]*(n-d2))

ie, you're missing the multiply operators.

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187