Instantly share code, notes, and snippets.

Embed
What would you like to do?
library(entropy)
library(ggplot2)
olaps <- function(sp1, sp2){
sp1 <- sp1/sum(sp1)
sp2 <- sp2/sum(sp2)
plot.table <- data.frame(species = c(rep("sp1", length(sp1)), rep("sp2", length(sp2))),
env = c(seq(1:length(sp1)), seq(1:length(sp2))),
suitability = c(sp1, sp2))
return(list(
D = 1 - sum(abs(sp1 - sp2))/2,
I = 1 - sum((sqrt(sp1) - sqrt(sp2))^2)/2,
cor = cor(sp1, sp2, method = "spearman"),
suitplot = qplot(env, suitability, data = plot.table, col=species, geom = "line")
))
}
# Perfect positive correlation
sp1 <- seq(0.1, 1.0, 0.001)
sp2 <- seq(0.1, 1.0, 0.001)
olaps(sp1, sp2)
# Perfect negative correlation
sp1 <- seq(0.1, 1.0, 0.001)
sp2 <- seq(1.0, 0.1, -0.001)
olaps(sp1, sp2)
# Random
sp1 <- abs(rnorm(1000))
sp2 <- abs(rnorm(1000))
olaps(sp1, sp2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment