需要优化特定功能才能获得最佳拟合曲线

时间:2018-02-08 20:03:52

标签: r best-fit-curve

我正在寻找符合特定配方的最佳拟合曲线。参数A和B需要优化

# X and Y are my datasets Y depend of X
X=c(73,33,201,90,1513,1312,1044,929,836,657,104,22)
Y=c(2.89,6.11,3.57,4.03,0.16,0.00,1.41,0.00,2.13,6.16,2.85,5.08)        

# X and Y plot give us
plot(X,Y, main="Y function of X",pch=6,xlab="X", ylab="Y",col='black')        


#Type of curve that I need
curve(0.07*90*(1-(x/(x+exp(5-0.001*x)))), add = T,col="blue",lw=3,lty=2)


#I want therefore to optimize the parameters A and B in order to have the best fitted curve according to this formula that fit better with my data 
curve(0.07*90*(1-(x/(x+exp(B+A*x)))), add = T, col="blue",lw=3,lty=2)         

# Please help `enter code here`

1 个答案:

答案 0 :(得分:0)

您可以使用nls()使用非线性最小二乘来优化函数。例如

nls(Y~0.07*90*(1-(X/(X+exp(B+A*X)))), data.frame(X,Y), start=list(A=.001,B=5))   

返回

Nonlinear regression model
  model: Y ~ 0.07 * 90 * (1 - (X/(X + exp(B + A * X))))
   data: data.frame(X, Y)
        A         B 
0.0004154 5.1653460 
 residual sum-of-squares: 30.27

Number of iterations to convergence: 8 
Achieved convergence tolerance: 8.472e-06