使用nls和nlr的嵌套用户定义函数进行非线性回归

时间:2019-02-15 14:43:21

标签: r function regression nls

我一直在尝试使用nls包中的函数nlrgnlm进行非线性回归,但是我的目标函数具有许多可调整的参数,并且通过定义更容易定义首先是一堆不同的方程式因此,我在链接(source)和以下等式中获取了数据:

#Import data to data.frame called 'DATA'

#Equations:
ws = function (ap,bp,cp,dp,vel) {
         ap*vel+cp*vel^2+dp
}

Fins = function(bs) {
    (X1+X5+bs)/(522.64+bs)
}

alp = function(ap,bp,cp,dp,vel,bs) {
     Fins(bs)*(ws(ap,bp,cp,dp,vel)-Fins(bs))/ws(ap,bp,cp,dp,vel)^2
}

vel.max = function(ep,fp,bs) {
     ep-fp*Fins(bs)
}

del = function(vel,ep,fp,gp,bs) {
     vel*(1-(1-vel.max(ep,fp,bs))*exp(-gp*(vel.max(ep,fp,bs)-vel)))
}
Le = function(hp,L,Ld,bs) {
     L*(1+hp*Fins(bs)*(1-Fins(bs))*Ld/L)
}

E.s = function(ae,be,ce) {
     ae*X1*X2/(100*X1)+be*X1*X3/(100*X1)+ce*(X1*X4)/(100*X1)
}

dens.0 = function(ad,bd,cd){
     ad*X1*X2/(100*X1)+bd*X1*X3/(100*X1)+cd*(X1*X4)/(100*X1)
}

Fb = function(lp,Dmill,L) {
      lp/(pi*Dmill^2*L)
}

dens.b = 8.05

dens.c = function(bs,ad,bd,cd,ae,be,ce,lp,Dmill,L) {
     (Fins(bs)*dens.0(ad,bd,cd)*(1-E.s(ae,be,ce))+Fb(lp,Dmill,L)*
     (dens.b-dens.0(ad,bd,cd))*(1-E.s(ae,be,ce)))/Fins(bs)
}

Pmo.s = function(Dmill,vel,Ld,L,ip,jp,kp) {
     Dmill^ip*(vel*(jp*Ld+L))^kp
} 


##Objective function  
Y.s = function(K,Dmill,vel,Ld,L,ip,jp,kp,bs,ad,bd,cd,ae,be,ce,lp,ap,bp,
                cp,dp,ep,fp,gp,hp) {
     Pmo.s(Dmill,vel,Ld,L,ip,jp,kp)+K*Dmill^2.5*Le(hp,L,Ld,bs)*
     dens.c(bs,ad,bd,cd,ae,be,ce,lp,Dmill,L)*alp(ap,bp,cp,dp,vel,bs)*
     del(vel,ep,fp,gp,bs)
}

一旦定义了所有方程式(请注意,目标函数Y.s包含了所有其他函数,并且我检查了此最终方程式是否与某些测试参数一起正确运行),我尝试运行以下代码以适合所有条件可调参数(K,Dmill,vel,Ld,L,ip,jp,kp,bs,ad,bd,cd,ae,be,ce,lp,ap,bp,cp,dp,ep,fp,gp,hp)

 nlr.test1 <- nls(Y ~ Y.s(K,Dmill,vel,Ld,L,ip,jp,kp,bs,ad,bd,cd,ae,be,ce,lp,
                          ap,bp,cp,dp,ep,fp,gp,hp),
   start=list(Dmill = 3.526,ap =    2.000,bp =  2.900,cp =  -2.200,
            dp =    -0.500,ep = 0.900,fp =  -0.135,gp = -19.420,
            hp =    12.280,ip = 2.500,jp =  0.600,kp =  0.8,K = 2.000,
            vel =   0.700,bs =  0.187,L  =  15.000,Ld = 3.000,ae =  0.307,
            be =    0.174,ce =  0.279,ad =  3.200,bd =  2.700,
            cd =    2.749,lp=430),
   algorithm = "port", ,lower=list(ap = 0,bp =  0,cp =  -5,dp = -2,ep = 0,
           fp = -5,gp = -100,hp =   0,ip =  0,jp =  0,kp =  0,K =   0,
           Dmill = 1,vel =  0,bs =  0,L  =  8,Ld =  0,ae =  0,be =  0,
           ce = 0,ad =  0,bd =  0,cd =  0,lp=0), upper=list(ap =    5,
           bp = 5,cp =  -1,dp = 0,ep =  2,fp =  0,gp =  0,hp =  20,ip = 5,
           jp = 1,kp =  2,K =   5,Dmill = 5,vel =   2,bs =  5,L  =  20,
           Ld = 8,ae =  1,be =  1,ce =  1,ad =  5,bd =  4,cd =  4,lp=1000), 
           data=DATA)

运行此代码会给出以下错误消息

  

Error in nlsModel(formula, mf, start, wts, upper) : singular gradient matrix at initial parameter estimates

据我了解,此错误表示初始值不好,但我认为这是不正确的,因为初始值似乎使Y.s值接近于Y实值。所以我尝试了一个不同的功能:

 nlr(Y,mu=Y.s(K,Dmill,vel,Ld,L,ip,jp,kp,bs,ad,bd,cd,ae,be,ce,lp,
                          ap,bp,cp,dp,ep,fp,gp,hp),
   pmu=list(Dmill = 3.526,ap =  2.000,bp =  2.900,cp =  -2.200,
            dp =    -0.500,ep = 0.900,fp =  -0.135,gp = -19.420,
            hp =    12.280,ip = 2.500,jp =  0.600,kp =  0.8,K = 2.000,
            vel =   0.700,bs =  0.187,L  =  15.000,Ld = 3.000,ae =  0.307,
            be =    0.174,ce =  0.279,ad =  3.200,bd =  2.700,
            cd =    2.749,lp=430))

运行此代码将显示以下错误消息:

  

Error in Pmo.s(Dmill, vel, Ld, L, ip, jp, kp) : object 'Dmill' not found

现在我不知道发生了什么,因为如前所述,我通过给出所有参数的测试值来检查该函数是否正确定义。

我知道这个问题有点难理解,因为它有很多参数和方程式,但是如果有人至少可以通过解释nlr为什么给我带来错误的原因来帮助我,我将不胜感激。在主函数中找不到定义的对象。或者,是否有更好的函数来解决非线性回归问题。

非常感谢您!

索非亚

0 个答案:

没有答案