0

I am trying to fit this nonlinear model in R using the nls function.

Here is the data I am trying to fit:

tab2 = data.frame(n = c(10,100,1000,10000,100000), Time = c(3.989220e-03, 
1.994681e-02, 3.311172e-01, 5.142252e+00, 1.314725e+03))

We see that time is growing exponentially, so I want to model this using nls. This is what I have tried so far:

mod4 = nls(Time ~ exp(a + b*n), data = tab2, start = list(a = -3, b = 0))

However, this does not work, and the following error message comes up:

Error in numericDeriv(form[[3L]], names(ind), env) : Missing value or an 
infinity produced when evaluating the model

I have no idea why this is happening, but I am guessing it has to do with these starting values? I got them by estimating the model using lm:

mod3 = lm(log(Time) ~ n, data = tab2); coef(mod3)
 (Intercept)             n 
-2.5908574883  0.0001010623 

Since this is pretty much the same model, I figured the coefficients would match up, but when I use values close to them for starting values, the nls model does not work.

Any suggestions as to why this is happening?

akenny430
  • 305
  • 3
  • 16
  • Have you tried `getInitial`? Does it give the same starting values? – EcologyTom Nov 20 '18 at 17:41
  • No, I have not, how would that be used? Sorry, this is the first time I've used `nls`. – akenny430 Nov 20 '18 at 17:45
  • Something like; `getInitia(Time ~ exp(a + b*n), data = tab2, start = list(a = -3, b = 0))`. Have a look at the help file. Also, normally `n` would be your response, affected by `Time`. Is this what you are aiming for? In which case you need to swap those round in your model. – EcologyTom Nov 20 '18 at 17:57
  • Also have a look at this question, which reports the same error as you https://stackoverflow.com/questions/38207716/error-fitting-a-model-in-nls?rq=1 – EcologyTom Nov 20 '18 at 18:18

1 Answers1

0

Better starting values are needed. If we take the log of both sides then it becomes a linear model and any starting values should work so just use a = b = 1, say, with that. (We could have alternately used lm.) Then use the coefficients from that first model as the starting values for the original model.

fo1 <- log(Time) ~ a + b*n
fm1 = nls(fo1, data = tab2, start = list(a = 1, b = 1))

fo2 <- Time ~ exp(a + b * n)
fm2 <- nls(fo2, tab2, start = coef(fm1))

fm2

giving:

Nonlinear regression model
  model: Time ~ exp(a + b * n)
   data: tab2
        a         b 
3.567e-01 6.825e-05 
 residual sum-of-squares: 10.84

Number of iterations to convergence: 7 
Achieved convergence tolerance: 2.051e-06
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341