-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.R
58 lines (42 loc) · 1.53 KB
/
main.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
47
48
49
50
51
52
53
54
55
56
57
58
library(rgdal)
library(raster)
library(ENMeval)
library(dplyr)
library(magrittr)
library(iterpc)
covarFileList <- tools::list_files_with_exts('data/', "tif")
env <- raster::stack(covarFileList)
occs <- read.csv('data/data_wo_duplicates.csv')
occs <- dplyr::select(occs, Dec_Long, Dec_Lat)
bg.df <- as.data.frame(dismo::randomPoints(env[[1]], n = 10000))
sp.models <- ENMeval::ENMevaluate(occs, env, bg.df, RMvalues = c(1),
fc = c("L"),
method = "jackknife", bin.output = TRUE,
updateProgress = TRUE)
resultsData <- sp.models@results
resultsData %<>% select(settings, contains('ORmin_'), contains('ProbMin'))
probs <- resultsData %>% select(contains('ProbMin')) %>% unlist(., use.names = TRUE)
obsState <- resultsData %>% select(contains('ORmin_')) %>% unlist(., use.names = TRUE)
zProb <- function(state, probs) {
success <- probs^state
fails <- (1-probs)^(1-state)
return(prod(success, fails))
}
zStat <- function(state, probs) {
d <- sum(state*(1-probs))
pD <- zProb(state, probs)
r <- c(d,pD)
names(r) <- c('D', 'pD')
return(r)
}
I <- iterpc(table(c(0, 1)), nrow(occs), ordered = TRUE, replace = TRUE)
no_perm <- getlength(I)
results <- data.frame(D = double(), pD = double())
for (i in 1:no_perm) {
s <- getnext(I)
z_statistic <- zStat(s, probs)
results <- rbind(results, z_statistic)
}
names(results) <- c('D', 'pD')
obsStat <- zStat(obsState, probs)
results %>% filter(D >= obsStat[1]) %>% summarise_at('pD', sum)