Skip to content

Instantly share code, notes, and snippets.

View kleinschmidt's full-sized avatar

Dave Kleinschmidt kleinschmidt

View GitHub Profile
@kleinschmidt
kleinschmidt / histogram_woes.ipynb
Created August 3, 2014 01:58
Problems with Geom.histogram in Gadfly
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kleinschmidt
kleinschmidt / qqplot.jl
Last active August 29, 2015 14:05 — forked from johnmyleswhite/qqplot.jl
QQ plots in Julia with Gadfly (based on Vega example)
using Stats, Distributions, Gadfly
import Gadfly.ElementOrFunction
# First add a method to the basic Gadfly.plot function for QQPair types (generated by Distributions.qqbuild())
Gadfly.plot(qq::QQPair, elements::ElementOrFunction...) = Gadfly.plot(x=qq.qx, y=qq.qy, Geom.point, Theme(highlight_width=0px), elements...)
# Now some shorthand functions
qqplot(x, y, elements::ElementOrFunction...) = Gadfly.plot(qqbuild(x, y), elements...)
qqnorm(x, elements::ElementOrFunction...) = qqplot(Normal(), x, Guide.xlabel("Theoretical Normal quantiles"), Guide.ylabel("Observed quantiles"), elements...)
@kleinschmidt
kleinschmidt / aws_creds.json
Last active August 29, 2015 14:08
Tiny demo of using Amazon SQS to listen to notifications from Mechanical Turk. Run `npm install` to get dependencies.
{
"accessKey": "your access key",
"secretKey": "your secret key"
}
@kleinschmidt
kleinschmidt / input.R
Created January 28, 2015 14:38
converting factor to character after rowwise()
library(dplyr)
data.frame(a=factor(c('10', '20', '30', '40'))) %>%
mutate(b=as.character(a)) %>%
rowwise() %>%
mutate(c=as.character(a)) %>%
str
@kleinschmidt
kleinschmidt / pre-push
Last active August 29, 2015 14:15
A pre-push hook to automatically publish to github pages every time you push.
#!/bin/bash
# drop this in .git/hooks/, and chmod +x .git/hooks/pre-push
# store current branch
cur_branch=$(git rev-parse --abbrev-ref HEAD)
if [[ $cur_branch == "gh-pages" ]]
then
# bail out if we're trying to push the gh-pages branch (to prevent infinite loop)
exit 0
else
@kleinschmidt
kleinschmidt / collect_keyboard_resp.js
Created August 15, 2012 16:12
JS: Collect a one-off keyboard response, using JQuery namespaces to allow multiple, overlapping responses
/* collect a one-off keyboard response, using JQuery namespaces to allow multiple, overlapping responses
* fcn(e): function to be called with event on key press
* keys: vector of allowed keys (as characters, uppercase for alphanumeric) (optional)
* to: timeout for response, in ms (optinal)
* tofcn: function to be called on timeout (optional)
*/
function collect_keyboard_resp(fcn, keys, to, tofcn) {
// create unique namespace for this response
var namespace = '._resp' + (new Date()).getTime();
@kleinschmidt
kleinschmidt / errorbars-se-ddply.R
Created March 2, 2013 00:36
How to compute SE of an observed variable and plot errorbars in R using plyr and ggplot.
require(plyr)
require(ggplot2)
# say dat is a data.frame with the predictor x, grouping variables z1, z2, and z3 (factors or discrete numeric values)
# and the observed values are in the variable y. then I would do:
dat.summ <- ddply(dat, .(x, z1, z2, z3), summarise,
y.mean=mean(y), y.se=sd(y)/sqrt(length(y)))
# this is equivalent to
@kleinschmidt
kleinschmidt / knit.sh
Created April 17, 2013 19:55
Little shell script to streamline using knitr from the command line, or in case you want to use it with something like emacs which can call a shell script to compile a document.
#!/bin/bash
# knit.sh -- Dave Kleinschmidt, April 2013
# streamline knitting of Rnw files from the command line.
usage="Usage: $0 input-filename.Rnw [-nolatex] [-notangle]"
if [ $# -lt 1 ]; then
echo $usage
exit 1
fi
@kleinschmidt
kleinschmidt / mod_to_table.R
Created January 13, 2014 17:00
Code to turn lmer models into a nice latex table. Specify model object or coefficients. Based on functions by Judith Degen (modified by Florian Jaeger): http://hlplab.wordpress.com/2010/06/15/r-code-for-latex-tables-of-lmer-model-effects/
mod.to.table <- function(mod.all=NA, prednames=NA, pred_name_subs=NA, file="",
coefs=as.data.frame(summary(mod.all)@coefs),
...) {
if (is.na(prednames)) {
if (is.na(pred_name_subs)) {
prednames <- row.names(coefs)
} else {
prednames <- str_replace_multi(row.names(coefs), pred_name_subs, replace.all=T)
}
}
@kleinschmidt
kleinschmidt / concat_results_unique_assignments.sh
Last active January 4, 2016 03:19
Bash script which combines multiple (possibly redundant) .results files returned by Amazon Mechanical Turk's command line tools.
#!/bin/bash
# Author: Dave Kleinschmidt
#
#!/bin/bash
# Author: Dave Kleinschmidt
#
# concatenate all *.results files in the directory, printing one line
# per unique assignment id (first encountered). NOTE: the header isn't