S3. Variance partitioning as a tool for controlling the type I error in ANOVA/MANOVA routines: a suggested R code. ### If you identify any problem or have any suggestion to improve this code, please contact the author of the manuscript (pedrov.eisenlohr@gmail.com). ### This code was prepared and edited by Danilo R.M. Neves (University of Leeds) and Pedro V. Eisenlohr (Universidade Federal de Minas Gerais), with the help of Mário José Marques Azevedo (Universidade Estadual de Campinas), considering the following references: # 1. Legendre, P., D. Borcard and D. W. Roberts. 2012. Variation partitioning involving orthogonal spatial eigenfunction submodels. Ecology 93: 1234-1240. # 2. Borcard, D., F. Gillet & P. Legendre. 2011. Numerical Ecology with R. Springer, New York, 302p. # 3. Peres-Neto, P. R. and P. Legendre. 2010. Estimating and controlling for spatial structure in the study of ecological communities. Global Ecology and Biogeography 19: 174-184. # 4. Blanchet F. G., P. Legendre, and D. Borcard. 2008. Forward selection of explanatory variables. Ecology 89: 2623-2632. # 5. Dray, S., P. Legendre and P. Peres-Neto. 2006. Spatial modelling: a comprehensive framework for principal coordinate analysis of neighbor matrices (PCNM). Ecological Modelling 196: 483-493. # 6. Legendre, P. and E. Gallagher. 2001. Ecologically meaningful transformations for ordination of species data. Oecologia 129: 271-280. # 7. Anderson, M. J. & P. Legendre. 1999. An empirical comparison of permutation methods for tests of partial regression coefficients in a linear model. Journal of Statistical Computation and Simulation 62: 271-303. ### Preparing data matrices: # X matrix: full model (without latitude and longitude). VarDep is the response variable # W matrix: latitude and longitude # Y matrix: only response variable (s) (VarDep or, in case of MANOVA, VarDep1, VarDep 2 etc.) ### Choose the directory (File->Change Dir...) ### If packages are not installed, run the following commands: install.packages("vegan") install.packages("packfor", repos="http://R-Forge.R-project.org") install.packages("spacemakeR", repos="http://R-Forge.R-project.org") install.packages("spdep") install.packages("venneuler") install.packages("tripack") ### Uploading the packages: library(vegan) library(packfor) library(spacemakeR) library(spdep) library(venneuler) library(tripack) ### Importing and verifying data matrices: X<-read.table(file.choose(),row.names=1,header=T,sep=",") edit(X) W<-read.table(file.choose(),row.names=1,header=T,sep=",") edit(W) Y<-read.table(file.choose(),row.names=1,header=T,sep=",") edit(Y) ### Generating and testing the significance of spatial variables (Moran's Eigenvector Maps MEMs): ll.temp<-tri2nb(W) ### Can be changed. See Borcard et al. (2011) and Legendre and Legendre (2012) for details. ll.transf = nb2listw(ll.temp, style = "minmax") ### Can be changed. See Borcard et al. (2011) and Legendre and Legendre (2012) for details. mem<-scores.listw(ll.transf) colnames(mem$vectors)<-paste("MEM",1:ncol(mem$vectors)) colnames(mem$vectors) mem.teste<-test.scores(mem,ll.transf,999) sel.mem<-mem sel.mem$vectors<-sel.mem$vectors[,mem.teste[,2]<0.05] dim(sel.mem$vectors) write.table(sel.mem$vectors,"mem_all.csv") ### Selecting MEMs via forward selection: lm1<-lm(VarDep~sel.mem$vectors, data=Y) r2.lm1<-RsquareAdj(lm1)$adj.r.squared r2.lm1 sel.lm1<-forward.sel(Y,sel.mem$vectors,adjR2thresh=r2.lm1) sel.lm1 ### Peres-Neto & Legendre (2010) suggested performing a MEMs selection for each response variable individually and, then, using those which have been selected at least once. ### The following commands should be used only for checking the ANOVA/MANOVA results when spatial structures are not considered. lm2<-lm(VarDep~treat, data=X) anova(lm2) r2.lm2<-RsquareAdj(lm2)$adj.r.squared r2.lm2 ### Preparing selected variables for variance partitioning: espaciais<-colnames(mem$vectors)%in%sel.lm1$variables sel.valores<-sel.mem$values[sel.lm1$order] sel.vetores<-sel.mem$vectors[,sel.lm1$order] dim(sel.vetores) write.table(sel.vetores,"mem_selected.csv") treat <- model.matrix(VarDep~treat, data=X)[ ,-1] ### Variance partitioning: all.varpart<-varpart(Y,treat,sel.vetores) all.varpart plot(all.varpart) ### Testing the significance of the treatment (fraction [a]), after considering the effect of selected MEMs: amb.rda<-rda(Y,treat,sel.vetores) teste.amb<-anova(amb.rda) teste.amb ### In case of significance, you should perform post hoc tests (Tukey etc.) using the selected MEMs as covariables.