-
Notifications
You must be signed in to change notification settings - Fork 1
/
MapGDMLight.R
31 lines (26 loc) · 1006 Bytes
/
MapGDMLight.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
MapGDMLight<-function(rastTrans){
#Maps general dissimilarity
#Args:
# occ.table: data frame with species occurrence information.
# Must contain lon,lat,species and site columns.
# env.vars: RasterStack or RasterBrick of environmental
# variables that define gradients of interest
#
#Returns:
# A raster showing the general dissimilarity model
require(gdm)
require(raster)
#Plot dissimilarity
cellsWData<-Which(!is.na(rastTrans[[1]]),cells=T)
rastDat<-rastTrans[cellsWData]
pcaSamp <- prcomp(rastDat)
pcaRast <- predict(rastTrans, pcaSamp, index=1:3)
# scale rasters
pcaRast[[1]] <- (pcaRast[[1]]-pcaRast[[1]]@data@min) /
(pcaRast[[1]]@data@max-pcaRast[[1]]@data@min)*255
pcaRast[[2]] <- (pcaRast[[2]]-pcaRast[[2]]@data@min) /
(pcaRast[[2]]@data@max-pcaRast[[2]]@data@min)*255
pcaRast[[3]] <- (pcaRast[[3]]-pcaRast[[3]]@data@min) /
(pcaRast[[3]]@data@max-pcaRast[[3]]@data@min)*255
return(pcaRast)
}