NLS功能-迭代次数超过最大

时间:2018-10-08 19:27:07

标签: r regression nls non-linear-regression

我有一个像这样的数据集:

dput(testing1)

structure(list(x = c(0, 426.263081392053, 852.526162784105, 
1278.78924417616, 
1705.05232556821, 2131.31540696026, 2557.57848835232, 2983.84156974437, 
3410.10465113642, 3836.36773252847, 4262.63081392053, 4688.89389531258, 
5115.15697670463, 5541.42005809668, 5967.68313948874, 6393.94622088079, 
6820.20930227284, 7246.4723836649, 7672.73546505695, 8098.998546449, 
8525.26162784105, 8951.52470923311, 9377.78779062516, 9804.05087201721, 
10230.3139534093, 10656.5770348013, 11082.8401161934, 11509.1031975854, 
11935.3662789775, 12361.6293603695, 12787.8924417616, 13214.1555231536, 
13640.4186045457, 14066.6816859377, 14492.9447673298, 14919.2078487218, 
15345.4709301139, 15771.734011506, 16197.997092898, 16624.2601742901, 
17050.5232556821, 17476.7863370742, 17903.0494184662, 18329.3124998583, 
18755.5755812503, 19181.8386626424, 19608.1017440344, 20034.3648254265, 
20460.6279068185, 20886.8909882106, 21313.1540696026, 21739.4171509947, 
22165.6802323867, 22591.9433137788, 23018.2063951708, 23444.4694765629, 
23870.732557955, 24296.995639347, 24723.2587207391, 25149.5218021311, 
25575.7848835232, 26002.0479649152, 26428.3110463073, 26854.5741276993, 
27280.8372090914, 27707.1002904834, 28133.3633718755, 28559.6264532675, 
28985.8895346596, 29412.1526160516, 29838.4156974437, 30264.6787788357, 
30690.9418602278, 31117.2049416198, 31543.4680230119, 31969.7311044039, 
32395.994185796, 32822.2572671881, 33248.5203485801, 33674.7834299722, 
34101.0465113642, 38363.6773252847, 42626.3081392053, 46888.9389531258, 
51151.5697670463, 55414.2005809668, 59676.8313948874, 63939.4622088079, 
68202.0930227284, 72464.7238366489, 76727.3546505695, 80989.98546449, 
85252.6162784105, 89515.247092331, 93777.8779062516, 98040.5087201721, 
102303.139534093, 106565.770348013, 110828.401161934, 115091.031975854, 
119353.662789775, 123616.293603695, 127878.924417616, 132141.555231536, 
136404.186045457, 140666.816859377, 144929.447673298, 149192.078487218, 
153454.709301139, 157717.340115059, 161979.97092898, 166242.601742901, 
170505.232556821, 174767.863370742, 179030.494184662, 183293.124998583, 
187555.755812503, 191818.386626424, 196081.017440344, 200343.648254265, 
204606.279068185, 208868.909882106, 213131.540696026, 217394.171509947, 
221656.802323867, 225919.433137788, 230182.063951708, 234444.694765629, 
238707.32557955, 242969.95639347, 247232.587207391, 251495.218021311, 
255757.848835232, 260020.479649152, 264283.110463073, 268545.741276993, 
272808.372090914, 277071.002904834, 281333.633718755, 285596.264532675, 
289858.895346596, 294121.526160516, 298384.156974437, 302646.787788357, 
306909.418602278, 311172.049416198, 315434.680230119, 319697.311044039, 
323959.94185796, 328222.572671881, 332485.203485801, 336747.834299722, 
341010.465113642, 345273.095927563, 349535.726741483, 353798.357555404, 
358060.988369324, 362323.619183245, 366586.249997165, 370848.880811086, 
375111.511625006, 379374.142438927, 383636.773252847, 387899.404066768, 
392162.034880688, 396424.665694609, 400687.296508529, 404949.92732245, 
409212.55813637, 413475.188950291, 417737.819764212, 422000.450578132, 
426263.081392053), y = c(0, 9.28064156596666, 18.545900177512, 
27.795801332368, 37.0303704859999, 46.2496330516791, 55.4536144005578, 
64.6423398617293, 73.8158347223069, 82.9741242274896, 92.1172335806295, 
101.245187943305, 110.35801243539, 119.455732135116, 128.538372079151, 
137.605957262664, 146.658512639393, 155.696063121713, 164.718633580707, 
173.726248846234, 182.718933706996, 191.696712910606, 200.659611163661, 
209.607653131799, 218.540863439782, 227.459266671548, 236.362887370294, 
245.25175003853, 254.125879138154, 262.98529909052, 271.830034276498, 
280.660109036552, 289.475547670796, 298.276374439066, 307.06261356099, 
315.834289216049, 324.591425543646, 333.334046643171, 342.062176574072, 
350.775839355914, 359.47505896845, 368.159859351686, 376.830264405948, 
385.486297991944, 394.127983930833, 402.755346004291, 411.368407954574, 
419.967193484584, 428.551726257936, 437.12202989902, 445.67812799307, 
454.220044086226, 462.747801685598, 471.261424259333, 479.760935236681, 
488.246358008055, 496.717715925098, 505.175032300746, 513.618330409295, 
522.047633486465, 530.462964729454, 538.86434729702, 547.251804309526, 
555.625358849021, 563.985033959285, 572.33085264591, 580.662837876353, 
588.981012579999, 597.285399648232, 605.576021934488, 613.852902254326, 
622.116063385486, 630.365528067953, 638.601319004021, 646.823458858352, 
655.031970258043, 663.226875792685, 671.408198014427, 679.575959438034, 
687.730182540955, 695.870889763381, 776.539498886984, 855.880929901957, 
933.917017841173, 1010.66923850263, 1086.15871435967, 1160.40622037394, 
1233.43218971275, 1305.25671937236, 1375.89957570869, 1445.38019987715, 
1513.71771318288, 1580.93092234301, 1647.03832466233, 1712.05811312379, 
1776.00818139531, 1838.90612875416, 1900.76926493033, 1961.61461487023, 
2021.45892342205, 2080.31865994395, 2138.21002283649, 2195.14894400053, 
2251.1510932217, 2306.23188248277, 2360.40647020513, 2413.68976542041, 
2466.09643187347, 2517.64089205797, 2568.33733118544, 2618.19970108913, 
2667.24172406357, 2715.47689664105, 2762.91849330583, 2809.5795701474, 
2855.47296845351, 2900.61131824417, 2945.00704174745, 2988.67235681812, 
3031.61928030007, 3073.85963133337, 3115.40503460692, 3156.26692355763, 
3196.45654351696, 3235.9849548056, 3274.8630357774, 3313.10148581304, 
3350.71082826463, 3387.70141335169, 3424.0834210096, 3459.86686369117, 
3495.06158912208, 3529.67728301099, 3563.72347171513, 3597.20952486194, 
3630.14465792765, 3662.53793477339, 3694.39827013962, 3725.73443209948, 
3756.55504447179, 3786.86858919437, 3816.68340865829, 3846.00770800373, 
3874.84955737805, 3903.21689415673, 3931.11752512776, 3958.5591286401, 
3985.5492567168, 4012.0953371333, 4038.20467546162, 4063.88445708088, 
4089.14174915471, 4113.98350257616, 4138.41655388066, 4162.44762712739, 
4186.0833357498, 4209.33018437567, 4232.19457061714, 4254.68278683143, 
4276.80102185247, 4298.55536269409, 4319.95179622522, 4340.99621081746, 
4361.6943979656, 4382.05205388147, 4402.0747810615, 4421.76808982864, 
4441.13739984872, 4460.18804162205, 4478.92525795032, 4497.35420537947, 
4515.4799556188, 4533.3074969367)), .Names = c("x", "y"), row.names = c(NA, 
-173L), class = c("tbl_df", "tbl", "data.frame"))

数据集的前六行:

# A tibble: 6 x 2
      x     y
  <dbl> <dbl>
1    0   0   
2  426.  9.28
3  853. 18.5 
4 1279. 27.8 
5 1705. 37.0 
6 2131. 46.2 

图(testing1 $ x,testing1 $ y)

enter image description here

我想拟合非线性最小二乘法。这就是我所拥有的:

a.start <- max(testing1$x)
b.start <- 1e-06
control1 <- nls.control(maxiter= 10000, minFactor= 1e-30, warnOnly= FALSE,tol=1e-05)

nl.reg <- nls(y ~ a * (1-exp(-b * x)),data=testing1,start= list(a=a.start,b=b.start),
          control= control1)

运行此命令时,出现错误提示:

Error in nls(y ~ a * (1 - exp(-b * x)), data = testing1, start = list(a = 
a.start,  : 
  number of iterations exceeded maximum of 10000

有人对此有经验吗,或者可以为我提供的数据提供一个可复制的例子以使其适合?任何帮助都会很棒,谢谢!

1 个答案:

答案 0 :(得分:2)

您的数据看起来很合适。我认为这可能“太好了”,因为一旦达到拟合度,该算法就可能无法计算梯度。 (请注意,关于此问题,在“值”部分之后立即有一个警告。)如果您大幅减少了迭代次数,并使用了找到的控制参数warnOnly,则不管起始值为a和b,结果相同。残差平方和实际上为零的事实意味着收敛就可以了,即使您将迭代次数限制为10,也会发生这种情况!

control1 <- nls.control(maxiter= 10,tol=1e-02, warnOnly=TRUE)
nl.reg <- nls(y ~ a * (1-exp(-b * x)),data=testing1,start= list(a=a.start,b=b.start),
           control= control1)
#------------
Warning message:
In nls(y ~ a * (1 - exp(-b * x)), data = testing1, start = list(a = a.start,  :
  number of iterations exceeded maximum of 10
> nl.reg
Nonlinear regression model
  model: y ~ a * (1 - exp(-b * x))
   data: testing1
        a         b 
5.599e+03 3.892e-06 
 residual sum-of-squares: 1.262e-21

Number of iterations till stop: 10 
Achieved convergence tolerance: 0.02381
Reason stopped: number of iterations exceeded maximum of 10