Skip to content

Instantly share code, notes, and snippets.

<html lang="en">
<title>Obot</title>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
@johnjosephhorton
johnjosephhorton / strat.R
Created February 8, 2015 12:44
showing_stratification_advantage
library(ggplot2)
# simulate experiments using stratification and randomization and report
# absolute difference in estimates from true treatment effect, tau.
simExp <- function(n, tau, beta = 3){
x <- sort(runif(n))
W.rand <- rbinom(n,1,0.5)
W.strat <- rep(c(0,1), n/2)
epsilon <- rnorm(n, 0, 0.5)
ts := $(shell /bin/date "+%Y-%m-%d---%H-%M-%S")
project.pdf: project.bib project.tex
pdflatex -interaction=nonstopmode project
bibtex project
pdflatex -interaction=nonstopmode project
pdflatex -interaction=nonstopmode project
backup:
tar --exclude='../backups' -zcvf ../backups/project_$(ts).tar.gz ../
@johnjosephhorton
johnjosephhorton / parseHTMLwithR.R
Created November 11, 2010 16:30
How to parse HTML tables using R
# from: http://learnr.wordpress.com/2010/01/21/ggplot2-crayola-crayon-colours/
library(XML)
library(ggplot2)
theurl <- "http://en.wikipedia.org/wiki/List_of_Crayola_crayon_colors"
html <- htmlParse(theurl)
sched <- readHTMLTable(html, stringsAsFactors = FALSE)
crayola <- readHTMLTable(html, stringsAsFactors = FALSE)[[2]]
crayola <- crayola[, c("Hex Code", "Issued", "Retired")]
names(crayola) <- c("colour", "issued", "retired")
@johnjosephhorton
johnjosephhorton / toJSONdf.R
Created November 12, 2010 16:28
R function for turning a data frame into JSON suitable for use in Protovis
entity.write <- function(x){
out = as.character(x)
out = paste("\"", x, "\"", sep="")
out
}
toJSONdf <- function(x){
str = "["
for(row in 1:(dim(x)[1])){
first_entry = TRUE
@johnjosephhorton
johnjosephhorton / mturk_distro.R
Created November 17, 2010 16:04
Code for plotting distribution of HITs/worker on MTurk
library(ggplot2)
library(reldist)
data <- read.csv("User_3144_workers.csv")
u = dim(data)[1]
png("mturk_distro.png")
qplot(Number.of.HITs.approved.or.rejected, data = data) + scale_x_log2() +
xlab("Number of HITs Approved or Rejected \n (John Horton's Account)") +
ylab("Count of MTurk Workers") +
@johnjosephhorton
johnjosephhorton / jsonp.html
Created November 19, 2010 17:07
JSONP Cats Example
<script src="http://code.jquery.com/jquery-1.4.4.js"></script> <script>
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
{
tags: "cat",
tagmode: "any",
format: "json"
},
function(data) {
$.each(data.items, function(i,item){
$("<img/>").attr("src", item.media.m).appendTo("#images");
@johnjosephhorton
johnjosephhorton / parsing_MTurk_datetime_in_R.R
Created December 1, 2010 21:53
This is the correct strptime to parse the datetime string that MTurk generates
accept_time <- sapply(results$AcceptTime,
function(t){as.numeric(as.POSIXct(as.character(t), "%a %b %d %H:%M:%S GMT %Y", tz="UTC"))}
)
@johnjosephhorton
johnjosephhorton / internal_HIT.html
Created December 3, 2010 04:26
HTML and JS for an internal HIT posted on MTurk
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"></script>
<p><style type="text/css">
/*
* jQuery UI CSS Framework 1.8.6
*
* Copyright 2010, AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* http://docs.jquery.com/UI/Theming/API