This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to calculate Euclidean distance between n sets of Principal Components | |
# Ensure all data are in data.matrix format | |
# | |
# x = data matrix 1 | |
# y = data matrix 2 | |
# d = distance method (pdist = unequal sets; dist = equal sets) | |
# e.g. | |
# distance <- euDist(dat1, dat2, dist = "dist") | |
# mean(distance) | |
# sd(distance) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create species presence/absence raster | |
# mask.raster = raster of focal area | |
# species.data = presence points of focal species | |
# raster.label = label for raster (e.g. species name) | |
# | |
# example | |
# pa.ras <- presabRa(mask.raster = raster1, species.data = hares, raster.label = "Lepus sp.") | |
# See https://amywhiteheadresearch.wordpress.com/2016/01/25/extracting-raster-data-using-a-shapefile/#more-918 for more info] | |
presabRas <- function (mask.raster,species.data,raster.label="") { | |
require(raster) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install.packages("pwr") | |
library(pwr) | |
# pwr.anova.test(k = , n = , f = , sig.level = , power = ) | |
# k = number of groups | |
# n = number of samples | |
# f = effect size | |
# sig.level = significance level | |
# power = power (0-1) | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a dataframe of example data (here derived from raptor growth metrics) | |
df <- matrix(c(24,NA,365,1,NA,NA,6,33,NA,10,59,NA,37,300,477,NA,233,312,NA,NA,450,4,28,49),byrow = T, 8,3) | |
colnames(df) <- c("age","wing","mass") | |
df <- as.data.frame(df) | |
df$loc=c("Flev","Flev","Flev","Ters","Ters","Schi","Schi","Schi") | |
df$yr=c("2004","2004","2004","2007","2007","2004","2004","2008") | |
df | |
# split data by focal column and remove duplicates from a span to columns, keeping the one with the most data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Capitalise the first letter of each word in a string | |
# From https://stackoverflow.com/questions/6364783/capitalize-the-first-letter-of-both-words-in-a-two-word-string | |
# | |
# e.g. | |
# name <- c("great shearwater", "black-browed albatross", "southern giant petrel") | |
# sapply(name, simpleCap) | |
simpleCap <- function(x) { | |
s <- strsplit(x, " ")[[1]] | |
paste(toupper(substring(s, 1,1)), substring(s, 2), | |
sep="", collapse=" ") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calculate total line length and density for a given set of polygons | |
# | |
# Intersects SpatialLine and SpatialPolygon objects, calculates individual line length, | |
# appends to dataframe extracted from intersect object and summarises by polygon ID. | |
# If density is not required, do not specify area parameter `a`. | |
# | |
# The function assumes a spatial projection system where distance is given in metres. | |
# | |
# E.g. | |
# sp1 = object of class SpatialLine/SpatialLineDataFrame |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# published on http://www.r-statistics.com/2010/07/visualization-of-regression-coefficients-in-r | |
# originally written by "<a href="http://statmath.wu.ac.at/~zeileis/">Achim Zeileis</a>" | |
# GPL-2 | |
# | |
# E.g. coefplot(glm, parm = -1) | |
coefplot <- function(object, df = NULL, level = 0.95, parm = NULL, | |
labels = TRUE, xlab = "Coefficient confidence intervals", ylab = "", | |
xlim = NULL, ylim = NULL, | |
las = 1, lwd = 1, lty = c(1, 2), pch = 19, col = 1, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The modOverlap function calculates three metrics: | |
# Shoener’s D for niche overlap | |
# Hellinger distance between probability distributions | |
# Warren's I similarity statistic | |
# | |
# This function is also found in the fuzzysim package | |
# | |
# From https://modtools.wordpress.com/2015/10/30/modoverlap/ | |
modOverlap <- function (pred1, pred2, na.rm = TRUE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# E.g. qDat.corr <- as.data.frame(cor.test.p(x)) | |
cor.test.p <- function(x){ | |
FUN <- function(x, y) cor.test(x, y)[["p.value"]] | |
z <- outer( | |
colnames(x), | |
colnames(x), | |
Vectorize(function(i,j) FUN(x[,i], x[,j])) | |
) | |
dimnames(z) <- list(colnames(x), colnames(x)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# if tweetnow is set to TRUE, sends the tweets directly from R to Twitter | |
# this function requires installing the rtweet package and setting up the relevant | |
# Twitter user credentials | |
# otherwise, the tweetstorm outputs to the console; the user can copy/paste to Twitter manually | |
# | |
# From https://sites.tufts.edu/emotiononthebrain/2017/08/12/time-for-a-tweetstorm/ | |
tweetstorm <- function(s, tweetnow = FALSE) { | |
#if you'll be tweeting directly from R, update the user name and token variables |