R 使用nls函数会产生奇异梯度

R 使用nls函数会产生奇异梯度,r,nls,R,Nls,我想使用nls函数将非线性最小二乘模型拟合到我的数据,但我得到一个错误: 我的数据是: y=c(0.3,1.5,4.1,10.1,21.9,39,4,58.2,77,89.6,95,98.3,100) x=c(30,35,40,45,50,55,60,65,70,75,80,85,90) 我使用了以下R命令: fit<-nls(y~a/(1+exp(-b*(x-c))), start=list(a=1,b=0.5,c=25)) fit以下是几种方法: 1)nls需要更好的起始值。首先

我想使用
nls
函数将非线性最小二乘模型拟合到我的数据,但我得到一个错误:

我的数据是:

y=c(0.3,1.5,4.1,10.1,21.9,39,4,58.2,77,89.6,95,98.3,100)

x=c(30,35,40,45,50,55,60,65,70,75,80,85,90)
我使用了以下R命令:

fit<-nls(y~a/(1+exp(-b*(x-c))), start=list(a=1,b=0.5,c=25))

fit以下是几种方法:

1)nls需要更好的起始值。首先取两边的倒数,使用不需要线性参数起始值的
“plinear”
算法,在本例中为
a
。然后,将其用作拟合的起始值

fit0 <- nls(1/y ~ 1 + exp(-b*(x-c)), start = list(b = .5, c = 25), alg = "plinear")
fit <- nls(y~1/(1+exp(-b*(x-c))),start=coef(fit0)[1:2], alg = "plinear")

plot(y ~ x)
lines(fitted(fit) ~ x)
fit

2)nls/SSlogisR提供了
SSlogis
自启动模型。不需要起始值。请注意,它是参数化的,因此b=1/b

nls(y ~ SSlogis(x, a, c, B))
给予:

Nonlinear regression model
  model: y ~ 1/(1 + exp(-b * (x - c)))
   data: parent.frame()
       b        c     .lin 
  0.1355  64.9761 106.7095 
 residual sum-of-squares: 1516

Number of iterations to convergence: 13 
Achieved convergence tolerance: 6.85e-06
Nonlinear regression model
  model: y ~ SSlogis(x, a, c, B)
   data: parent.frame()
     a      c      B 
106.71  64.98   7.38 
 residual sum-of-squares: 1516

Number of iterations to convergence: 2 
Achieved convergence tolerance: 4.087e-06
A 'drc' model.

Call:
drm(formula = y ~ x, fct = L.3())

Coefficients:
b:(Intercept)  d:(Intercept)  e:(Intercept)  
      -0.1355       106.7093        64.9761  
3)drcdrc软件包也可以满足这一要求,并提供自己的起始值。参数名称不同,下面的b、d和e对应问题中的-b、a和c

library(drc)

fm <- drm(y ~ x, fct = L.3())
plot(fm)
fm

以下是几种方法:

1)nls需要更好的起始值。首先取两边的倒数,使用不需要线性参数起始值的
“plinear”
算法,在本例中为
a
。然后,将其用作拟合的起始值

fit0 <- nls(1/y ~ 1 + exp(-b*(x-c)), start = list(b = .5, c = 25), alg = "plinear")
fit <- nls(y~1/(1+exp(-b*(x-c))),start=coef(fit0)[1:2], alg = "plinear")

plot(y ~ x)
lines(fitted(fit) ~ x)
fit

2)nls/SSlogisR提供了
SSlogis
自启动模型。不需要起始值。请注意,它是参数化的,因此b=1/b

nls(y ~ SSlogis(x, a, c, B))
给予:

Nonlinear regression model
  model: y ~ 1/(1 + exp(-b * (x - c)))
   data: parent.frame()
       b        c     .lin 
  0.1355  64.9761 106.7095 
 residual sum-of-squares: 1516

Number of iterations to convergence: 13 
Achieved convergence tolerance: 6.85e-06
Nonlinear regression model
  model: y ~ SSlogis(x, a, c, B)
   data: parent.frame()
     a      c      B 
106.71  64.98   7.38 
 residual sum-of-squares: 1516

Number of iterations to convergence: 2 
Achieved convergence tolerance: 4.087e-06
A 'drc' model.

Call:
drm(formula = y ~ x, fct = L.3())

Coefficients:
b:(Intercept)  d:(Intercept)  e:(Intercept)  
      -0.1355       106.7093        64.9761  
3)drcdrc软件包也可以满足这一要求,并提供自己的起始值。参数名称不同,下面的b、d和e对应问题中的-b、a和c

library(drc)

fm <- drm(y ~ x, fct = L.3())
plot(fm)
fm