Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving error message "step halving factor reduced below minimum in NLS step": adjusting nlsTols not working

I am trying to fit a logistic growth curve to some data using the gnls function.

Data:

structure(list(Nest = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 16L, 10L, 4L, 5L, 7L, 12L, 4L, 6L, 20L, 8L, 14L, 16L, 3L, 9L, 15L, 19L, 6L, 7L, 17L, 18L, 12L, 13L, 10L, 20L, 5L, 8L, 11L, 16L, 6L, 12L, 1L, 2L, 4L, 6L, 9L, 18L, 21L, 16L, 3L, 20L),
.Label = c("WTSN01", "WTSN02", "WTSN04", "WTSN05", "WTSN06", "WTSN07", "WTSN08", "WTSN09", "WTSN10", "WTSN12", "WTSN13", "WTSN14", "WTSN16", "WTSN18", "WTSN20", "WTSN21", "WTSN23", "WTSN24", "WTSN25", "WTSN26", "WTSN28", "WTSN29"), class = "factor"),
Hatch = structure(c(16177, 16177, 16177, 16165, 16185, 16189, 16188, 16193, 16181, 16181, 16177, 16181, 16180, 16195, 16200, 16177, 16182, 16176, 16173, 16189, 16181, 16178, 16177, 16181, 16165, 16185, 16188, 16181, 16165, 16189, 16189, 16193, 16195, 16177, 16177, 16181, 16200, 16173, 16189, 16188, 16182, 16176, 16181, 16180, 16181, 16189, 16185, 16193, 16177, 16177, 16189, 16181, 16177, 16177, 16165, 16189, 16181, 16176, 16181, 16177, 16177, 16189),
class = "Date"), 
Age = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 6, 7.5, 8, 8, 8, 8, 8.5, 8.5, 8.5, 9, 9, 9, 
9.5, 9.5, 9.5, 9.5, 10, 10, 10, 10, 10.5, 10.5, 11, 11, 11.5, 
11.5, 11.5, 11.5, 12, 12, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 
12.5, 13, 13.5, 13.5), Weight = c(1.022, 1.022, 1.022, 1.022, 
1.022, 1.022, 1.022, 1.022, 1.022, 1.022, 1.022, 1.022, 1.022, 
1.022, 1.022, 1.022, 1.022, 1.022, 1.022, 1.022, 1.022, 1.022, 
8.1, 8.5, 8.8, 8.8, 9.6, 8.6, 9.7, 11, 9.9, 11.1, 9.9, 12, 
10.5, 10.5, 7, 11.2, 11.9, 11.4, 11, 11.9, 11.2, 11.7, 9.1, 
12.3, 12.3, 13, 11.6, 13.4, 12.2, 11.1, 12.7, 11.3, 12.2, 
12.4, 11.8, 12.9, 11.2, 13.2, 11, 14.1)),
.Names = c("Nest", "Hatch", "Age", "Weight"),
row.names = c(NA, 62L), class = "data.frame")

Code:

StartLogistic = c(Asym = 14.2, b = 0.07, K = 0.5)
Logistic_gnls = gnls(Weight ~ Asym/(1 + exp(b + K*Age)), data = WTS_gw,
                    start = StartLogistic)

This is giving the error message:

Error in gnls(Weight ~ Asym/(1 + exp(b + K * Age)), data = WTS_w, start = StartLogistic):
step halving factor reduced below minimum in NLS step

I have read in a few places that increase nlsTols to 0.1 should fix the problem, but I have tried increasing it in increments of an order of magnitude up to 100, and it gives the same error.

Logistic_gnls = gnls(Weight ~ Asym/(1 + exp(b + K*Age)), data = WTS_w,
                    start = StartLogistic, control=list(nlsTols=100))

I have also tried increasing tolerance, but to no avail.

Logistic_gnls = gnls(Weight ~ Asym/(1 + exp(b + K*Age)), data = WTS_w,
                    start = StartLogistic, control=list(tolerance=100))

Can anyone see a solution to this?

like image 415
Andrew Avatar asked Dec 18 '14 13:12

Andrew


1 Answers

You have very bad data coverage, i.e. no data in the upwards curving part of the logistic function and one influential data point. In the following I use a different parametrization of the logistic function. First let's do an nls fit with the selfstarting function:

plot(Weight ~ Age, data=DF)

fit <- nls(Weight ~ SSlogis(Age, Asym, xmid, scal), data=DF)
summary(fit)

curve(predict(fit, newdata = data.frame(Age=x)), add=TRUE)

resulting plot

Now you can use the coefficients and pass them to gnls:

library(nlme)
Logistic_gnls <- gnls(Weight ~ Asym/(1+exp((xmid-Age)/scal)), data = DF,
                     start = coef(fit))
coef(Logistic_gnls)
#     Asym      xmid      scal 
#12.908170  5.702021  2.365212

Thus, you can get a successful fit with better starting values.

like image 82
Roland Avatar answered Nov 10 '22 13:11

Roland