Search code examples
rpredictnls

Structure of the SSasym() function in stats


I am using SSasym() to generate fits for a several different years of data. It's all exponential decay population mortality data. If I use predict() on the nls function made with SSasym(), what is the structure of the equation used to generate those values?

some example data:

df = data.frame(value = c(8.96, 6.32, 6.10, 5.85, 5.75, 5.41, 5.91, 5.83, 5.21, 4.84, 5.80, 5.72, 5.58, 5.30, 4.53),
                x = c(5,6,6,7,7,8,9,10,11,12,13,14,15,16,17)

nlsfit = nls(value ~ SSasymp(x, Asym, R0, lrc), data = df)
plot(predict(nlsfit)~df$x, type ='l')

What is the function that is being used to make that predict() line? I've read through the documentation and didn't find anything, and I've tried replicating it with standard exponential decay equations and they don't match. Any help would be greatly appreciated.


Solution

  • This is explained in ?SSasymp. The expression is Asym + (R0-Asym)*exp(-exp(lrc)*x).

    df <- data.frame(
      value = c(8.96, 6.32, 6.10, 5.85, 5.75, 5.41, 5.91, 5.83, 5.21, 4.84, 5.80, 5.72, 5.58, 5.30, 4.53),
      x = c(5,6,6,7,7,8,9,10,11,12,13,14,15,16,17))
    
    nlsfit <- nls(value ~ SSasymp(x, Asym, R0, lrc), data = df)
    
    coefs <- coef(nlsfit)
    Asym <- coefs["Asym"]
    R0 <- coefs["R0"]
    lrc <- coefs["lrc"]
    
    Asym + (R0-Asym)*exp(-exp(lrc)*df$x)
    # [1] 8.935511 6.300831 6.300831 5.641607 5.641607 5.476663 5.435392 5.425066
    # [9] 5.422482 5.421836 5.421674 5.421634 5.421624 5.421621 5.421620
    predict(nlsfit)
    # [1] 8.935511 6.300831 6.300831 5.641607 5.641607 5.476663 5.435392 5.425066
    # [9] 5.422482 5.421836 5.421674 5.421634 5.421624 5.421621 5.421620