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
# Download data from here: https://taz.de/Proteste-gegen-Rechtsextremismus/!5988174/
library(tidyverse)
# first version with police data
fl <- "~/Downloads/data-Q5MHN.csv"
dd <- read_csv(fl) |>
mutate(Ort = trimws(Ort),
date = as.Date(Datum, format = "%d.%m.%Y"),
pcount = `Teilnehmende laut Polizei`,
library(ggplot2)
# the data
dd <- data.frame(x = c(0,3,6,9,10,14),
y = c(7,2,8,6,12,7))
# OLS model
mod <- lm(y ~ x, dd)
@conjugateprior
conjugateprior / coastline.R
Created June 4, 2020 11:41
Which UK counties have coastline?
library(sf)
library(rmapshaper)
# Grab shapefiles, here from http://www.diva-gis.org/gdata
shps2 <- read_sf("GBR_adm/GBR_adm2.shp")
shps2s <- rmapshaper::ms_simplify(shps2)
outline <- st_union(shps2s)
outline_flat <- st_transform(outline, crs = 27700)
counties <- st_transform(shps2s, crs = 27700)
outline_5km_in <- st_buffer(outline_flat, dist = -5000) # 5km inland
@conjugateprior
conjugateprior / MRP_predictions_age.R
Last active November 29, 2019 22:39
UK 2019 MRP predictions: party preference by age and likely turnout
# Data from https://yg-infographics-data.s3-eu-west-1.amazonaws.com/ZAfbtHgj42wx4reHnaMtbBamoKdMxkFMpz4gnWMjiZCUAxDX66MsCB38K/2019_data/MRP_Tables_2019_Election_Public_Release.pdf
library(tidyverse)
theme_set(theme_minimal()) # no mouldy waffle
age <- "Age Con Lab LD Brexit Green SNP PC Other Turnout Fraction
18-25 22 53 13 2 5 4 0 1 52 8
25-30 26 48 13 2 5 4 0 1 52 6
30-35 30 43 15 2 4 4 0 1 57 7
35-40 36 38 15 2 4 3 0 1 63 7
@conjugateprior
conjugateprior / pol245-syllabus.tex
Created January 14, 2019 21:43
The pol245 syllabus
% Syllabus example by Will Lowe, (wlowe@princeton.edu) 2018
% for the course POL 245 'Visualizing Data' (Part of the Freshman Scholars Institute)
%
% Compile it with xelatex (after you've inserted your own fonts)
\documentclass[11pt,letterpaper]{article}
\usepackage[margin=1in]{geometry}
\usepackage{marginnote}
\usepackage[dvipsnames]{xcolor}
@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...
@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) %>%
```{r}
library(MonetDBLite)
library(RSQLite)
library(DBI)
library(dplyr)
library(microbenchmark)
library(ggplot2)
```
@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
@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"