R nls奇异梯度找到一个sigmoid函数

R nls singular gradient to find a sigmoid function

我正在尝试找到适合我的数据的最佳 sigmoid 函数(在此 post 的末尾)。但是出现错误:Error in nls(cumulativo ~ f(eixox, phi1, phi2, phi3), start = st, data = data, : singular gradient

有什么建议吗?

library("ggplot2")

data<-structure(list(cumulativo = c(2, 3, 17, 191, 819, 1699, 2679, 
                          3907, 5535, 7254, 9226, 11543, 13809, 15542, 16852, 17709, 18246, 
                          18661, 18976, 19256, 19412, 19539, 19639), eixox = 1994:2016), 
      class = "data.frame", row.names = c(NA, -23L))
plot(cumulativo~eixox, data=data)

st <- list(phi1=20000,phi2=-5,phi3=.0005)
f <- function(x,phi1,phi2,phi3) {phi1/(1 + exp(-phi3 * x - phi2))}
curvaS<-nls(cumulativo~f(eixox,phi1,phi2,phi3),start=st,data=data,trace=TRUE)

您的函数定义有误。应该是

f <- function(x, phi1, phi2, phi3) {phi1/(1 + exp(-phi3 * (x - phi2)))}

其中phi1是上限,phi2是sigmoid曲线的中点,phi3是比率。请注意额外的括号,以便从 x 中减去 phi2,然后乘以 -phi3。现在选择合理的起始值和 运行 nls:

st <- list(phi1=20000, phi2=2005, phi3=.5)
curvaS <- nls(cumulativo~f(eixox, phi1, phi2, phi3), start=st, data=data, trace=TRUE)
# 20466691 :  20000.0  2005.0     0.5
# 1334673 :  19669.7851882  2004.2327533     0.4406048
# 902806.6 :  19566.0810794  2004.1449741     0.4639131
# 901808.4 :  19578.7102128  2004.1498061     0.4637724
# 901808.4 :  19578.7652076  2004.1498401     0.4637683
curvaS
# Nonlinear regression model
#   model: cumulativo ~ f(eixox, phi1, phi2, phi3)
#    data: data
#       phi1       phi2       phi3 
# 19578.7652  2004.1498     0.4638 
#  residual sum-of-squares: 901808
# 
# Number of iterations to convergence: 4 
# Achieved convergence tolerance: 0.000003139

最后,剧情:

plot(cumulativo~eixox, data=data)
pred <- predict(curvaS)
lines(data$eixox, pred)