Search code examples
rcurvenls

Setting upper bound on nls fits for logistic growth curve


I am fitting logistic growth curves, but nls overestimates my parameters. I have data of snails that grew to some asymptotic size (K) and they have a logistic shape to their growth pattern. Some asymptotic sizes are way over estimated so I want to set an upper bound to the model so that it won't let that parameter exceed what the maximum length of what a snail grew to.

Logistic equation: y~K*y0*exp(mumax * x)/(K+y0*(exp(mumax * x)-1)

Data:

x<-c(4,6,8,10,12,14,16,18,20,22)
y<-c(0.7, 0.9, 1.3, 1.9, 2.3, 2.8, 3.35, 3.4, 3.4)

Model that works but estimates K at 3.9:

y0_start<-0.4
mumax_start<-0.2
K_start<-3.3

m<-nls(y~K*y0*exp(mumax*x)/(K+y0*(exp(mumax*x)-1)),
   start=list(y0=y0_start,mumax=mumax_start, K=K_start))

Model that doesn't work with constraints:

m<-nls(y~K*y0*exp(mumax*x)/(K+y0*(exp(mumax*x)-1)),
        start=list(y0=y0_start,mumax=mumax_start, K=K_start, 
        algorithim="port", lower=list(y0=0, mumax=0, K=0), 
        upper=list(y0=0.4,mumax=0.4, K=3.45)))

Error:

Error in qr.default(.swts * gr) : 
  NA/NaN/Inf in foreign function call (arg 1)
In addition: Warning message:
In storage.mode(temp) <- "double" : NAs introduced by coercion

Solution

  • There are several problems:

    • the lengths of x and y are different
    • you misspelled "algorithm"
    • your parentheses are misplaced.

    Here is the correct code, but you need to fix the lengths of x and y:

    m <- nls(y~K*y0*exp(mumax*x)/(K+y0*(exp(mumax*x)-1)),
            start=list(y0=y0_start, mumax=mumax_start, K=K_start), 
            algorithm="port", lower=list(y0=0, mumax=0, K=0), 
            upper=list(y0=0.5, mumax=0.4, K=3.45))