Skip to content

Instantly share code, notes, and snippets.

@fototo
fototo / bayes_update.R
Created August 17, 2012 13:00 — forked from cjbayesian/bayes_update.R
generate a video demonstrating Bayesian updating
## Corey Chivers, 2012 ##
sim_bayes<-function(p=0.5,N=100,y_lim=20,a_a=2,a_b=10,b_a=8,b_b=3)
{
## Simulate outcomes in advance
outcomes<-sample(1:0,N,prob=c(p,1-p),replace=TRUE)
success<-cumsum(outcomes)
for(frame in 1:N)
{
png(paste("plots/",1000+frame,".png",sep=""))
@fototo
fototo / horizonplot from plot-xts.r
Created August 18, 2012 05:33 — forked from timelyportfolio/horizonplot from plot-xts.r
plot.xts can create horizon charts
#plot.xts with horizons
require(PerformanceAnalytics)
require(quantmod)
horizon.panel <- function(index,x,...) {
#get some decent colors from RColorBrewer
#we will use colors on the edges so 2:4 for red and 7:9 for blue
require(RColorBrewer)
col.brew <- brewer.pal(name="RdBu",n=10)
#ease this reference later
require(quantmod)
require(PerformanceAnalytics)
require(xtsExtra)
require(RColorBrewer)
#unfortunately don't feel like fighting IP lawyers so I cannot share this index data
portfolio <- read.csv("C:\\Users\\Kent.TLEAVELL_NT\\Documents\\old\\R\\lbustruu with tbill.csv",stringsAsFactors=FALSE)
portfolio <- portfolio[2:NROW(portfolio),2:NCOL(portfolio)]
portfolio <- portfolio[,c(1,3,5)]
@fototo
fototo / distance_matrix.R
Created October 7, 2012 05:35 — forked from dsparks/distance_matrix.R
Calculating distances (including between matrices)
# Cross-matrix distances and different measurement options, with "proxy"
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("proxy", "MASS", "Zelig")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Invent two data frames
voterIdealPoints <- data.frame(matrix(rnorm(26*2), ncol = 2))
rownames(voterIdealPoints) <- letters
@fototo
fototo / Heatmap.R
Created October 12, 2012 06:08 — forked from dsparks/Heatmap.R
ggplot2 heatmap with "spectral" palette
# Simple ggplot2 heatmap
# with colorBrewer "spectral" palette
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ggplot2", "reshape2", "RColorBrewer")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Generate a random matrix
# This can be any type of numeric matrix,
@fototo
fototo / cairographics.R
Created October 14, 2012 10:33 — forked from dsparks/cairographics.R
Using cairographics with ggsave()
# .png with Windows GDI versus .png with cairographics
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ggplot2", "RColorBrewer", "Cairo")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Generate some data
nn <- 100
myData <- data.frame(X = rnorm(nn),
@fototo
fototo / gale_shapley.R
Created October 16, 2012 15:28 — forked from dsparks/gale_shapley.R
Gale-Shapley Algorithm
# Gale-Shapley matching
# From http://plausibel.blogspot.com/2012/01/illustrating-deferred-acceptance.html
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("devtools", "animation")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Source the daa() function:
source_gist("1628636")
loadfrench <- function(zipfile, txtfile, skip, nrows) {
require(xts)
#my.url will be the location of the zip file with the data
my.url=paste("http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/",zipfile,".zip",sep="")
#this will be the temp file set up for the zip file
my.tempfile<-paste(tempdir(),"\\frenchzip.zip",sep="")
#my.usefile is the name of the txt file with the data
my.usefile<-paste(tempdir(),"\\",txtfile,".txt",sep="")
download.file(my.url, my.tempfile, method="auto",
@fototo
fototo / raster_scatter.R
Created October 17, 2012 06:52 — forked from dsparks/raster_scatter.R
Drawing a scatter plot of raster images
# Drawing a scatter plot of raster images
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("png", "devtools", "MASS", "RCurl")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Some helper functions, lineFinder and makeTable
source_gist("818983")
source_gist("818986")
@fototo
fototo / Orange_R_color_theme.xml
Created November 2, 2012 02:43 — forked from baogorek/Orange_R_color_theme.xml
Orange_R_color_theme
<?xml version="1.0" encoding="utf-8" ?>
- <colorTheme id="1234" name="new" modified="2011-02-01 01:56:53" author="Bob Forrest" website="anythingbutrbitrary.blogspot.com">
<occurrenceIndication color="#616161" />
<findScope color="#191919" />
<deletionIndication color="#FF0000" />
<singleLineComment color="#D6D6D6" />
<multiLineComment color="#666666" />
<commentTaskTag color="#666666" />
<javadoc color="#666666" />
<javadocLink color="#666666" />