问题
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)
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
回答1:
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.
来源:https://stackoverflow.com/questions/38207716/error-fitting-a-model-in-nls