手动为 nls() 定义 maxiter 时不能使用自启动模型?

Cannot use self-starting models when manually defining maxiter for nls()?

数据:

structure(list(ID = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 
37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 
50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 59L, 60L, 61L, 
62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L), Stage = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 3L, 3L, 5L, 5L, 5L, 1L, 1L, 6L, 6L, 
4L, 4L, 2L, 2L, 7L, 7L), .Label = c("milpa", "robir", "jurup che", 
"pak che kor", "mehen che", "nu kux che", "tam che"), class = "factor"), 
    Time.Since.Burn = c(4, 2, 0.21, 2, 0.42, 4, 0.33, 0.33, 3, 
    6, 2.5, 5, 4, 5, 1.5, 6, 4, 6, 3, 6.5, 6.5, 6, 4, 2.5, 12, 
    10, 8, 18, 5, 10, 8, 16, 28, 22, 22, 21, 20, 18, 30, 27, 
    30, 36, 36, 40, 32, 28, 50, 32, 60, 60, 60, 60, 60, 60, 60, 
    60, 6, 6, 24, 26, 22, 2, 1, 50, 45, 10, 10, 4, 4, 60, 60), 
    meandec = c(0.3625, 0.3025, 0.275, 0.1075, 0.26, 0.395, 0.265, 
    0.4075, 0.9, 0.9275, 0.7075, 0.9625, 0.7725, 0.9325, 0.9875, 
    0.81, 0.575, 0.3075, 0.4675, 0.6975, 0.33, 0.8725, 0.46, 
    0.19, 0.495, 0.3825, 0.58, 0.2275, 0.45, 0.3925, 0.605, 0.515, 
    0.425, 0.34, 0.2475, 0.1375, 0.4225, 0.505, 0.36, 0.4325, 
    0.26, 0.1575, 0.125, 0.3125, 0.1725, 0.3175, 0.43, 0.3475, 
    0.2025, 0.395, 0.12, 0.1625, 0.3175, 0.1975, 0.1525, 0.2775, 
    0.4975, 0.725, 0.04, 0.326666666666667, 0.1425, 0.445, 0.4725, 
    0.3775, 0.27, 0.2225, 0.23, 0.3275, 0.9725, 0.215, 0.2325
    )), row.names = c(NA, -71L), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), vars = c("ID", "Stage"), drop = TRUE)

问题:

我正在尝试 运行 这些数据的指数衰减模型。我已经用类似的数据完成了它,但是当我尝试在这个特定的数据集上做它时,它说已经超过了最大迭代次数而没有收敛。

nonlinmod6<-nls(meandec~SSasymp(Time.Since.Burn, Asym,R0,lrc),data=averaged_perherb)

Error in nls(y ~ cbind(1 - exp(-exp(lrc) * x), exp(-exp(lrc) * x)), data = xy,  :   number of iterations exceeded maximum of 50

因此,我尝试使用以下代码手动增加最大迭代次数:

nonlinmod6<-nls(meandec~SSasymp(Time.Since.Burn, Asym,R0,lrc),data=averaged_perherb,nls.control(maxiter=500))

但它随后给我一个错误提示:

Error in nls(meandec ~ SSasymp(Time.Since.Burn, Asym, R0, lrc), data = 
averaged_perherb,: parameters without starting value in 'data': Asym, R0, lrc

考虑到我正在使用自启动函数来识别启动参数,我认为情况不应该如此。有什么办法可以解决这个问题吗?

问题在于 SSaymp 初始化例程本身使用了 nls,而 nls 的隐藏调用就是问题所在。

您将不得不破解初始化例程。制作一个名为 SSasymp2SSasymp 的新副本,获取其初始化例程并将其命名为 SSasymp2Init,比如说。然后使用 trace 将具有所需 control 参数的 nls 的新版本插入到初始化中。为此,我们使用 pryr 包中的 partial 函数。将初始化例程替换为被黑客攻击的例程,然后 运行 nls.

library(pryr)

SSasymp2 <- SSasymp
SSasymp2Init <- attr(SSasymp2, "initial")
trace(SSasymp2Init, 
  quote(nls <- partial(stats::nls, control = nls.control(maxiter = 500))))
attr(SSasymp2, "initial") <- SSasymp2Init

nls(meandec ~ SSasymp2(Time.Since.Burn, Asym, R0, lrc), data = averaged_perherb)

给予:

Tracing (attr(object, "initial"))(mCall = mCall, data = data, LHS = LHS) on entry 
Nonlinear regression model
  model: meandec ~ SSasymp2(Time.Since.Burn, Asym, R0, lrc)
   data: averaged_perherb
   Asym      R0     lrc 
 0.1641  0.5695 -3.4237 
 residual sum-of-squares: 2.977

Number of iterations to convergence: 15 
Achieved convergence tolerance: 5.875e-06