Skip to content

Instantly share code, notes, and snippets.

View willpearse's full-sized avatar

Will Pearse willpearse

  • Imperial College London
  • Silwood Park, UK
  • X @willpearse
View GitHub Profile
@willpearse
willpearse / toy-mcmc.R
Created October 24, 2021 13:43
Toy MCMC
data <- c(120, 130, 110, 105, 121)
n.iter <- 10000
mu.prior <- function(x) dnorm(x, mean=0, sd=10)
sd.prior <- function(x) dnorm(x, mean=0, sd=10)
posterior <- function(x, mu, sigma) sum(c(
dnorm(x, mu, abs(sigma), log=TRUE),
mu.prior(x), sd.prior(x)
))
proposal <- function(x) x + rnorm(1, 0, sd=.25)
mu <- 90
@willpearse
willpearse / pareto-bm-noodling.R
Last active November 5, 2021 17:55
Distributions of body mass noodling for sDevTraits
# Rough-and-ready body-mass/species-number estimation in mammals and birds
# Will Pearse - 2021-09-19
##########################
# Load data ##############
##########################
library(caper)
library(ape)
library(Pareto)
@willpearse
willpearse / MADcomm-triage.R
Last active February 18, 2020 03:47
MADcomm broken (fixable?) functions
#' @export
.reed.2017a <- function(...) {
data <-read.csv("http://pasta.lternet.edu/package/data/eml/knb-lter-sbc/17/30/a7899f2e57ea29a240be2c00cce7a0d4", as.is=TRUE)
names(data) <- tolower(names(data))
data$count[data$count < 0] <- 0
data$taxon_species[data$taxon_species == -99999] <- NA
data$taxon_genus[data$taxon_genus == -99999] <- NA
data$species <- with(data, paste(taxon_genus, taxon_species, sep="_"))
@willpearse
willpearse / macroevol_classic_intro.R
Created June 20, 2019 19:29
A brief overview of classic macro-evolutionary methods
# Packages
library(geiger)
library(caper)
library(phytools)
# Simulate phylogeny and some traits
tree <- sim.bdtree(n=100)
traits <- sim.char(tree, .01, nsim=7)[,1,]
# Fuss around to make a discrete character
@willpearse
willpearse / csv_to_gpx.rb
Created June 17, 2019 19:57
Make Garmin Waypoints (GPX files) from CSVs of site locations
#!/usr/bin/ruby
require 'optparse'
require 'csv'
options = {}
OptionParser.new do |opts|
#Defaults
options[:output] = nil
options[:input] = nil
options[:lat] = "lat"
@willpearse
willpearse / comparative-intro.R
Created April 13, 2018 19:50
A hasty and incomplete intro to comparative methods
# PICS and PGLS - oh my!
# Will Pearse - 2018-04-03
# Headers
library(ape)
library(caper)
library(geiger)
library(phytools)
library(mvMORPH)
@willpearse
willpearse / playing-with-pglmm.r
Created October 30, 2017 14:12
Simple demos of PGLMM with traits and environment. YMMV...
# Headers
library(pez)
data(laja)
#############################
# SETUP #####################
#############################
# Make a comparative community object from a phylogeny and some
# site-species community data - some invertebrates found in some
@willpearse
willpearse / bind.replace.R
Created June 29, 2017 21:51
A general way to bind a phylogeny to replace a given species on another phylogeny
# Functions
library(caper)
find.clade <- function(tree, tips){
clade.mat <- clade.matrix(tree)$clade.matrix
sums <- rowSums(clade.mat)
ancestors <- apply(clade.mat, 1, function(x) all(x[tips]==1))
sums[!ancestors] <- length(tree$tip.label)
return(unname(which.min(sums)))
}
bind.replace <- function(backbone, donor, replacing.tip.label){
@willpearse
willpearse / trees_for_managers.R
Created March 5, 2017 00:10
Calculating PD for managers
#Loading a package
library(pez)
#Building a tree
tree <- read.tree("Vascular_Plants_rooted.dated.tre") #file from http://datadryad.org/resource/doi:10.5061/dryad.63q27
species <- c("Quercus_notreal","Pinus_notreal","Blasia_notreal","Riccia_notreal")
tree <- congeneric.merge(tree, species)
tree <- drop.tip(tree, setdiff(tree$tip.label, species))
#Calculating PD
@willpearse
willpearse / article_cite.rb
Created February 15, 2016 18:50
Scraping all citing files from an article on Google Scholar
#!/usr/bin/ruby
require 'optparse'
require 'json'
require 'open-uri'
require 'nokogiri'
############################
# ARGUMENT PARSING #########
############################
options = {}