我觉得我错过了一些非常明显的东西,但经过一个小时的摆弄/谷歌搜索,我无法让它工作。法典:
#Generate data from exponential model
xdata<-seq_len(100)
ydata<-2*exp(-2*(xdata+rnorm(100)))
#Fit exponential model to data
firstorder<-function(C0,k){
ynew<-C0*exp(-k*xdata)
RMSE<-sum((ynew-ydata)^2,na.rm=TRUE)
return(RMSE)
}
#Initial parameter values
params<-c(1,1)
#Optimize
optimx(params,firstorder)
Error in optimx.check(par, optcfg$ufn, optcfg$ugr, optcfg$uhess, lower, : 无法在初始参数处计算函数
我尝试了多种方法来输入参数。
尝试
optimx(params, function(x) firstorder(x[1], x[2]))