Skip to content

Instantly share code, notes, and snippets.

@cboettig
cboettig / plottrees.R
Created June 1, 2011 17:16
plot lots of trees in R
# image trees are named something like paup1.tre to paup100.tre
require(ape)
# could use a for loop, but sapply is faster and will parallelize
sapply(1:100, function(i){
tree <- read.tree(paste("paup", i, ".tre", sep=""))
pdf(file=paste("paup", i, ".pdf", sep=""))
plot(tree) # add fancy plotting options etc,
dev.off()
})
## this function f will remember whatever options it is passed
f <- function(y, ...){
out <- y + dnorm(...)
list(out, opts=list(...))
}
## This is the way to call the function using its exact arguments
a <- f(1, x=4, sd=2, mean=4)
b <- do.call(f, c(list(y=1), a$opt))
identical(a,b) # TRUE
@cboettig
cboettig / example.R
Created June 10, 2011 19:17
Push R images to flickr with links to script code on github, and tweet the results and save the data using the flickr id.
##### socialR header info #####
require(socialR)
script <- "example.R" # Must specify the script name!
gitcommit(script) # Must commmit at start!
##### Optional but a good idea #####
#almost requisite, if not me running "repo"/"dir"
gitopts = list(user = "cboettig", dir = "demo", repo = "socialR")
on.exit(system("git push")) # For git links. May prompt for pw,
tags <- "test" ## multiple possible: space, delim, multiple items, etc.
@cboettig
cboettig / em.R
Created June 15, 2011 21:08
expectation-maximization algorithm
data(faithful)
attach(faithful)
## EM Algorithm
W = waiting
s = c(0.5, 40, 90, 16, 16)
em = function(W,s) {
Ep = s[1]*dnorm(W, s[2], sqrt(s[4]))/(s[1]*dnorm(W, s[2], sqrt(s[4])) +
@cboettig
cboettig / em2.R
Created June 15, 2011 21:10
abstract expectation maximization algorithm
data(faithful)
attach(faithful)
#Assumes two gaussian populations of eruptions
W = waiting
# Guess of the parameters:
s = c(p=0.5, mu1=50, mu2=90, sigma1=30, sigma2=30)
# EXPECTATION STEP
expectation_step <- function(observed, parameters){
@cboettig
cboettig / segue_test.R
Created July 1, 2011 17:22
Amazon cloud via segue
#segue_test.R
on.exit(stopCluster(myCluster))
library(segue)
setCredentials(getOption("amazon_key"), getOption("amazon_secret"))
# Jeffery Breen's example, adapted with "custom function" works fine
my_fun <- mean # a custom definition
myList <- NULL
@cboettig
cboettig / reviews.bib
Created July 4, 2011 23:49
Format for reviews in Bibtex
@ARTICLE{,
author = {L.H. Yang and V. H. Rudolf},
title = {Phenology, ontogeny and the effects of climate change on the timing
of species interactions},
journal = {Ecology Letters},
year = {2010},
volume = {13},
pages = {1-10},
doi = {10.1111/j.1461-0248.2009.01402.x},
@cboettig
cboettig / segue_custom.R
Created July 7, 2011 21:27
Segue with custom packages
library(segue)
setCredentials(getOption("amazon_key"), getOption("amazon_secret"))
require(mcmcTools)
X <- rnorm(20, 2, 5)
loglik <- function(pars){
sum( dnorm(X, mean=pars[1], sd=pars[2], log=TRUE) )
}
prior <- function(pars){
1/pars[2]^2
}
@cboettig
cboettig / plot_layout.R
Created July 26, 2011 19:45
R plot layouts
mat <- rbind(c(1),c(2),c(3)) # three rows, 1 column
layout(mat, height = c(1, .5, .5)) # height of each row
par(mar=c(4,6,4,2)) # can be changed for each subsequent plot
@cboettig
cboettig / greekplot.R
Created July 26, 2011 21:51
plot greek letters in multi-line R plot titles
## THANKS to David Scott for this one:
## http://r.789695.n4.nabble.com/Greek-letters-on-a-multi-line-plot-title-td1018642.html
### Change spacing to allow for multiline title
par(oma=c(2, 0, 4, 0))
### Some greek letters
mu <- 0
alpha <- 1
beta <- 2