# Formal way to test if a non-linear approach is necessary to correlating environmental variables to NMDS ordination axes?

I've got a follow-up question to this post regarding correlating [non-]linear environmental variables to NMDS ordination axes.

My original plan was to use function envfit() in R's vegan package to determine the correlation coefficients and R2 of the relationships of environmental variables to an NMDS ordination of samples of species abundances. However, I learned from vegan coauthor, Gavin Simpson's, response to the linked post above, that such an approach is only valid if the environmental variable has a linear relationship with the ordination axes. He recommended use of the ordisurf() function instead. (which produces fitted contour lines vs more stackable linear vectors as from envfit).

Questions:

• What would be considered too nonlinear to instead use envfit?
• How do I actually determine if a non-linear approach is necessary?

Gavin and I had a quick back and forth in the comments of the linked post, but he encouraged that I post a new question.

Below is some sample data (from dput) that appears to be nonlinear (but too non-linear?? is the question!):

structure(list(NMS1 = c(-0.571533823150979, -0.589436373019653,
-0.600757502021191, -0.58223210062027, -0.582933403700019, -0.589608120935237,
-0.636191959925498, -0.651752371798833, 0.435550407646855, 0.435550407646854,
0.435550407646855, 0.435550407646855, 0.0927761971031468, 0.105530386556617,
-0.0485668606819158, -0.357386860084599, -0.363430504409257,
-0.367029257698869, -0.414308292758857, -0.468853337021418, -0.415011805210572,
-0.412134310137531, -0.413279047638911, -0.415521358057594, -0.455640632968033,
-0.465222790595584, -0.258254403062467, -0.258580958929316, -0.257121175407976,
-0.313160034444383, -0.323954150022626, -0.284713983961946, 0.435550407646855,
0.435550407646854, 0.435550407646855, 0.435550407646855, -0.0102315552121985,
-0.0338768491950303, 0.435550407646855, 0.435550407646854, 0.435550407646854,
0.435550407646855, 0.435550407646855, -0.0892031988179496, -0.322178545826041,
-0.401228837225579, 0.435550407646855, 0.435550407646854, 0.435550407646855,
-0.119991750153387, -0.135884007345849, -0.111519230189533, -0.120749407109965,
0.435550407646854, 0.435550407646855, 0.435550407646855, 0.435550407646855,
0.00493231527090545, -0.321773772549244, 0.434947722126969, 0.434753712873496,
0.435550407646855, 0.435550407646855, 0.236809625659408, 0.160058368913337,
-0.00821281272382983, 0.435550407646854, 0.435550407646855, 0.435550407646855,
0.435550407646855, 0.100846098342375, 0.0675080258448837, 0.364456936962118,
0.466195363768222, 0.369274521252075, 0.372820815316297, -0.214965939317993,
-0.251179911106452, -0.666212701558072, 0.435550407646854, 0.435550407646854,
0.435550407646855, 0.435550407646855, 0.435550407646855, -0.0738886264664264,
0.434385722061976, 0.434451694392928, 0.435550407646854, 0.435550407646855,
0.435550407646855, 0.11323362965479, -0.134625975901192, -0.395180488909748,
0.433889016787792, 0.43389825836731, 0.435550407646855, 0.0968339036708512,
0.0829333960835875, -0.195271736156025, -0.438013826851635, 0.434952173876556,
0.435550407646854, 0.435550407646855, 0.435550407646855, -0.313464709106529,
-0.36638914474981, 0.376836749815773, 0.133381057552547, 0.058078972491914,
0.130303188702929, -0.16060238265999, -0.177773324854286, -0.223014494679199,
-0.234321505272004, 0.131639761582913, 0.283088298010558, 0.0345736105356725,
-0.0076627539796595, -0.0012441599107253, 0.157533683233566,
0.293311530022732, 0.128094602035681, 0.0790648683559848, -0.239312366787299,
-0.246328256988428, -0.232929776646792, 0.404794080099789, 0.404189060292601,
0.374245076693626, 0.435550407646855, 0.435550407646855, -0.0614226695739214,
0.411858499336661, 0.411405225602958, 0.415799271158231, 0.403428113931917,
0.391147506547123, 0.0593201386831803, 0.00548516165833276, 0.178392399041157,
-0.61942428255684, -0.61422427536195, -0.629873049819325, -0.629253638436057,
-0.651309623205852, -0.637605194693535, -0.637770464048963, -0.709097587012449,
-0.663724465484384, -0.679911186020965, -0.681029849035514, 0.390841396595386,
0.352879869939554, 0.283510023339836, -0.339854992409066, -0.317261138096206,
-0.583613121197508, 0.415634069868744, 0.402641636964312, 0.287389613094922,
0.413779692664365, -0.0826256212957902, 0.0180122722848601, 0.101525885038986,
0.396697125571528, 0.363225896223261, 0.220269856410258, -0.193294873562582,
-0.270660875406708, -0.309542296813648, 0.237665129256262, 0.230705215299842,
-0.207642520194385, -0.258880605016031, -0.272163023081107, 0.183717258362963,
0.274016260140473, 0.255506615204614, 0.280986344428903, -0.0912986976746207,
0.105032328015983, -0.71363770813358, -0.70935540627941, -0.710003027628952,
-0.728272896398928, -0.686849571959289, -0.716046520834526, -0.577082470442168,
-0.553910605267051, -0.640778283354797, -0.638167556856845, -0.645849298459387,
0.25809889287876, 0.203464402561037, -0.0400951644406734, -0.0274810558804936,
-0.21493770417535, -0.175569341472251, 0.426284621722218, 0.423585049265667,
0.393058154440033, 0.0218433169126962, 0.3126309822064, -0.238714847641894,
-0.144745317083338, -0.136559070942386, 0.136090683410041, 0.0674392466812571,
-0.00435301164823319, -0.112390710450336, -0.0910352916387604,
-0.251878699935269, -0.335188458088733, 0.0930723010484974, 0.168159260068383,
0.126664197659234, -0.14128311055289, 0.412117703303488, 0.407683496908572,
0.416582570483196, 0.167568807406547, 0.116628292422136, -0.154255877543003,
-0.112064541036526, -0.180607574390579, 0.373997652279553, 0.358139395606619,
0.15701761790352, -0.0526206958284176, -0.0530257537259154, 0.0121682335467888,
0.151631703573019), NMS2 = c(0.406967568307268, 0.421056511973023,
0.461138415115048, 0.458994193665071, 0.494857482545949, 0.492701150817477,
0.440219526429868, 0.411703097728796, 0.125131723652694, 0.125131723652694,
0.125131723652694, 0.125131723652694, -0.343076169744694, -0.35566961910044,
0.328570002433686, 0.665175595217182, 0.670853874531843, 0.66885273709489,
0.549977753812763, 0.478078016636868, 0.597013688897382, 0.612004289345008,
0.611446021309466, 0.615618525310342, 0.552599654930181, 0.53642558939018,
0.643637139616867, 0.655088898645172, 0.66600807523602, 0.709421285684683,
0.716350257875468, 0.735643899538481, 0.125131723652694, 0.125131723652694,
0.125131723652694, 0.125131723652694, -0.375373409466695, -0.377422714851016,
0.125131723652694, 0.125131723652694, 0.125131723652694, 0.125131723652694,
0.125131723652694, -0.261371433685269, -0.366515668849476, -0.554986317826014,
0.125131723652694, 0.125131723652694, 0.125131723652694, 0.0896706404757454,
0.121333719396706, 0.200642828733534, 0.270714738488218, 0.125131723652694,
0.125131723652694, 0.125131723652694, 0.125131723652694, -0.063248369537954,
-0.166175912070998, 0.115090343357088, 0.12256531388867, 0.125131723652694,
0.125131723652694, -0.195975331892813, -0.127783750182995, -0.288205679720217,
0.125131723652694, 0.125131723652694, 0.125131723652694, 0.125131723652694,
-0.412557189374149, -0.39719920215988, 0.241404953913073, -0.0223024548139457,
0.221482601991536, 0.212797392486135, 0.0291538221692346, 0.00993162025944313,
-0.0699624925810312, 0.125131723652694, 0.125131723652694, 0.125131723652694,
0.125131723652694, 0.125131723652694, 0.145576507786419, 0.120699960901922,
0.121295240460029, 0.125131723652694, 0.125131723652694, 0.125131723652694,
-0.146450961935236, -0.26088280203489, -0.183306220427207, 0.119376190950239,
0.119325655975859, 0.125131723652694, -0.171625485591029, -0.167767511012776,
-0.292614122808434, -0.301610705818433, 0.1254920654645, 0.125131723652694,
0.125131723652694, 0.125131723652694, -0.320029698159584, -0.351855509062361,
-0.267088929316478, 0.336802877816073, 0.314669521194105, -0.277514810621095,
0.0950158408184286, 0.0976976897404307, 0.142487722865411, 0.315712324569586,
-0.0233212410229224, -0.0135532759133549, -0.120759238005887,
-0.208029162677397, -0.221183855832797, -0.346290838738349, -0.107571939658168,
0.164488414161045, 0.138447827680684, -0.0748651817468282, -0.071232275120516,
0.0176927573300177, 0.158394175373522, 0.158509386568348, 0.130152008842204,
0.125131723652694, 0.125131723652694, -0.198060476336384, 0.141971243538825,
0.141673203128314, 0.131794495532393, 0.119608416039638, 0.108585287632788,
-0.137468339639506, -0.0401587770487287, -0.217342114806509,
0.249494995518928, 0.267784215069208, 0.265740745445064, 0.269913897159721,
0.206445076964772, 0.217591122182495, 0.221477996613845, 0.0518172729118026,
0.0453784080482054, 0.0872185442538041, 0.102718870490628, 0.0952763810193002,
0.0658334709979153, 0.0216064497098403, -0.0480499323890218,
-0.0384633699193948, -0.0215784133619366, 0.117650736539789,
0.114264280776928, 0.0584377221498822, 0.114736788702303, -0.211672171164431,
-0.171775009600994, -0.243599503933446, -0.172930979595001, -0.181024052542295,
-0.341042783979889, -0.34526162101458, -0.375618841868776, -0.393358288628527,
-0.508743352993071, -0.513015556984515, -0.457908358761689, -0.474969351809608,
-0.449783707469865, 0.207617031831611, -0.348421752783521, -0.384231236084215,
-0.338260649917476, -0.416277047272311, -0.445160107423671, -0.214157857862604,
-0.203235686328853, -0.170411391887729, -0.163056650309562, -0.317583157409136,
-0.310379776224529, -0.552625685232666, -0.584950906854886, -0.434651567146437,
-0.440706858277182, -0.460683786308098, -0.26471454578077, -0.286276078057675,
-0.314191194556906, 0.00389935595467848, -0.0243386728536483,
0.00812845203109148, 0.126782019279665, 0.124105032535032, 0.107941410221789,
-0.0855712962829527, 0.141089496800796, -0.188890649005355, -0.134245770054573,
-0.0717794559974599, -0.571419304531548, -0.617248383446321,
-0.550935072876868, -0.584373382116658, -0.546481130895051, -0.602422435792026,
-0.658023912918462, 0.165399816159342, -0.397604835483968, -0.320935016560999,
-0.0469032622790751, 0.0838786020377899, 0.0677101311183695,
0.03077569690418, -0.104173022442671, -0.0823122028787323, -0.147220332090287,
-0.111993076910129, -0.107491946839659, 0.0944395168847612, 0.0887106479709413,
-0.0268259890316029, -0.12006554001254, -0.113327020519115, 0.00808087601686215,
-0.2438832004046), EnvVar = c(0.64, 0.64, 0.64, 0.64, 0.64, 0.64,
0.64, 0.64, 0.83, 0.83, 0.83, 0.83, 0.83, 0.83, 0.83, 0.75, 0.75,
0.75, 0.75, 0.83, 0.83, 0.83, 0.83, 0.83, 0.83, 0.83, 0.82, 0.82,
0.82, 0.82, 0.82, 0.82, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.82,
0.82, 0.82, 0.82, 0.82, 0.82, 0.82, 0.82, 1.01, 1.01, 1.01, 1.01,
1.01, 1.01, 1.01, 0.77, 0.77, 0.77, 0.77, 0.77, 0.77, 0.88, 0.88,
0.88, 0.88, 0.88, 0.88, 0.88, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96,
0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.79, 0.79, 0.79, 0.79, 0.79,
0.79, 0.83, 0.83, 0.83, 0.83, 0.83, 0.83, 0.83, 0.83, 0.77, 0.77,
0.77, 0.77, 0.77, 0.77, 0.77, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75,
0.87, 0.87, 0.87, 0.87, 0.87, 0.87, 0.87, 0.87, 0.84, 0.84, 0.84,
0.84, 0.84, 0.84, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 1.26, 1.26,
1.26, 1.26, 1.26, 1.26, 1.03, 1.03, 1.03, 1.03, 1.03, 1.03, 1.03,
1.03, 0.92, 0.92, 0.92, 0.92, 0.92, 0.7, 0.7, 0.7, 0.7, 0.7,
0.7, 0.68, 0.68, 0.68, 0.68, 0.68, 0.68, 0.95, 0.95, 0.95, 0.95,
0.95, 0.95, 0.95, 0.78, 0.78, 0.78, 0.78, 0.78, 0.78, 0.97, 0.97,
0.97, 0.97, 0.97, 0.84, 0.84, 0.84, 0.84, 0.84, 0.84, 0.83, 0.83,
0.83, 0.83, 0.83, 0.83, 0.86, 0.86, 0.86, 0.86, 0.86, 0.66, 0.66,
0.66, 0.66, 0.66, 0.66, 0.77, 0.77, 0.77, 0.77, 0.77, 0.77, 0.77,
0.77, 0.89, 0.89, 0.89, 0.89, 0.89, 0.89, 0.89, 0.64, 0.64, 0.64,
0.64, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.91, 0.87, 0.87,
0.87, 0.87, 0.87, 0.87, 0.87)), row.names = c(NA, 232L), class = "data.frame")


