Supplementary Software 1 A meta-analysis of global fungal distribution reveals climate-driven patterns Tomáš Větrovský, Petr Kohout, Martin Kopecký, Antonín Machač, Matěj Man, Barbara Doreen Bahnmann, Vendula Brabcová, Jinlyung Choi, Lenka Meszárošová, Zander Rainier Human, Clémentine Lepinay, Salvador Lladó, Rubén López-Mondéjar, Tijana Martinović, Tereza Mašínová, Daniel Morais, Diana Navrátilová, Iñaki Odriozola, Martina Štursová, Karel Švec, Vojtěch Tláskal, Michaela Urbanová, Joe Wan, Lucia Žifčáková, Adina Chuang Howe, Joshua Ladau, Kabir Gabriel Peay, David Storch, Jan Wild, Petr Baldrian ### Analyses of "environmental drivers" part of the paper by Vetrovsky et al. "" in Nature Communications. ### R script written by Martin Kopecký (ma.kopecky@gmail.com) and works in R library(HH) # ver. 3.1-35 library(vegan) # ver. 2.5-2 library(extendedForest) # ver. 1.6.1 library(gradientForest) #ver. 0.1-17 #set WD setwd("c:/MK/Dokumenty/DATA/Rdata/globalFungi") ### GRADIENT FOREST # load envi data for 742 aggregated sites envi.dta <- read.table("env_ClimSoilNdvi.csv", row.names=, header=T, sep=";",dec=".") # select final envi data for models envi <- envi.dta[,c("CH_2","CH_18","CH_9","CH_19","CH_8","CH_15","GPP", "BulkD", "pH")] # check VIF (all variables with VIF<5) sort(HH::vif(chelsa), decreasing = T) # SH occurence data in 742 aggreegated sites species <- read.table("2019_top496_agreg_sites.csv", row.names=1, header=T, sep=";",dec=".") # remove site identification species2 <- species[,3:471] # transform to P/A data species.pa <- decostand(species2, "pa") nSites <- dim(species2)[1] nSites nSpecs <- dim(species2)[2] nSpecs # set levels for conditional permutation lev <- floor(log2(nSites * 0.368/2)) lev # fit gradient forest gf <- gradientForest(cbind(envi, species.pa), predictor.vars = colnames(envi), response.vars = colnames(species.pa), ntree = 500, transform = NULL, compact = T, nbin = 201, maxLevel = lev, corr.threshold = 0.5, trace=T) # sort outputs from random forest model for each SH with R2>0 sorted_models <- gf$res.u[order(gf$res.u$spec, -gf$res.u$rsq.var),] sorted_models ### RICHNESS ACROSS LATITUDES AND REGIONS ### REGRESSION ANALYSES ### INPUT ### x ... dataframe with cells, their richness, latitude, longitude ### ndata ... dataframe with climatic data for all grid cells (from chelsa) library(SDMTools) library(maptools) library(raster) ### EXPLORE GEOGRAPHIC PATTERNS IN THE RICHNESS DATA ### upload map template from directory blank <- raster.from.asc(read.asc ("C:\\blank_map.asc")) ### template map blank -> blank0 blank[] <- ndata$bio1 ### plot richness on the map showing world climate plot (blank, main= "richness across the world - against bio1") points (x$longitude, x$latitude, pch=20, cex = x$sprich,col="darkblue") ### loess fitting plot (x$sprich ~ x$latitude, xlab = "Latitude",ylab="richness",font.lab=2,pch=19, col="darkgrey", main="Latitudinal gradient (LDG)") mo1 <- cbind(x$latitude,predict (loess (x$sprich ~ x$latitude, span=0.75, degree=2))) lines( mo1[order(mo1[,1]),],col="darkblue",lwd=4) ################################################################ ### CREATE DATASET WITH SPECIES RICHNESS, CHAO, AND CHELSA CLIMATIC VARIABLES xdataset <- data.frame(x,matrix(ncol=dim(ndata)[[2]]-2,nrow=dim(x)[[1]],data=NA)) for (i in 1:19){ blank [] <- ndata[,2+i] xdataset[,10+i] <- extract (blank, xdataset[,c("longitude","latitude")]) names(xdataset)[10+i] <- names(ndata)[2+i] } ### save the created dataset, including environmental predictors write.table(xdataset,file="Fungi dataset OTU.xls",sep="\t",row.names=F) ### run best-subset regression on the environmental data library (leaps) xdataset2 <- xdataset [complete.cases(xdataset),] rg1 <- regsubsets (xdataset2[,c(11:29)],xdataset2$sprich) which.max(summary(rg1)$adjr2) which.min(summary(rg1)$cp) which.min(summary(rg1)$bic) ### identify the best model and predict geographic patterns in richness based on the model ### (here shown an example of a linear model with eight predictors) coef(rg1,8) ft1 <- lm (sprich ~ bio2 + bio3 + bio4 + bio5 + bio6 + bio12 + bio13 + bio17, data= xdataset2) pr1 <- predict (ft1,ndata[,c("bio2","bio3","bio4","bio5","bio6","bio12","bio13","bio17")],interval="predict") blank[] <- pr1[,"fit"] ### fit plot(blank, main = "richness R2=17.31%") ### plotting library(rasterVis) dev.new(width=25, height=15) blank[] <- pr1[,"fit"] ### fit myTheme <- rasterTheme(region=(brewer.pal('Reds', n=9))) levelplot(blank, margin = list (FUN = "sum"), par.settings=myTheme, contour=F, main = "richness R2=17.31%") dev.new(width=25, height=15) blank[] <- pr1[,"lwr"] ### lower prediction interval myTheme <- rasterTheme(region=(brewer.pal('PuBu', n=9))) levelplot(blank, margin = list (FUN = "sum"), par.settings=myTheme, contour=F, main = "richness LOWER BOUND R2=17.31%") dev.new(width=25, height=15) blank[] <- pr1[,"upr"] ### upper prediction interval myTheme <- rasterTheme(region=(brewer.pal('PuBu', n=9))) levelplot(blank, margin = list (FUN = "sum"), par.settings=myTheme, contour=F, main = "richness UPPER BOUND R2=17.31%") ### RANDOM FOREST ### predicting species richness, based on climate, environment, etc. ### input ### x ... dataframe with richness, chao, chelsa climate, environment, etc. ### ndata ... dataframe with chelsa climate, soil and vegetation for the entire world library(randomForest) xcomp <- x[complete.cases(x),] rdf <- randomForest (sprich ~ CH_01 + CH_02 + CH_03 + CH_04 + CH_05 + CH_06 + CH_07 + CH_08 + CH_09 + CH_10 + CH_11 + CH_12 + CH_13 + CH_14 + CH_15 + CH_16 + CH_17 + CH_18 + CH_19 + BLDFIE_M_sl1_ + CECSOL_M_sl1_ + EVMMOD3a_ + OCSTHA_M_sd1_ + PHIHOX_M_sl1_ + veg_gpp_mod17a2h.annual_d_, ntrees = 500, data = xcomp) rdf plot (rdf$predicted ~ xcomp$sprich, col="darkblue",pch=19) ### PLOT ON THE MAP library(SDMTools);library(maptools);library(raster);library(rasterVis) blank <- raster.from.asc(read.asc ("C:\\blank_map.asc")) ### template map uploaded from directory dev.new(width=25, height=15) blank[] <- predict (rdf,newdata=ndata[,-(1:2)]) ### fit myTheme <- rasterTheme(region=(brewer.pal('Reds', n=9))) levelplot(blank, margin = list (FUN = "sum"), par.settings=myTheme, contour=F, main = "Random forest")