Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
手动定义nls()的maxiter时无法使用自启动模型?_R_Self_Nls - Fatal编程技术网

手动定义nls()的maxiter时无法使用自启动模型?

手动定义nls()的maxiter时无法使用自启动模型?,r,self,nls,R,Self,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,

数据:

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)
问题:

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

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

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

你必须破解初始化程序。创建名为
SSasymp2
SSasymp2
的新副本,获取其初始化例程并将其称为
SSasymp2Init
,例如。然后使用
trace
在初始化中插入一个新版本的
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)

当我使用您描述的getInitial()函数时,我还得到了到达的最大迭代次数错误消息。我也尝试在那里包含maxiter参数,但没有成功:
getInitial(meandec~SSasymp(Time.Since.Burn,Asym,R0,lrc),data=averaged_perherb,nls.control(maxiter=500))
有没有办法在初始化例程中设置nls的maxiter?请参阅下面的答案。
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