Reputation: 389
I am trying to understand the relationship between NDVI and elevation using a GAM {mgcv}.
ndvi=c(0.37284458, 0.36299109, 0.34534124, 0.35626486, 0.33304086,
0.34456021, 0.34147954, 0.37136942, 0.34765118, 0.31762186, 0.31168666,
0.36252472, 0.37283841, 0.33449343, 0.34287873, 0.35803697, 0.29003596,
0.38974261, 0.37898183, 0.33467904, 0.37784857, 0.4082346, 0.34065819,
0.33835235, 0.34296334, 0.36620009, 0.38511008, 0.48989189, 0.38676593,
0.35365942, 0.44366336, 0.48514673, 0.49109551, 0.52452511, 0.39972124,
0.40625861, 0.43465778, 0.44273201, 0.40738371, 0.44018745, 0.49735194,
0.53355908, 0.44059527, 0.49126968, 0.4331325, 0.50643468, 0.49381891,
0.43036473, 0.41900063, 0.46537551, 0.38005379, 0.59700656, 0.50228047,
0.45391369, 0.49698669, 0.38300833, 0.40035707, 0.50576973, 0.34738368,
0.29102421, 0.38579389, 0.40881836, 0.32830772, 0.31917784, 0.38334072,
0.44629705, 0.55095547, 0.45162645, 0.44459134, 0.60119373, 0.5148682,
0.33612376, 0.36213681, 0.28747568, 0.29177585, 0.32551974, 0.35665748,
0.3745077, 0.40644151, 0.29654002, 0.35043341, 0.31480816, 0.3203741,
0.38690963, 0.37501845, 0.44434533, 0.42677853, 0.39572114, 0.33268374,
0.36218885, 0.45128649, 0.48611939, 0.36604273, 0.35607538, 0.40648854,
0.51214248, 0.42073068, 0.41392553, 0.43328416, 0.39342898, 0.43975314,
0.43961406, 0.56884927, 0.54535288, 0.66552502, 0.59507757, 0.35456958,
0.29807517, 0.28920445, 0.2994661, 0.29049689, 0.32402208, 0.35077614,
0.36170107, 0.35779324, 0.37913361, 0.35802785, 0.3657203, 0.37261558,
0.30806249, 0.29533851, 0.29045367, 0.30271557, 0.3270824, 0.36809677,
0.32528841, 0.40088555, 0.51367378, 0.53471488, 0.49363512, 0.42806908,
0.35330164, 0.31458119, 0.38571393, 0.33384511, 0.34088102, 0.38640618,
0.46462426, 0.46435356, 0.43860245, 0.45624161, 0.46190062, 0.45900786,
0.43069631, 0.42575583, 0.47714207, 0.39718044, 0.40318352, 0.2497917,
0.25269625, 0.47882196, 0.5320223, 0.5435406, 0.37848091, 0.20995587,
0.39205831, 0.51161075, 0.45779061, 0.26454785, 0.30714244, 0.33823466,
0.39386562, 0.39106974, 0.36486822, 0.23709562, 0.49485436, 0.3659955,
0.25378579, 0.42694312, 0.42897266, 0.46190798, 0.45739591, 0.28146976,
0.3088598, 0.30763069, 0.44386092, 0.261134, 0.23552018, 0.21810713,
0.19807275, 0.1983645, 0.20088004, 0.31146958, 0.2289221, 0.2640928,
0.18468697, 0.18130147, 0.18427111, 0.28448355, 0.21563233, 0.24098049,
0.22049443, 0.23217815, 0.24867195, 0.30727732, 0.37952852, 0.39026323,
0.3106921, 0.27072188, 0.40688464, 0.24761118, 0.23710926, 0.23490097,
0.20989761, 0.19913878, 0.22746462, 0.27149725, 0.23974811, 0.24351674,
0.21835253, 0.23701255, 0.2642346, 0.35599774, 0.38883707, 0.51227754,
0.52291566, 0.42694929, 0.31988204, 0.34567946, 0.28045416, 0.28074971,
0.3015548, 0.3018271, 0.33375728, 0.3026112, 0.36266547, 0.4212667,
0.37581727, 0.36138797, 0.42702183, 0.42004678, 0.48225939, 0.4645187,
0.48628905, 0.35416839, 0.42213014, 0.22399676, 0.20081049, 0.21593477,
0.18973792, 0.23069827, 0.23390402, 0.20583794, 0.42624232, 0.22057733,
0.21734384, 0.1978067, 0.38804373, 0.22859769, 0.21575791, 0.20370759,
0.35803479, 0.22058661, 0.20311546, 0.39473101, 0.25248429, 0.29062492,
0.18886448, 0.28385413, 0.43893406, 0.36858734, 0.1738556, 0.48782307,
0.38708588, 0.40135667, 0.27829349, 0.42564583, 0.39994976, 0.23983358,
0.29697156, 0.44519106, 0.32593229, 0.46419907, 0.31740955, 0.47189832,
0.33024263, 0.39319688, 0.37098923, 0.35016385, 0.36154863, 0.42523682,
0.38669956, 0.36397356, 0.36762711, 0.37823507, 0.31974643, 0.27281806,
0.28012297, 0.34467915, 0.42085385, 0.37235492, 0.35309508, 0.45293796,
0.39240688, 0.41030771, 0.3528643, 0.40271956, 0.4757368, 0.36223486,
0.41054854, 0.41387588, 0.38983357, 0.4167802, 0.35918111, 0.50965786,
0.39109153, 0.49194288, 0.55038857, 0.39720327, 0.43226415, 0.56539428,0.45440963, 0.34884879, 0.41057199, 0.38644075, 0.47156903, 0.33063081,
0.32137984, 0.35716292, 0.38749653, 0.42357811, 0.46679774, 0.44854486,
0.30482626, 0.27483031, 0.35759008, 0.42913625, 0.46651256, 0.28502983,
0.39216331, 0.40816534, 0.27440724, 0.3111816, 0.37306091, 0.38279143,
0.3264704, 0.44179216, 0.38299596, 0.29607165, 0.30998632, 0.46233353,
0.33230495, 0.32675204, 0.24098285, 0.20409316, 0.30174595, 0.21717471,
0.23586701, 0.21025825, 0.2350591, 0.51234818, 0.30030167, 0.17971739,
0.52621692, 0.52366912, 0.40064591, 0.57194382, 0.63093466, 0.48751017,
0.52511013, 0.50126761, 0.37541237, 0.42792675, 0.31729576, 0.37768126,
0.52844834, 0.31242937, 0.39442217, 0.40255255, 0.40193877, 0.36440474,
0.41571805, 0.315835, 0.31166464, 0.35319975, 0.50620955, 0.50272065,
0.45752475, 0.40692955, 0.37109366, 0.36725962, 0.36716217, 0.33104709,
0.41383833, 0.37535995, 0.41239858, 0.39627686, 0.42360836, 0.3887102,
0.44936863, 0.33351886, 0.48855564, 0.41484746, 0.47105169, 0.39093614,
0.40007663, 0.49453706, 0.48607925, 0.3932541, 0.44774082, 0.42149165,
0.3819266, 0.36476272, 0.36824036, 0.41211739, 0.36111835, 0.41108459,
0.36994269, 0.34518829, 0.34582183, 0.3388541, 0.36861211, 0.40138465,
0.38365835, 0.43873557, 0.43879241, 0.49522537, 0.52001029, 0.46294218,
0.43171886, 0.12007371, 0.4836823, 0.46957007, 0.10998849, 0.42262775,
0.44603577, 0.10697274, 0.47638127, 0.50140697, 0.48923677, 0.10984974,
0.49029085, 0.45691997, 0.42762986, 0.11285156, 0.39055535, 0.35207632,
0.40831631, 0.44256192, 0.50568485, 0.54724079, 0.11433452, 0.54162121,
0.11548679, 0.12456147, 0.55419147, 0.5490337, 0.1308123, 0.5958755,
0.37515038, 0.61899012, 0.54582655, 0.12366795, 0.53331429, 0.54886377,
0.12084137, 0.54416335, 0.33218127, 0.12337033, 0.57192695, 0.54615563,
0.12288751, 0.47263604, 0.3987166, 0.42566201, 0.41175276, 0.12040752,
0.59236515, 0.52589303, 0.55943716, 0.42766839, 0.5547722, 0.11845382,
0.56455755, 0.59910047, 0.5679692, 0.43171248, 0.12218402, 0.5436691,
0.56641918, 0.12743743, 0.55414909, 0.14656784, 0.52954382, 0.42851776,
0.14594139, 0.60023719, 0.12003227, 0.59396029, 0.62594134, 0.16057645,
0.3776767, 0.67848641, 0.21419135, 0.6850282, 0.58143109, 0.15435438,
0.54788327, 0.15782964, 0.58817184, 0.19433907, 0.2134172, 0.46749261,
0.54181325, 0.41827089, 0.42650056, 0.1875321, 0.46528313, 0.4021,
0.50298762, 0.48612255, 0.43469813, 0.16945361, 0.17312884, 0.5417611,
0.15071063, 0.57286531, 0.5541805, 0.32761294, 0.56888014, 0.11994911,
0.5775314, 0.55209744, 0.5880518, 0.5298745, 0.53949076, 0.43877628,
0.48537695, 0.38416794, 0.49095127, 0.56465995, 0.4470818, 0.56394529,
0.11755586, 0.57454139, 0.11578638, 0.5672397, 0.57609165, 0.5380218,
0.58232319, 0.59500247, 0.60516119, 0.45734581, 0.48618528, 0.51702631,
0.55027866, 0.37871492, 0.31339511, 0.34899774, 0.39584169, 0.34440562,
0.55272824, 0.38057595, 0.40194014, 0.37820032, 0.35338613, 0.4434987,
0.47183508, 0.50089973, 0.56338143, 0.35097095, 0.47634727, 0.29248065,
0.35980406, 0.30520365, 0.51648533, 0.5068593, 0.31666866, 0.32031479,
0.37659955, 0.26569951, 0.26894692, 0.43952745, 0.44224274, 0.46047512,
0.28328276, 0.40928012, 0.37350893, 0.39134952, 0.48740214, 0.36256111,
0.33733693, 0.50880104, 0.36416224, 0.46596181, 0.43892652, 0.37539759,
0.49673808, 0.44390601, 0.44054565, 0.47481841, 0.5103085, 0.51133341,
0.57967305, 0.57180786, 0.53124815, 0.41183549, 0.46823403, 0.33697557,
0.50546753, 0.55019134, 0.59541911, 0.52273095, 0.63234776, 0.54289067,
0.6559633, 0.44042417, 0.52003521, 0.69010967, 0.45370963, 0.40090004,
0.32907081, 0.44446883, 0.18390173, 0.17358142, 0.27948725, 0.32681823, 0.17034216, 0.17107487, 0.30296239, 0.1622318, 0.16486378, 0.28758663,
0.16008918, 0.47051576, 0.39771613, 0.20432511, 0.30118409, 0.18917,
0.16774413, 0.19803287, 0.31669104, 0.28809702, 0.27116156, 0.32019755,
0.47789127, 0.22356878, 0.33761221, 0.2071128, 0.45098323, 0.43004736,
0.46809581, 0.19979469, 0.20777245, 0.40051672, 0.24367467, 0.17879868,
0.47685167, 0.40548974, 0.20904543, 0.18480691, 0.22970232, 0.25320169,
0.34040126, 0.1866201, 0.3197476, 0.29494119, 0.23753925, 0.2466726,
0.33789754, 0.19939244, 0.20041303, 0.2025224, 0.19561875, 0.27618363,
0.30849349, 0.22153169, 0.55085963, 0.3131991, 0.3583152, 0.20948842,
0.20855463, 0.31315297, 0.28010854, 0.25264174, 0.39960471, 0.4657588,
0.28375575, 0.25989574, 0.31080499, 0.467841, 0.41235253, 0.45717904,
0.5812813, 0.56274688, 0.48186132, 0.39704716, 0.42959207, 0.46030018,
0.40959281, 0.57361746, 0.51162231, 0.48982546, 0.4931736, 0.48223504,
0.53471524, 0.52804619, 0.49133918, 0.5440793, 0.49828768, 0.52568519,
0.52009314, 0.55978638, 0.54395258, 0.41577986, 0.38484025, 0.4419764,
0.44596735, 0.44326246, 0.39296818, 0.42032951, 0.50332654, 0.44751549,
0.39300203, 0.44557381, 0.52650803, 0.49614227, 0.42115286, 0.49260482,
0.44385687, 0.5635072, 0.32846645, 0.50394791, 0.46008989, 0.58905828,
0.53291762, 0.32630342, 0.61202854, 0.41473407, 0.58198237, 0.35745674,
0.34939414, 0.38247347, 0.62988394, 0.57093704, 0.48079449, 0.36502972,
0.41322416, 0.45119342, 0.46854448, 0.46846315, 0.64025593, 0.42413765,
0.51297534, 0.59165025, 0.51079077, 0.49969929, 0.6184572, 0.55338889,
0.58190191, 0.55547076, 0.59358573, 0.63075489, 0.51425701, 0.63361824,
0.49662313, 0.47938466, 0.49662527, 0.51538551, 0.48033077, 0.65019745,
0.49855691, 0.50957841, 0.46327689, 0.52606738, 0.54897904, 0.54415298,
0.53295821, 0.48907343, 0.5813539, 0.5791558, 0.56734169, 0.50948626,
0.61064368, 0.63390613, 0.54600215, 0.54012775, 0.54547435, 0.53523856,
0.47844616, 0.54452389, 0.61522639, 0.66612244, 0.54486001, 0.55848712,
0.57533371, 0.59418201, 0.57968897, 0.55798745, 0.43673918, 0.59824342,
0.61019611, 0.56767529, 0.23656663, 0.49716783, 0.42394811, 0.49415183,
0.48858669, 0.56280184, 0.53211331, 0.570948, 0.44651186, 0.55828547,
0.44064161, 0.52318674, 0.50507104, 0.56085426, 0.62119055, 0.52611685,
0.43739846, 0.56699002, 0.48123884, 0.44280618, 0.49389672, 0.57842433,
0.60003191, 0.59438759, 0.60395777, 0.52329993, 0.58154529, 0.50001037,
0.53997809, 0.42599022, 0.40334904, 0.5300926, 0.47504365, 0.61598688,
0.5307219, 0.54195917, 0.36586505, 0.52503568, 0.58194685, 0.56088555,
0.43736571, 0.44608584, 0.49466795, 0.46879953, 0.38689315, 0.37443966,
0.48397902, 0.50784773, 0.38129795, 0.53752059, 0.3884064, 0.43738136,
0.56364578, 0.67537719, 0.59310961, 0.38332301, 0.4207001, 0.6113413,
0.37628123, 0.58488911, 0.30617586, 0.50850123, 0.58645731, 0.3069602,
0.32366791, 0.57885045, 0.57523584, 0.48794562, 0.45338526, 0.4764891,
0.51286817, 0.56180888, 0.43689296, 0.51353568, 0.60581577, 0.40476534,
0.55250001, 0.61904049, 0.54644275, 0.56612611, 0.56348616, 0.58739144,
0.55728227, 0.49260241, 0.48701286, 0.54145378, 0.53963166, 0.4933565,
0.45025027, 0.42232114, 0.47838867, 0.58416939, 0.56239808, 0.37954712,
0.56743282, 0.39150637, 0.53962982, 0.60306382, 0.46025828, 0.49077743,
0.60236579, 0.47457057, 0.47792545, 0.57731998, 0.447983, 0.44238743,
0.45048851, 0.4277384, 0.38526028, 0.45638478, 0.42012143, 0.46913737,
0.44282815, 0.52651328, 0.4475337, 0.47447568, 0.4499096, 0.57560682,
0.46730033, 0.49862021, 0.35509562, 0.53960955, 0.50024015, 0.59349477,
0.38186911, 0.44112691, 0.36592636, 0.64959121, 0.48866126, 0.41112146,
0.59468359, 0.40227708, 0.46466774, 0.52884471, 0.54625285, 0.3591285, 0.46930903, 0.59036058, 0.48719868, 0.58185583, 0.58928138, 0.4725737,
0.47224933, 0.48249629, 0.51947838, 0.50547099, 0.49053812, 0.58460832,
0.56633383, 0.54741734, 0.55866396, 0.56510276, 0.54703504, 0.50296366,
0.59481162, 0.39838254, 0.49417058, 0.55508375, 0.44624022, 0.40848476,
0.47220662, 0.41816965, 0.35065037, 0.5417226, 0.3843298, 0.57464051,
0.33038601, 0.35658485, 0.40679565, 0.36484152, 0.54721773, 0.39360908,
0.48341459, 0.5437758, 0.45842963, 0.53718525, 0.56601614, 0.37139726,
0.43007621, 0.58927023, 0.31203148, 0.49183467, 0.43646091, 0.29040816,
0.42643794, 0.44335389, 0.3266508, 0.47064027, 0.4330571, 0.32086417,
0.52630913, 0.3401593, 0.58157116, 0.55650526, 0.40066051, 0.53089213,
0.51907247, 0.52990496, 0.50308192, 0.41526651, 0.41932482, 0.49954349,
0.40952563, 0.54189724, 0.43167049, 0.38867795, 0.32555526)
Elevation=c(1871.92, 1875.38, 1878.28, 1878.54, 1878.33, 1879.2, 1880.51,
1883.78, 1884.6, 1884.85, 1885.46, 1888.72, 1890.94, 1897.19,
1901.95, 1902.47, 1902.81, 1903.49, 1906.62, 1908.73, 1909.4,
1910.65, 1913.44, 1915, 1915.81, 1918.06, 1920.01, 1921.53, 1925.48,
1926.66, 1927.64, 1931.02, 1932.8, 1935.27, 1938.33, 1941.19,
1945.71, 1948.68, 1951.52, 1951.83, 1955.76, 1961.02, 1963.92,
1963.25, 1969.53, 1972.56, 1977.92, 1978.93, 1981.54, 1985.64,
1987.6, 1987.62, 1988.78, 1991.92, 1997.03, 1998.06, 1998.98,
2001.26, 2006.97, 2009.56, 2009.81, 2011.55, 2017.92, 2021.75,
2023.42, 2024.91, 2028.15, 2032.83, 2032.83, 2033.5, 2035.75,
2037.44, 2045.51, 2047.38, 2049.85, 2052.33, 2059.36, 2069.27,
2071.41, 2071.83, 2074.15, 2081.55, 2083.52, 2086.3, 2090.5,
2095.57, 2096.69, 2100.65, 2108.06, 2110.48, 2113.45, 2121.78,
2124.82, 2133.54, 2137.54, 2146.43, 2150.53, 2156.63, 2160.05,
2168.57, 2174.68, 2183.42, 2188.54, 2194.25, 2204.1, 2214.74,
1629.73, 1634.59, 1635.31, 1637.06, 1638.47, 1639.57, 1641.11,
1643.07, 1644.68, 1646.96, 1648.51, 1650.56, 1652.34, 1654.67,
1658.78, 1660.71, 1662.58, 1663.67, 1664.53, 1665.73, 1667.21,
1668.69, 1669.68, 1670.13, 1671.99, 1673.73, 1674.78, 1676.91,
1678.96, 1685.13, 1686.78, 1688.51, 1690.57, 1692.54, 1694.26,
1696.56, 1702.03, 1703.58, 1705.62, 1707.59, 1709.36, 1711.76,
2121.37, 2132.33, 2139.32, 2039.39, 2147.19, 2037.25, 2116.11,
2036.77, 2047.41, 2032.13, 2100.15, 2024.77, 2048.57, 2095.7,
2156.29, 2023.25, 2056.88, 2090.25, 2021.07, 2060.16, 2012.3,
2064.89, 2011.73, 2085.58, 2072.01, 2076.73, 2082.98, 2010.81,
2007.68, 2001.07, 2000.23, 1999.18, 1996.75, 1993.55, 1988.19,
1987.55, 1986.94, 1982.81, 1977.24, 1976.09, 1964.42, 1972.47,
1975.41, 1974.17, 1960.14, 1954.49, 1951.47, 1951.95, 1953.25,
1951.26, 1945.84, 1714.37, 1942.2, 1939.7, 1939.43, 1937.78,
1930.15, 1927.88, 1927.52, 1927.48, 1927.41, 1927.3, 1925.13,
1923.31, 1922.32, 1716.56, 1922.9, 1923.41, 1921.9, 1916.1, 1914.31,
1914.32, 1914.47, 1914.54, 1914.67, 1914.95, 1916.91, 1914.58,
1914.79, 1914.31, 1915.26, 1915.35, 1913.95, 1913.92, 1913.46,
1913.56, 1718.82, 1912.73, 1926.81, 1927.11, 1927.06, 1928.72,
1926.95, 1926.98, 1927, 1907.55, 1926.84, 1930.71, 1926.89, 1904.94,
1922.97, 1931.07, 1916.23, 1904.11, 1931.5, 1917.11, 1907.87,
1932.36, 1932.87, 1914.5, 1932.82, 1912.08, 1903.99, 1914.09,
1902.17, 1915.08, 1916.14, 1932.75, 1922.91, 1914.29, 1915.2,
1934.44, 1904.43, 1936.41, 1903.17, 1938.07, 1902.5, 1938.02,
1902.48, 1723.01, 2225.15, 1940.32, 1938.35, 1901.94, 1901.92,
1903.22, 1901.91, 1901.93, 2219.35, 2218.75, 1901.91, 1944.29,
2216.48, 1901.94, 1946.04, 1901.96, 1947.24, 2207.48, 1901.91,
2204.64, 1948.74, 1901.99, 2193.47, 1901.04, 1950.25, 1725.13,
2192.55, 1899.9, 1950.86, 2192.36, 1951.09, 1893.62, 2025.57,
2188.52, 2011.76, 2023.53, 2018.23, 2033.62, 2022.24, 2010.83,
2009.79, 1954.57, 2101.77, 2007.61, 2034.71, 2109.56, 2173.75,
1959.35, 2010.4, 2098.15, 1976.98, 1961.81, 1890.4, 2115.34,
1976.8, 1963.54, 2038.5, 1963.56, 2001.21, 1889.72, 1890.61,
2120.13, 1996.95, 2168.35, 2098.63, 1963.51, 1974.27, 2120.01,
1973.45, 1963.76, 1971.47, 1965.09, 2040.76, 2120.84, 2086.92,
2124.03, 2132.81, 1889.83, 2043.69, 2052.11, 2158.47, 2047.67,2133.93, 2069.27, 2137.94, 2142.95, 1889.71, 2156.19, 2145.34,
2077.96, 2149.46, 2146.56, 1889.56, 2077.97, 1727.14, 1889.97,
1889.52, 1889.55, 1889.66, 1889.66, 1889.43, 1889.82, 1890.37,
1889.78, 1889.33, 1888.81, 1889.27, 1887.77, 1729.92, 1889.4,
1888.38, 1889.46, 1888.1, 1889.02, 1730.97, 1889.62, 1886.39,
1882.77, 1878.25, 1878.55, 1732.12, 1881.32, 1886.54, 1877.46,
1878.27, 1737.56, 1877.55, 1877.68, 1877.73, 1877.45, 1877.43,
1877.42, 1878.29, 1876.89, 1876, 1876.75, 1876.25, 1870.85, 1867.05,
1869.89, 1866.53, 1866.94, 1746.48, 1864.61, 1864.07, 1748.33,
1863.07, 1864.13, 1750.49, 1862.93, 1863.03, 1863.16, 1751.31,
1863.19, 1862.92, 1862.95, 1753.05, 1862.93, 1863.03, 1864.12,
1862.93, 1862.8, 1856.28, 1754.17, 1854.11, 1755.62, 1757.18,
1853.99, 1853.98, 1759.96, 1853.13, 1740.91, 1852.56, 1852.44,
1762.71, 1853.72, 1852.82, 1764.36, 1851.1, 1741.95, 1764.45,
1850.01, 1849.63, 1766.6, 1850.63, 1743.01, 1850.85, 1842.18,
1768.84, 1840.46, 1840.76, 1842.49, 1744.39, 1839.77, 1772.73,
1837.41, 1841.33, 1834.92, 1745.12, 1776.63, 1829.04, 1827.93,
1779.97, 1829.79, 1782.22, 1828.53, 1830.62, 1783.97, 1827.74,
1786.68, 1831.09, 1825.75, 1788.19, 1746.17, 1825.5, 1790.37,
1818.35, 1818.19, 1792.41, 1817.08, 1793.87, 1819.06, 1794.25,
1797.18, 1818.26, 1815.02, 1814.11, 1815.84, 1799.14, 1813.09,
1812.92, 1813.89, 1812.33, 1812.04, 1800.49, 1802.19, 1811.64,
1804.87, 1811.4, 1813.42, 1752.03, 1809.08, 1808.67, 1807.37,
1805.93, 1805.48, 1807.77, 1803.93, 1802.22, 1802.78, 1801.65,
1802.62, 1802.5, 1752.67, 1800.97, 1813.63, 1800.5, 1815.28,
1799.48, 1797.99, 1755.6, 1797.71, 1798.05, 1796.27, 1796.9,
1794.06, 1792.36, 1792.99, 1790.65, 1791.16, 1789.92, 1789.94,
1789.01, 1757.84, 1788.33, 1788.58, 1788.27, 1786.5, 1786.16,
1785.88, 1785.58, 1784.27, 1763.94, 1784.82, 1768.21, 1768.01,
1764.2, 1783.01, 1771.94, 1763.11, 1763.51, 1770.36, 1769.7,
1765.57, 1777.21, 1776.14, 1776.69, 1770.34, 1775.5, 1775.05,
1775.91, 1778.65, 1780.19, 1779.96, 1779.18, 1776.59, 1779.63,
1778.05, 1777.26, 1761.97, 1762.44, 1764.13, 1766.15, 1767.69,
1767.26, 1768.75, 1770.93, 1772.52, 1777.11, 1779.86, 1779.64,
1779.34, 1917.73, 1917.71, 1923.29, 1912.43, 1922.86, 1923.71,
1903.65, 1780.31, 1925.46, 1929, 1932.02, 1934.43, 1777.95, 1804.96,
1807.93, 1938.6, 1902.78, 1802.99, 1810.59, 1939.48, 1801.73,
1798.44, 1901, 1796.54, 1782.16, 1781.21, 1814.04, 1941.99, 1815.55,
1794.75, 1818, 1897.76, 1792.72, 1944.83, 1791.25, 1896.75, 1819.75,
1945.57, 1853.68, 1890.88, 1894.6, 1888.73, 1851.72, 1822.29,
1890.96, 1886.88, 1849.91, 1784.11, 1945.47, 1856.43, 1848.56,
1885.92, 1825.24, 1783.09, 1846.9, 1947.9, 1881.56, 1827.85,
1832.55, 1880.35, 1830.48, 1844.54, 1843.07, 1859.73, 1835.88,
1878.36, 1861.92, 1788.58, 1953.35, 1786.04, 1842.89, 1839.23,
1875.58, 1788.51, 1864.77, 1957.86, 1786.62, 1872.43, 1869.94,
1867.4, 1961.32, 1788.87, 1961.8, 1962.85, 1790.93, 1964.52,
1975.17, 1977.23, 1973.22, 1972.71, 1978.95, 1791.88, 1791.47,
1983.11, 1790.61, 1985.42, 1785.62, 1790.9, 1791.18, 1791.64,
1987.16, 1792, 1990.31, 1788.24, 1791.68, 1791.59, 1793.23, 1993.54,
1792.6, 1999.36, 1999.11, 1789.57, 1791.92, 1793.61, 2001.08,
2010.03, 1790.75, 1794.05, 2011.14, 2014.25, 1791.73, 1794.82,2017.13, 1794.22, 2022.89, 2023.66, 1795.95, 1793.59, 1795.52,
2027.24, 1799.63, 1797.86, 1798.08, 2033.89, 1795.03, 2034.61,
2035.44, 1797.2, 1796.28, 2035.16, 1811.38, 1815.54, 2041.69,
1796.7, 1815.21, 1812.67, 1796.2, 1814.42, 2044.94, 1816.37,
1815.83, 2045.72, 1816.91, 1811.4, 1815.11, 1796.26, 1809.74,
2049.19, 1797.83, 1808.23, 1818.27, 2057.36, 2072.06, 1801.35,
2060.49, 2061.48, 1802.16, 2243.49, 1806.6, 1804, 2168.52, 2154.35,
2158.78, 2234.3, 2168.46, 2173.64, 2146.41, 2230.64, 2075.03,
2179.79, 2109.15, 2116.69, 2142.9, 2124.1, 2220.3, 2222.55, 1818.53,
2216.72, 2196.56, 2179.52, 1824.68, 2192.8, 2098.23, 2129.92,
2186.49, 2184.4, 2203.64, 2083.22, 2208.83, 2135.86, 2087.47,
2131.31, 2205.24, 1821.12, 2083.4, 2131.92, 1821.53, 1821.77,
1823.3, 1797.87, 1823.67, 1822.63, 1821.61, 2092.94, 1824.39,
1824.59, 1828.08, 1829.39, 2096.16, 1828.28, 1828.44, 2100.71,
1827.75, 1800.58, 2106.87, 1828.26, 2110.01, 2112.92, 1828.96,
1801.75, 1831.11, 2119.06, 2119.9, 1831.43, 2120.19, 1802.65,
1832.43, 1833.84, 1805.05, 2121.7, 1833.51, 1806.83, 2129.11,
1809.04, 1810.34, 1833.15, 2132.54, 2135.95, 1810.58, 1834.28,
2142.68, 1812.44, 2144.2, 1814.18, 1835.95, 2145.67, 1815.11,
1818.33, 2149.61, 1837.32, 2153.75, 2157.44, 2169.83, 2174, 1838.69,
1818.93, 2179.41, 1839.41, 1951.04, 1820.96, 2181.19, 1839.94,
1941.84, 2196.77, 1821.99, 1940.25, 1840.09, 1932.06, 1938.69,
1924.98, 1937.19, 1932.57, 1932.28, 1839.95, 1822.15, 1919.97,
1840.47, 1919.86, 1841.51, 1823.52, 1917.86, 1843.54, 1825.24,
1916.44, 1844.93, 1827.12, 1914.46, 1914.19, 1828.12, 1913.22,
1827.88, 1846.16, 1829.38, 1832.26, 1848.55, 1907.12, 1906.78,
1906.99, 1850.9, 1835.08, 1903.74, 1901.55, 1837.54, 1851.59,
1898.61, 1840.15, 1897.77, 1851.68, 1852.35, 1852.33, 1896.24,
1841.05, 1854.28, 1893.12, 1842.15, 1841.55, 1841.62, 1841.44,
1856.49, 1891.74, 1843.09, 1890.18, 1845.1, 1888.84, 1870.77,
1871.77, 1858.39, 1857, 1871.29, 1886.55, 1845.39, 1878.96, 1885.84,
1845.45, 1880.05, 1879.16, 1857.02, 1850.36, 1867.19, 1862.37,
1851.63, 1851.91, 1868.64, 1862.6, 1852.53, 1870.75, 1864.11,
1852.46, 1864.66, 1853.31, 1852.88, 1871.04, 1853.86, 1865.5,
1853.47, 1873.25, 1867.24, 1858.89, 1868.92, 1857.28, 1876.26,
1870.66, 1858.34, 1877.39, 1871.34, 1859.81, 1878.99, 1871.35,
1859.23, 1886.02, 1859.71, 1873.11, 1883.71, 1867.03, 1884.62,
1877.96, 1863.53, 1888.45, 1876.56, 1863.37, 1888.76, 1881.78,
1863.69, 1892.6, 1880.54, 1864.96, 1896.4, 1883.5, 1909.95, 1866.91
)
First I force NDVI to be bound between 0 & 1 so that I can use a beta distribution
ndvi_corrected=(ndvi + 1)/2
Then I run a gam using mgcv
mod_el <- gam(ndvi_corrected ~ s(Elevation),
family = betar(link='logit'),
data = ndvi)
I am trying to make a plot that is interpretable, that is, the y-axis has NDVI values that are what we would expect "on the ground" based on the model predictions. So, the y-axis should be bound between -1 & 1. Here I am using code from Plotting output of GAM model.
preds <- predict(mod_el,se.fit=TRUE,data.frame(Elevation = Elevation))
summary((preds$fit+1)*2)
my_data <- data.frame(mu=preds$fit, low =(preds$fit - 1.96 * preds$se.fit), high = (preds$fit + 1.96 * preds$se.fit))
ggplot()+
geom_point(data= my_data, aes(x= Elevation, y=ndvi_corrected), size = 1, alpha = 0.5)+
geom_smooth(data=my_data,aes(ymin = low, ymax = high, x=Elevation, y = mu), stat = "identity", col="green")
So I am guessing (but it is hard to tell) that the above plot has 1. back-transformed mu from logit link and 2. includes the intercept in the prediction.
The next step I need is to unbind the mu from 0,1...that is, get it back from the ndvi_corrected state. To do this, I:
ggplot()+
geom_point(data= my_data, aes(x= ndvi_noNA, y=ndvi), size = 1, alpha = 0.5)+
geom_smooth(data=my_data,aes(ymin = ((low*2)-1), ymax = ((high*2)-1), x=ndvi_noNA, y = ((mu*2)-1)), stat = "identity", col="green")
But this doesnt seem to make sense either.
My questions are:
Upvotes: 1
Views: 265
Reputation: 910
Okay, so I think I've solved everything, if there are any other problems please let me know.
I created a data frame with the 1000 rows of data you provided (thanks for updating that), and I bind the two variables into a data frame called df
, then create the corrected variable bound between 0 and 1 for Beta regression. Then I fit the model.
# Created data frame
df <- data.frame(ndvi, Elevation)
# Transformed variable
df$ndvi_corrected <- (df$ndvi + 1)/2
# Fit model
mod_el <- gam(ndvi_corrected ~ s(Elevation),
family = betar(link='logit'),
data = df)
You then get your predicted values, which are on the logit scale (answer to question #1 I believe), and then you call summary on the predicted values which I think is an attempt at undoing the transformation that you did earlier to create ndvi_corrected
, but which produces values that are much higher than they should be.
preds <- predict(mod_el,se.fit=TRUE,data.frame(Elevation = Elevation))
summary((preds$fit+1)*2)
Min. 1st Qu. Median Mean 3rd Qu. Max.
3.468 3.607 3.760 3.771 3.922 4.320
The original transformation added 1 and then divided by 2. This means to undo it, we need to first multiply the values by 2, and then subtract one. But first, we need to get them from the logit scale back to the bounded variable between 0 and 1, which means applying the inverse logit. This function below does this:
backtransform <- function(x) {
x = ((exp(x)/(1+exp(x)))*2)-1
return(x)
}
summary(backtransform(preds$fit))
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.3515 0.3815 0.4136 0.4152 0.4466 0.5226
Now, if we create your plot with the original untransformed data along with the back-transformed model predictions, things look very reasonable. I've modified your code here, calling my backtransform
function on the predicted values to create second versions of them.
# Backtransforming predicted values
preds$fit2 <- backtransform(preds$fit)
preds$se.fit2 <- backtransform(preds$se.fit)
# Creating data frame of predicted values
my_data <- data.frame(mu=preds$fit2, low =(preds$fit2 - 1.96 * preds$se.fit2), high = (preds$fit2 + 1.96 * preds$se.fit2))
# Plotting the predicted values with original data
ggplot()+
geom_point(data= df, aes(x= Elevation, y=ndvi), size = 1, alpha = 0.5)+
geom_smooth(data=my_data,aes(ymin = low, ymax = high, x=Elevation, y = mu), stat = "identity", col="green")
So I think your problem was not back-transforming the model predictions appropriately, which caused all of the other confusion. I hope this solution scales and works with the full data-set.
As an aside, I normally plot GAMMs using the itsadug
package. The function plot_smooth
has an optional argument transform
that allows you to transform the y-axis with a custom function so you can use the function I defined above in the plotting call.
# Define function that uses inverse logit and then back-transforms your original transformation
# Plot values back-transformed to original scale
plot_smooth(mod_el,
view = "Elevation",
transform = "backtransform")
Upvotes: 1