if (FALSE) {
file <- system.file("external/sp1.shp", package="sdm") # get the path to the species data
sp1 <- vect(file) # read the shapefile for species 1
file <- system.file("external/sp2.shp", package="sdm")
sp2 <- vect(file) # read the shapefile for species 2
path <- system.file("external", package="sdm") # path to the folder contains the data
lst <- list.files(path=path,pattern='asc$',full.names = T) # list of predictor filenames
lst
preds <- rast(lst) # making a raster object
names(preds) # 4 environmental variables are used!
d1 <- sdmData(formula=Occurrence~., train=sp1, predictors=preds)
d1
d2 <- sdmData(formula=Occurrence~., train=sp2, predictors=preds)
d2
# fit models for species 1
m1 <- sdm(Occurrence~.,data=d1,methods=c('rf','glm','brt'),
replication='sub',test.p=30)
m1
# fit models for species 2:
m2 <- sdm(Occurrence~.,data=d2,methods=c('rf','glm','brt'),
replication='sub',test.p=30)
m2
# ensemble using weighted averaging based on AUC statistic (species 1):
p1 <- ensemble(m1, newdata=preds,setting=list(method='weighted',stat='AUC'))
plot(p1, main='Habitat Suitability in Geographic Space (species 1)')
# ensemble for species 2:
p2 <- ensemble(m2, newdata=preds,setting=list(method='weighted',stat='AUC'))
plot(p2, main='Habitat Suitability in Geographic Space (species 2)')
# maps together:
plot(c(p1,p2), main=c('species 1','species 2'))
# calculating niche similarity (all metrics) in geographic space:
nicheSimilarity(p1,p2)
nicheSimilarity(p1,p2, stat=c('Icor','Imod'))
######################################
# calculating niche similarity in environmental space:
# Mapping Ecological Niche using selected two variables
n1 <- niche(x=preds, h=p1, c('precipitation','temperature'),out = T)
n2 <- niche(x=preds, h=p2, c('precipitation','temperature'),out=T)
nicheSimilarity(n1,n2)
###################
#### Alternatively, predictors can be transformed to two components using PCA
pc <- pca(preds)
pc
# niche for first species based on the first two components of PCA:
n1 <- niche(pc@data,p1,c("Comp.1","Comp.2"),out=T)
n2 <- niche(pc@data,p2,c("Comp.1","Comp.2"),out=T)
nicheSimilarity(n1,n2)
}
Run the code above in your browser using DataLab