Skip to content

Instantly share code, notes, and snippets.

@jsonbecker
jsonbecker / link_list_templates.html
Last active December 28, 2015 17:59
How to modify your `articles.html` and `index.html` template for "Linked List" style posts.
@jsonbecker
jsonbecker / cutoff_matrix.R
Created October 17, 2013 19:17
Quick function to calculate common statistics when evaluating a predictor of binary outcomes.
cutoff_matrix <- function(df, classifier, outcome, breaks=seq(1,0,-.01)){
results <- data.frame(cutoff=vector(mode='numeric', length=length(breaks)),
true_pos=vector(mode='numeric', length=length(breaks)),
true_neg=vector(mode='numeric', length=length(breaks)),
false_pos=vector(mode='numeric', length=length(breaks)),
false_neg=vector(mode='numeric', length=length(breaks)))
for(i in seq(1, length(breaks))){
value <- breaks[i]
j <- data.frame(table(df[[classifier]]>value, df[[outcome]]))
results[i,1] <- value
@jsonbecker
jsonbecker / modalSDPdt.R
Created January 31, 2013 01:46
SDP business rules to resolve student attributes.
modal_person_attribute <- function(df, attribute){
# df: rbind of all person tables from all years
# attribute: vector name to calculate the modal value
# Calculate the number of instances an attributed is associated with an id
dt <- data.table(df)
mode <- dt[, rle(as.character(.SD[[attribute]])), by=sasid]
setnames(mode, c('sasid', 'counts', as.character(attribute)))
setkeyv(mode, c('sasid', 'counts'))
# Only include attributes with the maximum values. This is equivalent to the
# mode with two records when there is a tie.
@jsonbecker
jsonbecker / arghslow.R
Created January 29, 2013 19:24
If this doesn't motivate you to learn data.table...
modal_person_attribute <- function(df, attribute){
# df: rbind of all person tables from all years
# attribute: vector name to calculate the modal value
# Calculate the number of instances an attributed is associated with an id
mode <- do.call(rbind,
tapply(as.character(df[[attribute]]), df$sasid,
function(x) data.frame(attribute=rle(x)$values,
counts=rle(x)$lengths)))
names(mode) <- c(as.character(attribute), 'counts')
# Clean up
@jsonbecker
jsonbecker / WPFootnotesToMarkdown.py
Last active December 9, 2015 23:08
WP-Footnotes to Markdown footnotes.
from sys import argv
import re
name, file_path = argv
p = re.compile(r"[\s]\(\((.*?[)]{0,1})\)\)[\s]{0,1}")
# The tricky part here is to match all text between (()), including as many as
# one set of (), which may even terminate ))). The {0,1} captures as many as
# one ). The trailing space is there because I often surrounded the (()) with
# a space to make it clear in the Wordpress editor.