Skip to content

Instantly share code, notes, and snippets.

@sckott
sckott / phylometa_rand_fxn_one.R
Created April 18, 2011 13:33
Conduct tip shuffle randomization of phylogenetic meta-analysis
#####################################################################
# Created by Scott Chamberlain
# Ecology and Evolutionary Biology Dept., Rice University
# Houston, TX 77005, USA
# myrmecocystus@gmail.com
# http://schamber.wordpress.com/
#####################################################################
##### Directions
#Put your phylogeny, in format required by phylometa (see Phylometa manual), in your working directory
@sckott
sckott / CalcDists.R
Created April 20, 2011 14:18
get a matrix of pairwise geographic distances between locations
# Convert degrees to radians
deg2rad <- function(deg) return(deg*pi/180)
# Calculates the geodesic distance between two points specified by
# radian latitude/longitude using the Haversine formula
# Ouputs distance between sites 1 and 2 as meters
gcd.hf <- function(long1, lat1, long2, lat2) {
R <- 6371 # Earth mean radius [km]
delta.long <- (long2 - long1)
delta.lat <- (lat2 - lat1)
@sckott
sckott / bladjing_twomethods.R
Created April 23, 2011 04:57
Conduct and compare two methods for transforming branch lengths using node ages.
# Run these functions first (scalePhylo, assign.ages, and assign.brlen)
# scalePhylo, assign.ages, and assign.brlen written by Gene Hunt (http://paleobiology.si.edu/staff/individuals/hunt.cfm)
# AdjBrLens written by Scott Chamberlain (myrmecocystus@gmail.com)
scalePhylo<- function(tr, tip.ages, node.mins=NULL, min.diff=0.1)
## tr is a 'phylo' object
## tip.ages is a vector of the ages of terminal taxa (best if this vector has names that match the taxa labels)
## tip.ages MUST BE A NAMED VECTOR, that is, e.g., names(tipages) <- tree$tip.label [where tipages is a numeric vector of ## the tip ages]
## node.mins is a vector of optional constraints on nodes
## min.diff is the minimum branch length that will be imposed
@sckott
sckott / phylometa_fxn.R
Created April 24, 2011 23:19
Conduct phylogenetic meta-analysis from R calling Phylometa - These are functions needed in the code in file phylometa_inR_run.R.
#####################################################################
# Created by Scott Chamberlain
# Ecology and Evolutionary Biology Dept., Rice University
# Houston, TX 77005, USA
# myrmecocystus@gmail.com
#####################################################################
# Function to split confidence intervals, this function is required in the below functions
CI_split <- function(a){
dd<-c(gsub("\\(","",unlist(strsplit(a[5],","))[1]),gsub("\\)","",unlist(strsplit(a[5],","))[2]))
@sckott
sckott / phylometa_inR_run.R
Created April 24, 2011 23:20
Conduct phylogenetic meta-analysis from R calling Phylometa.
#####################################################################
# Created by Scott Chamberlain
# Ecology and Evolutionary Biology Dept., Rice University
# Houston, TX 77005, USA
# myrmecocystus@gmail.com
# http://schamber.wordpress.com/
#####################################################################
########Directions
#Put your phylogeny, in format required by phylometa (see Phylometa manual), in your working directory
@sckott
sckott / phylip_fromR.R
Created April 26, 2011 12:32
Run the Phylip application 'contrast' from R, and get formatted output.
##############################################
# Created by Scott Chamberlain
# Ecology and Evolutionary Biology Dept., Rice University
# Houston, TX 77005, USA
# myrmecocystus@gmail.com
##############################################
#### Load packages
require(stringr); require(ape); require(plyr); require(reshape2)
#### Set directory
@sckott
sckott / GetCorrs.R
Created May 12, 2011 12:26
Function for calculating pairwise correlations between b*c columns of data in a data frame
# a = data frame
# b = list of variables in x
# c = list of variables in y
# method_ = one of pearson, spearman, kendall (or their abbreviations)
# output is a data frame with as many columns as b * c
GetCorrs <- function(a, b, c, method_) {
dfout_ <- list()
names <- list()
names_ <- list()
@sckott
sckott / idata_frame.R
Created May 13, 2011 19:46
Comparison of operations on normal and immutable (see package plyr) data frames
# load packages
require(plyr); require(reshape2)
# Make immutable data frame
baseball_i <- idata.frame(baseball)
# Example 1 - idata.frame more than twice as fast
system.time( replicate(50, ddply( baseball, "year", summarise, mean(rbi))) )
system.time( replicate(50, ddply( baseball_i, "year", summarise, mean(rbi))) )
@sckott
sckott / ggtree_v1.R
Created May 17, 2011 19:34
Simple function for plotting phylogenies in ggplot2
###### Simple function for plotting phylogenies in ggplot2
# x = a phylo object
# form = one of: star or ladder
# dependencies: ape, ggplot2, adephylo (loaded within function)
ggtree <- function(x, form) {
# Load packages
require(ape); require(ggplot2); require(adephylo)
# Define plotting format
phytheme_ <- function()
@sckott
sckott / phylogeneticsignal_sim.R
Created May 18, 2011 04:37
Compare measures of phylogenetic signal on simulated trees
#### Simulations
install.packages(c("ape","reshape2","ggplot2"))
require(ape); require(reshape2); require(ggplot2)
source("http://anolis.oeb.harvard.edu/~liam/R-phylogenetics/phylosig/v0.3/phylosig.R")
source("http://anolis.oeb.harvard.edu/~liam/R-phylogenetics/fastBM/v0.4/fastBM.R")
# Simulation function physig_sim
# input: x = number of species in tree
# output: a vector length two with (K, lamba)
physig_sim <- function(x) {