Reputation: 11
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.
Upvotes: 1
Views: 992
Reputation: 57697
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.
Upvotes: 3