Skip to content

Instantly share code, notes, and snippets.

View conjugateprior's full-sized avatar
💁‍♀️
. o O °

Will Lowe conjugateprior

💁‍♀️
. o O °
View GitHub Profile

Keybase proof

I hereby claim:

  • I am conjugateprior on github.
  • I am conjugateprior (https://keybase.io/conjugateprior) on keybase.
  • I have a public key whose fingerprint is 5D5B 2B00 382C E647 8B68 87D0 0AC2 609A 0570 BA8A

To claim this, I am signing this object:

@conjugateprior
conjugateprior / mp-sex-appeal.R
Last active August 29, 2015 14:19
Scraping the barrel
library(magrittr)
library(stringr)
library(ggplot2)
library(rvest)
make_data <- function(sex=c("M", "F")){
sex <- match.arg(sex)
addr <- paste0('http://www.sexymp.co.uk/index.php?gender=', sex, '#vote')
gg <- read_html(addr, encoding="ISO-8859-1")
@conjugateprior
conjugateprior / out-of-excel-into-r-t-test1.Rmd
Last active April 5, 2016 19:15
Out of Excel and into R, for a t.test
```{r}
## make sure the two columns of data are next to each other and have column headers
## highlight and copy them, including their headers. If the headers are, e.g. 'treatment' and 'control'
## on a Mac
ff <- read.delim(pipe('pbpaste'))
## on Windows
ff <- read.delim("clipboard")
@conjugateprior
conjugateprior / messy-merge.R
Created August 29, 2017 19:48
Messy merge
library(dplyr)
library(stringdist) # for approximate matching with 'amatch'
DB1 <- data_frame(name1 = c("John Q. Smith", "Sajeet Rau",
"Federico K. Giordino"),
judge_variable = c(1, 2, 3))
DB2 <- data_frame(name2 = c("John Smith", "John Q. Smith",
"F. K. Giordino","Federico K. Giordino"),
case_variable = c(1, 1, 1, 2))
@conjugateprior
conjugateprior / get-latest-release-from-private-gh-repo.sh
Created October 9, 2017 20:41
A clunky bit of bash to download binaries for the latest release of a private Github repository
#!/usr/bin/env bash
read -p "GitHub username: " username
read -s -p "GitHub password: " password
echo
read -p "Github repository name: " repo
up="$username:$password"
ur="$username/$repo"
@conjugateprior
conjugateprior / fourfold_box.R
Last active July 10, 2018 17:21
Fourfold plot function but with boxes and an example in Pew report style
fourfold_box <- function(bl_br_tl_tr, # values for bottom left, top right, etc.
cols = rep(rgb(0.8, 0.8, 0.8), 4), # default to grey fill
labels = NULL, # use these instead of bl, br, tl, and tr values
label.cols = rep("black", 4), # value colors
quadrant.labels = NULL, # default: don't label quadrants
quadrant.label.col = NULL, # default: "black"
small.min = 5, # center label in box unless it's smaller than this
small.label.mult = 1.5, # label this multiple of box side length further out
axis.lwd = 1, # crossbar thickness
axis.col = NULL, # default: "black"
@conjugateprior
conjugateprior / predicted-probabilities-for-logistic-regression.R
Created July 24, 2018 22:30
Predicted probabilities for logistic regression models using R and ggplot2
# convenience function for logit models
invlogit <- function(x){ 1 / (1 + exp(-x)) }
# some data cribbed from the R help pages
dd <- data.frame(ldose = rep(0:5, 2),
dead = c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16),
sex = rep(c("M", "F"), each = 6))
# 30 trials in each row of which 'dead' beasties died
# fit a model
```{r}
library(MonetDBLite)
library(RSQLite)
library(DBI)
library(dplyr)
library(microbenchmark)
library(ggplot2)
```
@conjugateprior
conjugateprior / decreasers.R
Created November 13, 2018 17:48
Hours-reversing NC counties
library(tidyverse)
page <- "https://www.newsobserver.com/news/politics-government/election/article100235752.html"
decreasers <- read_html(page) %>% # grab and parse the webpage
html_table %>%
pluck(1) %>% # first (and only) table
mutate(h2016 = parse_number(`2016 Hours`), # parse ugly number formats
h2012 = parse_number(`2012 Hours`),
County = toupper(County)) %>%
filter((h2016 - h2012) < 0) %>%
@conjugateprior
conjugateprior / chargemaster.R
Last active January 9, 2019 17:51
A monster table of medical charges, parsed but not interpreted
# install.packages(c("jsonlite", "dplyr"))
library(jsonlite)
library(dplyr)
json <- "https://www.sutterhealth.org/for-patients/chargemaster-2019.json"
cmr <- read_json(json)
gg <- bind_rows(lapply(cmr$CDM, as.data.frame))
head(gg) # ok, now what...