0

previous answers to similar questions have not help me to solve my problem.

I am trying to fit a model y=a1*(1-exp(-a21*Age_WH40))^a3, where a21=ln(1/a3)/a2, and Age_WH40 goes from 1 to 40. I've plot the data and a line to get an idea of the starting values

plot(MOE_WH40 ~ Age_WH40)
lines(ts(8*(1-exp(log(1/3)/5*(1:40)))^3),col="red", lwd=2)

enter image description here

fit.nlm_MOE4A.WH <- nls(MOE_WH40 ~ a*(1-exp(log(1/c)/b*Age_WH40))^b, start=list(a=10, b=6, c=2))

but even if I restrict the data to avoid dispersion I only get

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

I do not think it is a problem with the starting values, and I have run the model from 1 to 40 in Excel with no problem. Any idea what is happening? Here there is a subset of the data:

structure(list(ID = c(245L, 246L, 247L, 248L, 249L, 250L, 251L, 
252L, 253L, 254L, 255L, 256L, 257L, 258L, 259L, 260L, 261L, 262L, 
263L, 264L, 265L, 266L, 267L, 268L, 269L, 270L, 508L, 509L, 510L, 
511L), MOE_WH40 = c(7.9, 7.12, 4.369, 5.44, 8.97, 9.58, 8.07, 
7.9, 6.93, 5.63, 6, 6.17, 8.51, 8.79, 7.21, 6.64, 6.7, 7.88, 
7.97, 6.93, 5.64, 6.86, 9.36, 9.44, 10.04, 9.58, 4.337, 5.12, 
6.7, 7.86), Age_WH40 = c(23L, 29L, 4L, 8L, 13L, 20L, 24L, 29L, 
33L, 2L, 7L, 9L, 15L, 20L, 23L, 27L, 12L, 13L, 20L, 23L, 3L, 
9L, 16L, 22L, 26L, 30L, 2L, 8L, 11L, 15L)), .Names = c("ID", 
"MOE_WH40", "Age_WH40"), class = "data.frame", row.names = c(NA, 
-30L))

Thanks

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
David
  • 15
  • 11
  • 1
    Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to make it easier for us to help you. – Kara Woo Jul 05 '16 at 16:03
  • Thank you @Zheyuan Li, I was struggling to put the data in a nice way – David Jul 05 '16 at 16:50

1 Answers1

0

You could try using the minpack.lm package which uses the Levenberg-Marquardt algorithm. I've called your data "demo" for brevity.

nlsLM(data = demo, formula = MOE_WH40 ~ a*(1-exp(log(1/c)/b*Age_WH40))^b, start=list(a=10, b=6, c=2))
Nonlinear regression model
model: MOE_WH40 ~ a * (1 - exp(log(1/c)/b * Age_WH40))^b
data: demo
 a      b      c 
8.5573 0.3774 1.0347 
residual sum-of-squares: 32.89

Number of iterations to convergence: 24 
Achieved convergence tolerance: 1.49e-08
Warning messages:
1: In log(1/c) : NaNs produced
2: In log(1/c) : NaNs produced
3: In log(1/c) : NaNs produced

In nls it's always the starting values.

biomiha
  • 1,358
  • 2
  • 12
  • 25
  • Thank you @biomiha. It solved this problem. However, if I try to fit the model using the coefficients calculated to avoid the log expression like "model <- nls(MOE_WH40 ~ a*(1-exp(-b*Age_WH40))^c, start=list(a=8, b=0.03, c=0.3))" I still get the same error...any thoughts? – David Jul 17 '16 at 13:23
  • In nls this is usually due to one or several of the coefficients not converging. Looking at your data, there is considerable variability in your data points, i.e. you could fit several different curves to the data. Can you try a simpler model to fit? – biomiha Jul 17 '16 at 19:36
  • Yes @biomiha. I have fit different curves. The one shown was only one more. It makes sense to keep it simple, thanks. – David Jul 17 '16 at 22:10