在NLS中拟合模型时出错 [英] Error fitting a model in nls
问题描述
以前对类似问题的回答并不能帮助我解决问题。
previous answers to similar questions have not help me to solve my problem.
我正在尝试拟合模型 y = a1 *( 1-exp(-a21 * Age_WH40))^ a3
,其中 a21 = ln(1 / a3)/ a2
,Age_WH40从1开始到40。
我已经绘制了数据和一行以了解起始值
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
numericalDeriv(form [[3L]],names(ind),env)中的错误:
评估时缺少值或产生无穷大模型
Error in numericDeriv(form[[3L]], names(ind), env) : Missing value or an infinity produced when evaluating the model
我认为起始值不存在问题,并且我已经在Excel中将模型从1运到40没问题。知道发生了什么吗?这里是数据的子集:
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))
谢谢
推荐答案
您可以尝试使用使用Levenberg-Marquardt算法的minpack.lm软件包。为简便起见,我称您的数据为演示。
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
在nls中,它始终是起始值。
In nls it's always the starting values.
这篇关于在NLS中拟合模型时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!