Skip to content

Instantly share code, notes, and snippets.

@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 / 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
}
@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