Skip to content

Instantly share code, notes, and snippets.

View arcaravaggi's full-sized avatar

Anthony Caravaggi arcaravaggi

View GitHub Profile
@arcaravaggi
arcaravaggi / euDist.R
Last active March 14, 2018 15:08
Calculate Euclidean distance between groups of ecological point data
# 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)
@arcaravaggi
arcaravaggi / presabRas.R
Last active February 14, 2018 17:21
Create species presence/absence raster
# 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)
@arcaravaggi
arcaravaggi / power_analysis.R
Created February 14, 2018 17:14
Simple power analysis in R
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)
#
@arcaravaggi
arcaravaggi / maxDup_NAs.R
Created January 31, 2018 11:51
Split data by focal column and remove duplicates from a span to columns, keeping the one with the most data
# 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
@arcaravaggi
arcaravaggi / simpleCap.R
Last active November 13, 2017 11:48
Capitalise the first letter of each word in a string
# 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=" ")
@arcaravaggi
arcaravaggi / spLine.ld.R
Last active October 3, 2017 13:00
Intersects SpatialLine and SpatialPolygon objects and produces total line length and density per polygon ID.
# 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
@arcaravaggi
arcaravaggi / coefplot.R
Created September 25, 2017 14:26
Plot regression coefficients
# 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,
@arcaravaggi
arcaravaggi / modOverlap.R
Created September 21, 2017 12:59
Metrics for quantifying the similarity among ecological niche models
# 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)
@arcaravaggi
arcaravaggi / cor.test.R
Created September 14, 2017 15:42
Function for pairwise correlations across a data frame
# 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))
@arcaravaggi
arcaravaggi / tweetstorm.R
Created August 31, 2017 11:30
Create a series of tweets from one text string.
# 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