Skip to content

Instantly share code, notes, and snippets.

@cboettig
cboettig / socialR.R
Created November 29, 2010 21:50
Enable simulations to share status on social networks
## Error reporting
tweetError <- function(){
system(paste('hpc-autotweets "@cboettig runtime error"', sep=""))
}
options(error = tweetError)
## Reporting a finished run
##social reporting
gitcom <- system('git commit -a -m "autocommit"', intern=TRUE)[[1]]
@cboettig
cboettig / pdf2png.sh
Created December 15, 2010 19:11
convert all pdf to png
#!/bin/bash
# Convert all pdf to png
for f in *.pdf
do
convert "$f" "${f%.pdf}.png"
done
@cboettig
cboettig / flickr-gallery
Created January 6, 2011 19:27
shortcodes for the flickr-gallery wordpress plugin to include photos by date and tag
@cboettig
cboettig / S3S4.R
Created January 10, 2011 18:49
Using S3 and S4 classes in R
setOldClass("myS3Class")
setGeneric("foo", function(x, ...) standardGeneric("foo"))
foo =
function(x, ...)
UseMethod("foo")
foo.myS3Class =
@cboettig
cboettig / KingMarkovIsland.R
Created January 26, 2011 23:57
Demonstration of Metropolis algorithm
##########################################################
# King Markov, island dictator example
# this is a simple metropolis algorithm
num.visits <- 20000
population <- 1:10
current.island <- sample( 1:10 , size=1 )
visits <- {}
par(mfrow=c(2,1))
for ( i in 1:num.visits ) {
visits[i] <- current.island
@cboettig
cboettig / bayesian_update_animate.R
Created January 27, 2011 00:02
bayesian_update_animate.R
##########################################################
# show updating process iteratively
# just paste all of this code into R to execute it
# generate data
n <- 20
y <- rnorm( n , mean=7 , sd=0.5 )
# assign prior and compute posterior as we add each y value to observations
prior.mu <- 3
k.sigma<-sd(y)
# @file: abc.R
# @author: Carl Boettiger <cboettig@gmail.com>
# @date: 11 April, 2011
# Description: A trivial example of Approximate Bayesian Computing
# We will simulate under a Gaussian process to determine
# The target data:
TrueMean <- 7 ## irrelevant note: "True" is rather a frequentist name for this
TrueSD <- 1
Npts <- 100
@cboettig
cboettig / mcmc.R
Created May 5, 2011 00:28
Markov Chain Monte Carlo
## The observed data.
X <- rnorm(1000, 2, 5)
loglik <- function(mu, sigma, X){
## The likelihood function
sum( dnorm(X, mean=mu, sd=sigma, log=TRUE) )
}
## initial starting point
pars <- c(mu=5, sigma=15)
@cboettig
cboettig / dryad_data.R
Created May 12, 2011 23:54
Download data from Dryad using the api
# id <- "10255/dryad.23"
download_data <- function(id, curl=getCurlHandle() ){
# Returns the url to data file
mets_metadata <- sprintf("%s/%s/%s", "http://datadryad.org/metadata/handle/", id, "/mets.xml")
tt <- getURLContent(mets_metadata, curl=curl)
page <- xmlParse(tt)
out <- xpathApply(page, "//mets:FLocat",
function(x){
link <- xmlAttrs(x, "xlink:href")
@cboettig
cboettig / mcmcmc.R
Created May 19, 2011 18:26
metropolis coupled markov chain monte carlo
beta <- function(i, Delta_T=1){
1/(1+Delta_T*(i-1))
}
step_fn <- function(pars, stepsizes = .02){
# Sequential random updating
j <- sample(1:length(pars), 1)
pars[j] <- rnorm(1, pars[j], stepsizes)
pars
}