Skip to content

Instantly share code, notes, and snippets.

@danlwarren
Created October 5, 2018 10:56
Show Gist options
  • Save danlwarren/5509c6a45d4e534c3e0c0ecf1702bbdd to your computer and use it in GitHub Desktop.
Save danlwarren/5509c6a45d4e534c3e0c0ecf1702bbdd to your computer and use it in GitHub Desktop.
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