R Programming – Solving Convergence Failure in Exponential Decay Upwards Model

convergenceexponential-familynlsr

I have some data that when plotted looks similar to this:

enter image description here

Then eyeballing the charts on this page, it looks like I might have an exponential decay (Increasing form) relationship?

enter image description here

I searched for how to fit this type of model in R and found this SO post. The solution from that post:

set.seed(1)     # so the example is reproduceable
df <- data.frame(Abundance=sort(sample(1:70,30)))
df$Richness <- with(df, 20*(1-exp(-0.03*Abundance))+rnorm(30))  

fit <- nls(Richness ~ C*(1-exp(k*Abundance)),data=df, 
           algorithm="port",
           start=c(C=10,k=-1),lower=c(C=0,k=-Inf), upper=c(C=Inf,k=0))

I tried to fit this to my own data but get a convergence error:

fit <- nls(TOTAL_CUMULATIVE_AMOUNT ~ C*(1-exp(k*TENURE)), data=pdata2, 
+            algorithm="port",
+            start=c(C=10,k=-1),lower=c(C=0,k=-Inf), upper=c(C=Inf,k=0))
Error in nls(TOTAL_CUMULATIVE_AMOUNT ~ C * (1 - exp(k * TENURE)), data = pdata2,  : 
  Convergence failure: singular convergence (7)

I was unsure what start, lower and upper where in this context so looked at the documentation for ?nls:

start: a named list or named numeric vector of starting estimates.
When start is missing (and formula is not a self-starting model, see
selfStart), a very cheap guess for start is tried (if algorithm !=
"plinear").

lower/upper: vectors of lower and upper bounds, replicated to be as
long as start. If unspecified, all parameters are assumed to be
unconstrained. Bounds can only be used with the "port" algorithm. They
are ignored, with a warning, if given for other algorithms.

I still don't really understand what those mean.

I tried leaving them out and fitting again but this gave another error:

fit <- nls(TOTAL_CUMULATIVE_AMOUNT ~ C*(1-exp(k*TENURE)), data=pdata2, 
+            algorithm="port")
Error in C * (1 - exp(k * TENURE)) : 
  non-numeric argument to binary operator

Here's a summary of pdata2:

pdata2 %>% summary
     TENURE      TOTAL_CUMULATIVE_AMOUNT
 Min.   :  7.0   Min.   :1.207e+08      
 1st Qu.:187.8   1st Qu.:1.191e+09      
 Median :368.5   Median :1.703e+09      
 Mean   :368.5   Mean   :1.538e+09      
 3rd Qu.:549.2   3rd Qu.:1.969e+09      
 Max.   :730.0   Max.   :2.119e+09

pdata2 %>% glimpse
Rows: 724
Columns: 2
$ TENURE                  <dbl> 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,…
$ TOTAL_CUMULATIVE_AMOUNT <dbl> 12065.81, 13422.30, 14708.39, 15923.89, 17107.51, 18243.34, 19395.96, 20…

How can I overcome this error and fit a exponential decay (increasing form) model?

[Edit]
Scaled and sanitized data, here's the dput of pdata2:

structure(list(TENURE = c(7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 
97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 
110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 
123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 
136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 
149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 
162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 
175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 
188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 
201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 
214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 
227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 
253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 
266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 
279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 
292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 
305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 
318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 
331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 
344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 
357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 
370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 
383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 
396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 
409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 
422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 
435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 
448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 
461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 
474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 
487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499, 
500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 
513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 
526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 
539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 
552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 
565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575, 576, 577, 
578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 
591, 592, 593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 
604, 605, 606, 607, 608, 609, 610, 611, 612, 613, 614, 615, 616, 
617, 618, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 
630, 631, 632, 633, 634, 635, 636, 637, 638, 639, 640, 641, 642, 
643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 653, 654, 655, 
656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 
669, 670, 671, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 
682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 
695, 696, 697, 698, 699, 700, 701, 702, 703, 704, 705, 706, 707, 
708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719, 720, 
721, 722, 723, 724, 725, 726, 727, 728, 729, 730), TOTAL_CUMULATIVE_AMOUNT = c(120.658144, 
134.222981, 147.083863, 159.238861, 171.07506, 182.433427, 193.959608, 
205.398569, 217.065617, 227.88294, 238.208604, 248.7267, 259.033375, 
269.645001, 280.22768, 290.339236, 300.175645, 309.893532, 319.401227, 
328.538212, 337.812905, 346.897638, 356.256771, 365.203453, 374.028778, 
382.44069, 391.031376, 399.603472, 408.643964, 417.62814, 425.905169, 
433.880421, 442.137973, 450.552238, 458.817027, 467.328472, 475.763503, 
483.932499, 491.837662, 499.533161, 507.334929, 515.577564, 523.74277, 
531.629968, 539.297029, 546.683601, 554.070205, 561.262417, 568.487104, 
575.741234, 582.921559, 589.671381, 596.42627, 603.115026, 610.038471, 
616.910091, 623.626384, 630.386505, 636.95572, 643.033317, 649.249905, 
655.330175, 661.8546, 668.450269, 675.098372, 681.524995, 687.873301, 
694.147956, 700.505076, 706.964236, 713.422493, 719.760698, 725.931634, 
731.999539, 738.055802, 743.957791, 749.99346, 756.130357, 762.077698, 
767.799198, 773.650374, 779.182589, 784.743745, 790.331146, 795.880115, 
801.298307, 806.524801, 811.712454, 816.822691, 821.595714, 826.572265, 
831.77164, 837.024091, 842.260608, 847.209915, 852.37978, 857.474076, 
862.78088, 868.00515, 873.381476, 878.25394, 883.144414, 887.951485, 
892.789252, 897.888436, 902.783288, 907.86608, 912.641968, 917.480793, 
922.212901, 926.830266, 931.138449, 935.693909, 940.170322, 944.446869, 
948.740737, 952.613686, 956.618882, 961.00304, 965.167469, 969.386703, 
973.634411, 977.892102, 981.991028, 986.129414, 990.283106, 994.517307, 
998.58441, 1002.682591, 1006.611134, 1010.656914, 1014.5957, 
1018.852938, 1023.053059, 1027.293643, 1031.410214, 1035.34798, 
1039.265272, 1043.157102, 1047.040021, 1050.864722, 1054.764933, 
1058.244164, 1061.742756, 1065.266805, 1068.59785, 1072.123547, 
1075.62904, 1079.227407, 1082.703456, 1086.119529, 1089.39846, 
1092.684765, 1096.106552, 1099.430422, 1102.964752, 1106.267448, 
1109.678124, 1113.008005, 1116.387063, 1120.13737, 1123.723282, 
1127.398092, 1130.718812, 1134.390364, 1138.054106, 1141.497474, 
1144.981198, 1148.473253, 1152.063834, 1155.414638, 1158.86215, 
1162.231715, 1165.461909, 1168.959181, 1172.251376, 1175.614261, 
1178.964644, 1182.154069, 1185.338248, 1188.626183, 1192.02864, 
1195.565526, 1199.110816, 1202.663641, 1206.077989, 1209.294201, 
1212.90853, 1216.563103, 1220.366396, 1224.2231, 1227.84374, 
1231.72717, 1235.474692, 1239.367729, 1243.368245, 1247.34284, 
1251.397334, 1255.199083, 1259.067231, 1262.995486, 1266.839902, 
1270.844278, 1274.544348, 1278.462843, 1282.244194, 1286.059436, 
1289.712231, 1293.522813, 1297.489811, 1301.467906, 1305.34363, 
1309.235806, 1312.996128, 1316.735788, 1320.669961, 1324.753659, 
1328.804284, 1332.754207, 1336.738981, 1340.545334, 1344.40407, 
1348.305588, 1352.371126, 1356.334823, 1360.299955, 1363.904752, 
1367.702834, 1371.617151, 1375.284838, 1379.101011, 1382.813628, 
1386.333213, 1389.743653, 1393.19626, 1396.504151, 1399.842653, 
1403.069048, 1406.239951, 1409.303995, 1412.339564, 1415.322623, 
1418.305515, 1420.994144, 1423.982232, 1427.052502, 1430.145899, 
1433.122526, 1436.083515, 1439.057432, 1442.071709, 1445.13259, 
1448.185091, 1451.381345, 1454.330247, 1457.269911, 1460.174267, 
1462.995213, 1466.115549, 1468.980675, 1471.846668, 1474.654904, 
1477.216483, 1479.754973, 1482.376588, 1485.067753, 1487.733443, 
1490.324571, 1492.931949, 1495.236589, 1497.511185, 1499.797216, 
1502.305557, 1504.928327, 1507.512611, 1509.99524, 1512.443055, 
1515.004266, 1517.554906, 1520.30226, 1523.050502, 1525.814132, 
1528.317127, 1530.983185, 1533.701645, 1536.499075, 1539.360258, 
1542.147208, 1545.005254, 1547.59168, 1550.095662, 1552.846693, 
1555.434547, 1558.075603, 1560.508738, 1563.170669, 1565.440903, 
1567.777821, 1570.200538, 1572.473211, 1574.871431, 1577.285379, 
1579.642384, 1581.941331, 1584.237304, 1586.506749, 1588.946016, 
1591.354846, 1593.89034, 1596.317624, 1598.814065, 1601.163581, 
1603.433059, 1605.824195, 1608.368816, 1610.906523, 1613.400685, 
1615.747353, 1618.164303, 1620.453064, 1622.75839, 1625.06362, 
1627.207458, 1629.347692, 1631.464796, 1633.367574, 1635.226171, 
1637.21937, 1639.143279, 1641.134923, 1643.004188, 1644.84348, 
1646.795757, 1648.510309, 1650.331746, 1652.343251, 1654.205449, 
1656.179753, 1657.97928, 1659.890551, 1661.760924, 1663.721208, 
1665.878364, 1668.071522, 1670.222918, 1672.279365, 1674.357448, 
1676.444112, 1678.395956, 1680.514781, 1682.514992, 1684.567783, 
1686.454762, 1688.292311, 1690.080282, 1691.836858, 1693.690806, 
1695.45231, 1697.237985, 1698.782572, 1700.367967, 1701.930589, 
1703.575422, 1705.308103, 1707.014531, 1708.759111, 1710.459851, 
1712.229397, 1713.904607, 1715.53513, 1717.359619, 1719.273153, 
1721.269764, 1723.174235, 1724.952676, 1726.766171, 1728.716402, 
1730.723349, 1732.638268, 1734.523676, 1736.336088, 1738.127718, 
1739.952827, 1741.716276, 1743.534389, 1745.303761, 1746.958243, 
1748.72396, 1750.327146, 1751.895053, 1753.411818, 1755.047015, 
1756.763254, 1758.426989, 1760.005224, 1761.555474, 1763.107565, 
1764.712157, 1766.306445, 1767.923549, 1769.60143, 1771.171298, 
1772.741663, 1774.315452, 1775.905081, 1777.777227, 1779.543643, 
1781.27696, 1782.958628, 1784.576438, 1786.181779, 1787.792262, 
1789.445287, 1790.923773, 1792.39076, 1793.854135, 1795.361157, 
1796.863343, 1798.223991, 1799.810749, 1801.376326, 1802.839369, 
1804.232902, 1805.659449, 1807.079526, 1808.415968, 1809.87946, 
1811.438264, 1813.026061, 1814.516293, 1815.987857, 1817.466149, 
1819.123237, 1820.923127, 1822.742003, 1824.430273, 1826.19286, 
1827.85354, 1829.495486, 1831.083874, 1832.901235, 1834.553371, 
1836.19862, 1837.754796, 1839.291447, 1840.940053, 1842.588251, 
1844.304211, 1845.832641, 1847.455655, 1848.890278, 1850.363349, 
1851.768116, 1853.20816, 1854.780594, 1856.41732, 1857.992287, 
1859.496168, 1861.014546, 1862.588633, 1864.052801, 1865.682258, 
1867.332324, 1868.928318, 1870.496911, 1871.957182, 1873.477532, 
1875.018451, 1876.678698, 1878.315847, 1879.882682, 1881.388598, 
1882.846858, 1884.291378, 1885.743881, 1887.241131, 1888.564247, 
1890.070431, 1891.39115, 1892.665267, 1893.850056, 1895.131998, 
1896.392988, 1897.712892, 1899.043681, 1900.257178, 1901.467152, 
1902.666816, 1903.872949, 1905.094606, 1906.344748, 1907.670942, 
1908.963841, 1910.182558, 1911.442594, 1912.699588, 1914.055497, 
1915.378803, 1916.847055, 1918.121266, 1919.388446, 1920.658823, 
1921.954494, 1923.182034, 1924.52994, 1925.756313, 1926.9195, 
1928.183374, 1929.390734, 1930.468215, 1931.613638, 1932.811333, 
1933.956674, 1935.083563, 1936.094193, 1937.146568, 1938.097524, 
1939.133879, 1940.191244, 1941.266231, 1942.395255, 1943.536303, 
1944.625599, 1945.807483, 1946.976349, 1948.23384, 1949.467243, 
1950.789394, 1952.072652, 1953.193346, 1954.533898, 1955.835956, 
1957.095555, 1958.337401, 1959.588729, 1960.705211, 1961.880867, 
1962.940155, 1964.109688, 1965.267875, 1966.33494, 1967.432935, 
1968.377021, 1969.4206, 1970.413201, 1971.512269, 1972.649377, 
1973.726413, 1974.808854, 1975.895989, 1977.043123, 1978.157038, 
1979.210513, 1980.344019, 1981.363123, 1982.460554, 1983.588788, 
1984.703296, 1985.756032, 1986.979204, 1988.202853, 1989.392291, 
1990.53944, 1991.668943, 1992.743234, 1993.763261, 1994.849328, 
1995.941137, 1997.060964, 1998.055055, 1999.062206, 2000.058294, 
2001.085915, 2002.112686, 2003.148157, 2004.220921, 2005.213965, 
2006.199354, 2007.193265, 2008.097689, 2009.18287, 2010.263105, 
2011.289517, 2012.29533, 2013.374441, 2014.414378, 2015.532926, 
2016.612376, 2017.699906, 2018.78173, 2019.793475, 2020.86628, 
2021.82259, 2022.779732, 2023.720419, 2024.726128, 2025.769499, 
2026.588959, 2027.527135, 2028.360566, 2029.186272, 2030.108328, 
2031.009647, 2031.845284, 2032.702852, 2033.499841, 2034.366214, 
2035.129436, 2035.97315, 2036.954232, 2037.841308, 2038.599598, 
2039.433289, 2040.194893, 2040.982037, 2041.829719, 2042.736004, 
2043.708097, 2044.591034, 2045.417719, 2046.171035, 2047.020962, 
2047.861129, 2048.771176, 2049.610206, 2050.415006, 2051.206384, 
2051.91939, 2052.661776, 2053.416051, 2054.143987, 2054.944003, 
2055.665503, 2056.382868, 2057.101225, 2057.746514, 2058.390645, 
2059.14009, 2059.901556, 2060.655685, 2061.348192, 2062.016618, 
2062.703635, 2063.459766, 2064.170619, 2065.028039, 2065.739219, 
2066.46064, 2067.155939, 2067.95052, 2068.759063, 2069.580089, 
2070.320339, 2071.087929, 2071.879018, 2072.596424, 2073.275462, 
2074.004092, 2074.735455, 2075.417659, 2076.112459, 2076.726591, 
2077.491649, 2078.211092, 2078.877653, 2079.658134, 2080.334735, 
2080.900122, 2081.612223, 2082.188123, 2082.842574, 2083.512981, 
2084.16379, 2084.882462, 2085.589718, 2086.296668, 2086.968431, 
2087.662409, 2088.376486, 2089.091666, 2089.837666, 2090.573526, 
2091.300063, 2091.944606, 2092.684779, 2093.444628, 2094.250776, 
2094.937853, 2095.691648, 2096.373772, 2097.052356, 2097.819015, 
2098.454447, 2099.148136, 2099.804163, 2100.474076, 2101.083332, 
2101.710105, 2102.374124, 2103.007984, 2103.701434, 2104.386384, 
2105.103455, 2105.738529, 2106.329921, 2106.976084, 2107.649693, 
2108.374716, 2109.083112, 2109.740453, 2110.413842, 2111.043085, 
2111.811259, 2112.514298, 2113.186637, 2113.848428, 2114.541891, 
2115.278173, 2115.904512, 2116.545973, 2117.164639, 2117.787811, 
2118.424383, 2119.028871)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -724L))

Best Answer

Your issue are the bad starting values. You can easily fit this model using a self-starting model.

fit <- nls(TOTAL_CUMULATIVE_AMOUNT ~ SSasympOrig(TENURE, Asym, lrc), data=DF)

#C:
coef(fit)[["Asym"]]
#[1] 2159.835

#k (the log-transformation enforces the boundary condition):
-exp(coef(fit)[["lrc"]])
#[1] -0.004412887

plot(TOTAL_CUMULATIVE_AMOUNT ~ TENURE, data = DF, pch = ".")
curve(predict(fit, newdata = data.frame(TENURE = x)), col = "red", add = TRUE, type = "l")

plot of fit with exponential decay model

However, the functional relationship doesn't seem to be well described by this model. I don't know anything about your data but a quick test suggests that a Michaelis-Menten kinetic seems to fit better.

fit2 <- nls(TOTAL_CUMULATIVE_AMOUNT ~ SSmicmen(TENURE, Vm, k), data=DF)
curve(predict(fit2, newdata = data.frame(TENURE = x)), col = "red", add = TRUE, type = "l")
coef(fit2)
#     Vm        k 
#2837.357  245.202 

plot of fit with Michaelis-Menten model

Obviously, the choice of model has a strong impact on the fitted asymptote because you have no measured values close to the actual asymptote.