R- Optimx for exponential function with 2 parameters - cannot evaluate function at initial parameter values

狂风中的少年 提交于 2020-01-10 05:07:52

问题


I feel like I missed something very obvious but after an hour of fiddling/googling I cannot get this to work. Code:

#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, : Cannot evaluate function at initial parameters

I tried a variety of ways to input the parameters.


回答1:


Try

optimx(params, function(x) firstorder(x[1], x[2]))


来源:https://stackoverflow.com/questions/22176082/r-optimx-for-exponential-function-with-2-parameters-cannot-evaluate-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!