我正在尝试优化涉及伽玛函数的函数。我的数据是一个审查数据。我遇到的错误是: “fn(par,...)出错:尝试应用非功能” R代码是:
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")
fn(par,...)出错:尝试应用非功能
提前感谢您的帮助。
答案 0 :(得分:3)
在
d*log(w2-theta[3](n-d2))- D*log(w1+theta[3](n-d2))
你可能想要
d*log(w2-theta[3]*(n-d2))- D*log(w1+theta[3]*(n-d2))
即,你错过了乘法运算符。