This is not a test if non-linear fit is necessary, but it is a test of the "significance" of its non-linearity:

library(vegan)
data(mite, mite.env)
mod <- metaMDS(mite, trace=FALSE)
fit <- ordisurf(mod ~ SubsDens, data=mite.env) # (possibly) nonlinear
fit0 <- ordisurf(mod ~ SubsDens, data=mite.env, knots=1) # forced linear
anova(fit0, fit, test = "F") # test against linear null

• Thanks. Elegant and simple +1. Two questions: (1) when I apply your code to Gavin's example in the linked post, the p-value is blank (technically NA I guess). Any idea why? (2) Any suggestion for how to describe/cite this method in a manuscript? Commented Jan 19, 2023 at 13:55
• Didn't see where this blankness pops up, but did you specify argument test in your anova() call? The default for glm+gam models is to only tabulate and you must specifically request the test and the kind of test to get the P-value (I requested test="F", where F does not mean FALSE but Fisher). Commented Jan 19, 2023 at 14:22
• yes. See my screenshot for outcome. Commented Jan 19, 2023 at 16:13
• This calls for @GavinSimpson comments: both models seem to be give fairly similar linear responses, but the straight linear model uses three degrees of freedom (intercept + two axes) whereas the GAM only uses 2.59 degrees of freedom (intercept + 1.6 axes). It seems that anova does not know what to do with this anomalous model. Anyway, in this case the response is linear. Commented Jan 19, 2023 at 22:11
• I had a look at the guts of anova. WIth list of these two models, anova.glm is used for linear (knots=1) and anova.gam for smooth model. anova.gam re-evaluates model degrees of freedom, and finds them be 3.24 instead of 2.59 of the first report. Non-linear model uses more DoF than linear model and should be better, but it is worse: residual deviance increases. Changes in resid DoF and resid Dev have different signs, and this confuses F-test and P=NA. Why? Because splines do bad job fitting linear planes, and it is better use linear model (knots=1). Blank P => it's linear. @GavinSimpson? Commented Jan 20, 2023 at 12:40