# start with the southwest data set
# grab the columns with xy, site ID, and species data
sppTab <- southwest[, c("species", "site", "Lat", "Long")]
##fit gdm using rasters
rastFile <- system.file("./extdata/swBioclims.grd", package="gdm")
envRast <- terra::rast(rastFile)
sitePairRast <- formatsitepair(sppTab, 2, XColumn="Long", YColumn="Lat",
sppColumn="species", siteColumn="site",
predData=envRast)
##remove NA values
sitePairRast <- na.omit(sitePairRast)
##fit raster GDM
gdmRastMod <- gdm(sitePairRast, geo=TRUE)
##raster input, raster output
transRasts <- gdm.transform(gdmRastMod, envRast)
# map biological patterns; increase maxcell if using large rasters
pcaSamp <- terra::prcomp(transRasts, maxcell = 1e4)
# note the use of the 'index' argument
pcaRast <- terra::predict(transRasts, pcaSamp, index=1:3)
# stretch the PCA rasters to make full use of the colour spectrum
pcaRast <- terra::stretch(pcaRast)
terra::plotRGB(pcaRast, r=1, g=2, b=3)
Run the code above in your browser using DataLab