Search code examples
rnls

nls issue: Missing value or an infinity produced when evaluating the model


I am an R newbie trying to fit a curve to the following data

LOEC = c(1.15, 1.28, 1.29, 1.30, 1.72, 2.08, 2.18, 2.21, 2.24, 2.24, 2.26, 2.28, 2.37, 2.50, 2.52, 2.60, 2.63, 3.58, 3.91, 3.94, 4.17, 4.29, 4.46, 4.50, 5.29)
PAF = c(0.000385, 0.0769, 0.154, 0.231, 0.308, 0.346, 0.385, 0.410, 0.436, 0.462, 0.487, 0.513, 0.538, 0.554, 0.631, 0.646, 0.692, 0.769, 0.795, 0.846, 0.859, 0.872, 0.897, 0.923, 1)

The model in question is the following:

PAF(LOEC)= 1/(1+e−((log LOEC−a)/b))

plot:

ggplot(pol_loec, aes(x = LOEC, y = PAF)) +
  geom_point()

code:

> pol1 <- nls(PAF ~ 1/(1 + exp(-log(LOEC-a)/b)),
>            data = pol_loec,
>            start = c(a=0.39 , b=0.11),
>            trace=TRUE)

error:

Error in numericDeriv(form[[3L]], names(ind), env, central = nDcentral) : 
  Missing value or an infinity produced when evaluating the model
In addition: Warning message:
In log(LOEC - a) : NaNs produced

I tried changing the start values without success and used trace=TRUE to find the last tried parameter values. Any help?


Solution

  • The error you received is usually a good clue that your model is incorrect. In this case, I believe your parenthesis are incorrect. I think this is what you are looking for:

    pol1 <- nls(PAF ~ 1/(1 + exp(-(log(LOEC)-a)/b)),
                           data = pol_loec,
                            start = c(a=.39 , b=0.11),
                           trace=TRUE)
    summary(pol1)
    
    Formula: PAF ~ 1/(1 + exp(-(log(LOEC) - a)/b))
    
    Parameters:
      Estimate Std. Error t value Pr(>|t|)    
    a  0.83985    0.01612   52.10  < 2e-16 ***
    b  0.29878    0.02002   14.92 2.55e-13 ***
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    
    Residual standard error: 0.05105 on 23 degrees of freedom
    
    Number of iterations to convergence: 5 
    Achieved convergence tolerance: 4.931e-07