Skip to content

Instantly share code, notes, and snippets.

View derekpowell's full-sized avatar

Derek Powell derekpowell

View GitHub Profile
@derekpowell
derekpowell / gdocs
Created March 10, 2017 21:57
google docs - accept all changes
javascript:(function(){ var d=document.getElementsByClassName("docos-accept-suggestion"); d = Array.prototype.slice.call(d); var i=1; d.forEach(function(n){console.log("accepting"); var e = document.createEvent("MouseEvents"); e.initEvent("click", true, false); n.dispatchEvent(e,true); e = document.createEvent("MouseEvents"); e.initEvent("mousedown", true, false); n.dispatchEvent(e,true); e = document.createEvent("MouseEvents"); e.initEvent("mouseup", true, false); i++; i++; setTimeout( function(){ n.dispatchEvent(e,true); } , (i * 1000)); }); })();
@derekpowell
derekpowell / standard_error.R
Created June 30, 2017 01:04
R: standard error functions for use in ggplot2
# standard error function
stderr <- function(x) {
sqrt(var(x[!is.na(x)]) / length(x[!is.na(x)]))
}
# Then create a wrapper to *stderr* to make it compatible with *stat_summary*
my.stderr <- function(x) {
meany <- mean(x)
ymin <- mean(x) - stderr(x)
ymax <- mean(x) + stderr(x)
@derekpowell
derekpowell / random_chars.R
Created July 12, 2017 17:41
Create random alphanumeric sequence in R
result <- rawToChar(as.raw(sample(c(48:57,65:90,97:122),16,replace=T)))
@derekpowell
derekpowell / get_ggdata.R
Created July 21, 2017 21:37
R: get data for elements of ggplot() object
dfPlot <- (ggplot_build(plt))$data
@derekpowell
derekpowell / stat_summary.py
Last active September 14, 2017 22:04
Python: stat_summary - function for statistical summary printout
def stat_summary(x, quantiles=False):
import numpy as np
print "mean:", np.mean(x)
print "sd:", np.std(x)
print "max:", np.max(x)
print "min:", np.min(x)
print "count:", len(x)
@derekpowell
derekpowell / crossValidate.R
Last active January 12, 2018 01:20
Functions for cross validation in R using modelr package
# author: Derek Powell
# created: 11-02-2017
# last updated: 1/11/18, 5:19 PM
# Also available as gist:
# devtools::source_gist("8838d867daa4185c9c09187a6b02f96b", filename="crossValidate.R")
library(tidyverse)
library(multidplyr)
library(modelr)
library(purrr)
@derekpowell
derekpowell / brms_model.R
Created March 24, 2018 06:40
boilerplate to create brms model
model_name <- brm(
DV ~ formula,
data = d,
family = normal(), # student(), #cumulative(), #bernoulli(), etc
control = list(adapt_delta = .80),
cores = parallel::detectCores(),
iter = 2000)
@derekpowell
derekpowell / rescale_beta.R
Last active March 24, 2018 06:45
rescale variable on open interval (0, 1)
rescale_beta <- function(x, lower, upper) {
# rescales onto the open interval (0,1)
# rescales over theoretical bounds of measurement, specified by "upper" and "lower"
N <- length(x)
res <- (x - lower) / (upper - lower)
res <- (res * (N - 1) + .5) / N
return(as.vector(res))
}
@derekpowell
derekpowell / gg_marginal_effects.R
Last active April 14, 2018 00:31
R: extract ggplot object from brm marginal_effects() plot
gg_marginal_effects <- function(model, effects, probs=c(0.25, 0.75)) {
me <- brms::marginal_effects(model, effects=effects, probs=probs)
plt.me <- plot(me,
effects=effects,
plot=FALSE,
rug=TRUE,
theme=ggplot2::theme_get()
)[[1]]
}
@derekpowell
derekpowell / redact.R
Last active August 30, 2018 23:50
Redact qualtrics workerId and IPAddress from data
# Author: Derek Powell
# Date: 8/30/18, 4:49 PM
# ---
# Script to redact workerIds and ip addresses from qualtrics files.
# Script looks for "date_private/" directory, saves resulting data in "data" directory.
# Personal info is replaced with a "hash" using xxhash64,
# a super fast hash algo w/ short resulting hashes (confirmed appropriate for this use)
suppressMessages(library(tidyverse))
suppressMessages(library(digest))