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 / nhl_minor_penalty_scrape
Last active August 29, 2015 14:20
Scraping player minor penalties from NHL
#Headers
require 'open-uri'
require 'nokogiri'
require 'set'
#Get all the penalty stats
results = []
(2011..2014).to_a.each do |year|
(0..30).to_a.map{|x| x * 40 + 1}.each do |player_count|
page = Nokogiri::HTML(open("http://espn.go.com/nhl/statistics/player/_/stat/minor-penalties/sort/minorPenalties/year/#{year}/seasontype/2/count/#{player_count}"))
@willpearse
willpearse / ml_reml_thunderdome
Last active September 1, 2015 23:27
Test of ML vs. REML Lambda estimation
#Headers
require(geiger)
require(phytools)
require(nlme)
#Wrappers
get.lam <- function(x) attr(x$apVar, "Pars")[1]
get.vars <- function(n, lam){
tree <- rcoal(n=n)
x <- fastBM(geiger::rescale(tree, 'lambda', lambda = lam),sig2=0.5) # Brownian motion
@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 = {}
@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 / 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 / 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 / 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 / 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 / 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 / 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="_"))