Skip to content

Instantly share code, notes, and snippets.

@cpfiffer
Created February 7, 2018 04:19
Show Gist options
  • Save cpfiffer/e0c67fe2457ec1c7b77b2a52d35d9f5e to your computer and use it in GitHub Desktop.
Save cpfiffer/e0c67fe2457ec1c7b77b2a52d35d9f5e to your computer and use it in GitHub Desktop.
# Misc functions for conservation biology stats in R.
shannon <- function (x) {
proportion_x = x/sum(x)
print(proportion_x)
print(log(proportion_x))
print(proportion_x * log(proportion_x))
print(sum(proportion_x * log(proportion_x)))
return(-sum(proportion_x*log(proportion_x)))
}
hills <- function (x) {
return(exp(shannon(x)))
}
simpson <- function (x) {
p = x/sum(x)
ans = 1/sum(p^2)
return(ans)
}
a <- c(120, 120, 120, 120)
b <- c(78, 80, 76, 78, 78, 78, 77)
c <- c(623, 17, 12, 24, 7, 18, 6)
d <- c(470, 34, 22, 25)
e <- c(17, 31, 7, 12, 42, 29)
whitaker <- function(x, total) {
alpha = mean(x)
print(alpha)
beta = total/alpha - 1
return(beta)
}
whit_a <- c( 7, 8, 8, 6, 7, 7)
whit_b <- c(22, 12, 4, 7, 15, 8)
whit_c <- c(46, 50, 51, 39, 44, 43)
tot_a <- 9
tot_b <- 34
tot_c <- 71
whitaker(whit_a, tot_a)
whitaker(whit_b, tot_b)
whitaker(whit_c, tot_c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment