Skip to content

Instantly share code, notes, and snippets.

@sckott
sckott / PGLMM_exampleoutput.R
Created October 13, 2011 15:16
Example of PGLMM and its output
> # Simulate a dataset for the example
> modelflag <- 1
> sim.dat <- PGLMM.sim(stree(16, "balanced"), nsites = 30, modelflag = modelflag,
+ second.env = TRUE, compscale = 1)
> sim.dat$Vphylo # I think this is the variance-covariance matrix from the phylogeny
t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16
t1 1.00 0.75 0.50 0.50 0.25 0.25 0.25 0.25 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
t2 0.75 1.00 0.50 0.50 0.25 0.25 0.25 0.25 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
t3 0.50 0.50 1.00 0.75 0.25 0.25 0.25 0.25 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
@sckott
sckott / newfoodwb.R
Created October 14, 2011 16:11
Playing with the Otago Harbor food web dataset.
# Blog post about a new food web
> require(ggplot2); require(igraph); require(bipartite); require(NetIndices)
# read in edge list
> mygraph <- read.graph("/Mac/R_stuff/Blog_etc/newfoodweb/edgelist_new.txt",
+ format = "edgelist")
> str(mygraph)
List of 9
$ : num 181
@sckott
sckott / simdeepbranch.R
Created October 14, 2011 22:05
Simulate a tree with an outgroup on a long branch.
# Code from Liam Revell, and put up here by Scott Chamberlain
require(phytools); require(geiger) # load packages
ntaxa1<-20; ntaxa2<-2 # number of taxa in each clade
total.height<-10 # total tree height (may need to be adjusted)
# simulate subtree 1
tr1<-birthdeath.tree(b=1,d=0,taxa.stop=ntaxa1)
tr1$tip.label<-1:ntaxa1; tr1$root.edge<-0
@sckott
sckott / taxize_rgbif_eg.R
Created November 1, 2011 00:23
Example of using taxize and rgbif packages together.
#######################################################
# by Scott Chamberlain <myrmecocystus@gmail.com>
# for blog post titled "Awesome use case: check my species names and
# gimme some distribution data"
#######################################################
# Load packages
install.packages(c("RCurl","stringr","XML","plyr","RJSONIO"))
require(RCurl);require(stringr);require(XML);require(plyr);require(RJSONIO)
# Clone taxize and rgbif repositories from GitHub
@sckott
sckott / iworkfortheinternet.R
Created December 13, 2011 15:49
Code for searching Twitter using the twitteR #rstats package.
require(plyr); require(stringr); require(ggplot2); require(lubridate); require(twitteR)
datout_1 <- searchTwitter("I work for the internet", n = 1500, since='2011-11-11', until='2011-12-12')
datout_2 <- searchTwitter("I work for the internet", n = 1500, since='2011-11-13', until='2011-12-14')
datoutdf <- ldply(c(datout_1, datout_2), function(x) x$toDataFrame(), .progress="text")
actual <- grep("I work for the internet", datoutdf[,1], ignore.case=T)
datoutdf2 <- datoutdf[actual,]
datoutdf2$newtime <- round_date(datoutdf2[,4], "hour")
@sckott
sckott / piechart.R
Created December 24, 2011 00:11
Simple ggplot2 pie chart function.
piechart <- function(set) {
ggplot(dat[dat$type==set,], aes(x = "", y = pageviews, fill = name)) +
theme_bw() +
geom_bar(width = 1) +
coord_polar(theta = "y") +
labs(y="", x='') +
opts(panel.border = theme_rect(colour = 'white'),
axis.text.x = theme_text(colour='white'))
}
@sckott
sckott / mammaldataset.R
Created December 29, 2011 16:04
Looking at the Weecology mammal dataset
# setwd("/Mac/R_stuff/Blog_etc/Mammal_Dataset")
# URLs for datasets
comm <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_communities.csv"
refs <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_references.csv"
sites <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_sites.csv"
spp <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_species.csv"
trap <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_trapping.csv"
# read them
@sckott
sckott / goodriddance.R
Created January 8, 2012 13:28
Good riddance to excep pivot tables example with plyr.
library(reshape2)
library(ggplot2)
dataset <- data.frame(var1 = rep(c("a","b","c","d","e","f"), each = 4),
var2 = rep(c("level1","level1","level2","level2"), 6),
var3 = rep(c("h","m"), 12), meas = rep(1:12))
Created by Pretty R at inside-R.org
# simply pivot table
dcast(dataset, var1 ~ var2 + var3)
@sckott
sckott / loghistplot.R
Created January 10, 2012 13:41
Plot logistic regression with bar plots for distribution of 0 and 1 values on top and bottom x-axes.
# Define the function
loghistplot <- function(data) {
require(ggplot2); require(gridExtra) # load packages
names(data) <- c('x','y') # rename columns
# get min and max axis values
min_x <- min(data$x)
max_x <- max(data$x)
@sckott
sckott / treeresstats.R
Created January 13, 2012 17:02
Calculate phylogeny resolution statistics on a phylo object.
# calculate tree resolution stats
treeresstats <- function(x) {
require(phangorn) # load the phangorn package
todo <- ( 1+Ntip(x)) : (Ntip(x) + Nnode(x) )
trsize_tips <- Ntip(x)
trsize_nodes <- Nnode(x)
polytomyvec <- sapply(todo, function(y) length(Children(x, y)))
numpolys <- length(polytomyvec[polytomyvec > 2])
numpolysbytrsize_tips <- numpolys/trsize_tips
numpolysbytrsize_nodes <- numpolys/trsize_nodes