Skip to content

Instantly share code, notes, and snippets.

@Torvaney
Torvaney / rule-of-succession.R
Last active June 6, 2018 14:55
Visualising Laplace's rule of succession
library(tidyverse)
# CF en.wikipedia.org/wiki/Rule_of_succession
# True probabilities
true_probs <- c(0.1, 0.3, 0.7, 0.9)
# How many runs for each probability and prior?
n_repetitions <- 10
# How many observations to update estimated probability?
n_observations <- 500
library(tidyverse)
library(lubridate)
source("https://raw.githubusercontent.com/mjfrigaard/RDataSets/master/DotR/monarchs.R")
monarchs <-
monarchs %>%
mutate(reign_length = lubridate::interval(from, to) / years(1))
monarchs %>%
@Torvaney
Torvaney / flight-delays.Rmd
Created July 1, 2018 22:24
WIP/my flight is delayed
---
title: "UK flights"
---
```{r}
library(tidyverse)
library(rvest)
```
@Torvaney
Torvaney / hillary.R
Last active July 13, 2018 13:58
is both a boy and a girls name
# install.packages("babynames")
library(babynames)
babynames %>%
filter(str_detect(name, "Hill?ary")) %>%
count(year, sex, wt = prop) %>%
ggplot(aes(x = year, y = nn)) +
geom_line(aes(colour = sex)) +
scale_y_continuous(labels = scales::percent_format()) +
theme_minimal() +
@Torvaney
Torvaney / premier_league_xg.csv
Created July 14, 2018 08:49
Premier League shot xGs 2014/15 -> 2017/18
We can't make this file beautiful and searchable because it's too large.
match_id,date,home,away,hgoals,agoals,side,xg,league,season
4749,2014-08-16T12:45:00Z,Manchester United,Swansea,1,2,h,0.01874101348221302,EPL,2014
4749,2014-08-16T12:45:00Z,Manchester United,Swansea,1,2,h,0.015597633086144924,EPL,2014
4749,2014-08-16T12:45:00Z,Manchester United,Swansea,1,2,h,0.06492315977811813,EPL,2014
4749,2014-08-16T12:45:00Z,Manchester United,Swansea,1,2,h,0.057787537574768066,EPL,2014
4749,2014-08-16T12:45:00Z,Manchester United,Swansea,1,2,h,0.0488010048866272,EPL,2014
4749,2014-08-16T12:45:00Z,Manchester United,Swansea,1,2,h,0.03097105398774147,EPL,2014
4749,2014-08-16T12:45:00Z,Manchester United,Swansea,1,2,h,0.02559618093073368,EPL,2014
4749,2014-08-16T12:45:00Z,Manchester United,Swansea,1,2,h,0.06947857141494751,EPL,2014
4749,2014-08-16T12:45:00Z,Manchester United,Swansea,1,2,h,0.5765787363052368,EPL,2014
benchmark <-
games %>%
mutate(
outcome = case_when(
hgoals > agoals ~ "home_win",
agoals > hgoals ~ "away_win",
hgoals == agoals ~ "draw"
)
) %>%
count(outcome) %>%
to_clipboard <- function(data) {
clip <- pipe("pbcopy", "w")
write.table(data, file = clip, row.names = FALSE)
close(clip)
}
@Torvaney
Torvaney / classifier-agreement-bounds.R
Created July 20, 2018 12:47
Bounds on classifier error given disagreement rate
lower_bound <- function(d) {
(1 - sqrt(1 - 2*d)) / 2
}
upper_bound <- function(d) {
(1 + sqrt(1 - 2*d)) / 2
}
tibble(d = seq(0, 0.5, 0.001),
lower = lower_bound(d),
suppressPackageStartupMessages({
library(tidyverse)
library(sf)
})
# c.f. http://web.mit.edu/tee/www/bertrand/problem.html
chord_length <- function(theta) {
2 * sin(theta / 2)
}