Skip to content

Instantly share code, notes, and snippets.

@johnjosephhorton
johnjosephhorton / make_table.R
Created September 24, 2012 17:23
Create a new table in the database with R
df.odw <- data.frame(application = df$application, p = df$p)
if(dbExistsTable(con, "predicted_response_probabilities")){
dbRemoveTable(con, "predicted_response_probabilities")
}
dbWriteTable(con, "predicted_response_probabilities", df.odw)

Papers

Published & Forthcoming Papers in Economics

"The Online Laboratory: Conducting Experiments in a Real Labor Market" (with David Rand and Richard Zeckhauser) Experimental Economics, 14:3 (2011), 399-425.

@johnjosephhorton
johnjosephhorton / .emacs
Created August 21, 2012 17:39
John Horton's .emacs file
(add-to-list 'load-path "~/.emacs.d/")
(add-to-list 'load-path "/usr/share/emacs/site-lisp/ess")
;; LaTeX stuff
(load "auctex.el" nil t t)
(load "preview-latex.el" nil t t)
;; browse the kill ring
(load "browse-kill-ring.el" nil t t)
@johnjosephhorton
johnjosephhorton / memisc_update_for_lme4.R
Created August 5, 2012 16:37
Change to memisc to support lme4 models
## ----------------------------------------------------------------------------
## Author: Jason Morgan (borrowing heavily from code contained in Martin Elff's
## memisc package).
##
## Notes: Additional methods for mtable formatting of lme4 model
## objects. Requires that the memisc package be loaded prior to
## sourcing these functions.
## ----------------------------------------------------------------------------
setSummaryTemplate(mer = c("Log-likelihood" = "($logLik:f#)",
library(ggplot2)
library(RPostgreSQL)
library(plyr)
library(scales) # now needed for ggplot2
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, dbname='db',
host = 'localhost',
user = 'db',
password ='pwd',
Coefficients:
Estimate Std. Error t value
(Intercept) -0.070957 0.022836 -3.107
log(job_desc_length) 0.032188 0.003320 9.695
grouptreatment 0.042197 0.008536 4.943
factor(level1)Business Services 0.023747 0.027189 0.873
factor(level1)Customer Service -0.010701 0.030912 -0.346
factor(level1)Design & Multimedia 0.077688 0.017592 4.416
factor(level1)Networking & Information Systems 0.048057 0.030840 1.558
factor(level1)Sales & Marketing 0.119671 0.017374 6.888
@johnjosephhorton
johnjosephhorton / missingness.R
Created June 15, 2012 00:34
Dealing with missing values in R
n <- 100
x <- runif(n)
y <- 5*x
missing <- runif(n) > .80
df <- data.frame(y = y, x = x, missing = missing)
df$x[missing] <- mean(df$x[!missing])
df$y[!df$missing] <- df$y[!df$missing] + .80
m <- lm(y ~ x + missing, data = df)
summary(m)
@johnjosephhorton
johnjosephhorton / hashfree.py
Created May 15, 2012 00:28
Makes sure there are no unsaved files
def hashfree(input_dir):
"""Makes sure---before we start doing lots of intense computations---that
there are not any files w/ hash in front of them (which the shutil utility
cannot copy for some reason) """
no_bad_files = True
bad_files = []
for root, subFolders, files in os.walk(input_dir):
for f in files:
if re.search('\.#.*', f):
no_bad_files = False
@johnjosephhorton
johnjosephhorton / db.sh
Created April 13, 2012 18:20
Bash script to move a file to your public dropbox folder and copy the url to the clip board
#!/bin/bash
cp $1 ~/Dropbox/Public/
echo "http://dl.dropbox.com/u/<your id>/"$1 | xclip -i -selection clipboard
@johnjosephhorton
johnjosephhorton / sql_to_R.R
Created March 7, 2012 02:20
Code snipped for turning Postgresql stdout of a table into an R data frame
get.sql.out <- function(file, nrows = -1){
raw <- read.table(file, header=TRUE, nrow = nrows, sep="|", fill=TRUE, strip.white=TRUE)
num.rows <- dim(raw)[1]
raw$index <- 1:num.rows
raw <- subset(raw, index != 1 & index != num.rows)
raw
}