-
Notifications
You must be signed in to change notification settings - Fork 1
/
temporalAlpha.R
46 lines (35 loc) · 1.32 KB
/
temporalAlpha.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#-----------------#
# ALPHA DIVERSITY #
#-----------------#
# Version 1.0
alphaRaster <- function(x, y, atree){
if (!require("raster")) install.packages("raster")
require(raster)
# if (!require("BAT")) install.packages("BAT")
# require(BAT)
# Convert raster to dataframe
data.1 <- as.data.frame(x, xy=F)
data.2 <- as.data.frame(y, xy=F)
coord <- as.data.frame(x, xy=T)[ ,1:2]
# Replace NA with 0
data.1[is.na(data.1)] <- 0
data.2[is.na(data.2)] <- 0
# Load alpha function from BAT
source("/Volumes/GoogleDrive/Il mio Drive/Odonata_SDM/R_script/Function/BetaRaster/BAT_Beta.R")
if(missing(atree)){
alpha.db <- data.frame(present = alpha(data.1),
future = alpha(data.2))
colnames(alpha.db) <- c("present", "future")
alpha.db$diff <- alpha.db$future - alpha.db$present
} else {
alpha.db <- data.frame(present = alpha(data.1, atree),
future = alpha(data.2, atree))
colnames(alpha.db) <- c("present", "future")
alpha.db$diff <- alpha.db$future - alpha.db$present
}
a.present.raster <- rasterFromXYZ(cbind(coord, alpha.db$present))
a.future.raster <- rasterFromXYZ(cbind(coord, alpha.db$future))
a.diff.raster <- rasterFromXYZ(cbind(coord, alpha.db$diff))
return(results <- list(alpha = alpha.db,
aplpha.raster = stack(a.present.raster, a.future.raster, a.diff.raster)))
}