Search code examples
rstatisticsnls

Why is there a difference between logistic growth output of R and another one?


df <- data.frame(
  time = c(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17),
  var = c(12.69,16.35,20.29,25.08,30.81,38.75,45,49.16,55.15,62.852,68.63,76.64,82.47,85.68,89.14,91.86,95.28,98.17)
)

logisticmodel <- nls(var ~ SSlogis(time, phi1, phi2, phi3), data = df)
summary(logisticmodel)
coef(logisticmodel)
#predict(logisticmodel, data.frame(time = 18))

The output given by R is as follows:

      phi1       phi2       phi3 
105.737368   7.432555   3.852865

But the website gives us:

enter image description here

I know some languages have different outputs. It's normal but I am wondering what your thoughts are?

Thanks in advance.


Solution

  • The problem is that different models are being fit.

    nls with self start function SSlogis fits the model (see help('SSlogis'))

    Asym/(1+exp((xmid-input)/scal))
    

    or, using your notation,

    phi1/(1 + exp((phi2 - input)/phi3))
    

    Pen and paper shows that the following transformations give the results in the webpage.

    fit <- nls(var ~ SSlogis(time, phi1, phi2, phi3), data = df)
    
    kappa <- coef(fit)[1]
    alpha <- exp(coef(fit)[2]/coef(fit)[3])
    beta <- 1/coef(fit)[3]
    c(kappa = unname(kappa), alpha = unname(alpha), beta = unname(beta))
    #      kappa       alpha        beta 
    #105.7373679   6.8832991   0.2595471 
    

    So, in order to automate this, write a simple function.

    transf <- function(x){
      kappa <- coef(x)[1]
      alpha <- exp(coef(x)[2]/coef(x)[3])
      beta <- 1/coef(x)[3]
      c(kappa = unname(kappa), alpha = unname(alpha), beta = unname(beta))
    }
    
    transf(fit)