View advent-2021-day-19.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(adventdrob) | |
input <- advent_input(19, 2021) | |
beacons <- input %>% | |
filter(x != "") %>% | |
mutate(scanner = cumsum(str_detect(x, "scanner"))) %>% | |
filter(!str_detect(x, "scanner")) %>% | |
separate(x, c("x", "y", "z"), sep = ",", convert = TRUE) %>% |
View advent-2021-day-18.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(adventdrob) | |
# Add a value to the leftmost position in a binary tree | |
add_leftmost <- function(x, value) { | |
if (!is.list(x)) x + value | |
else list(add_leftmost(x[[1]], value), x[[2]]) | |
} | |
# Add a value to the rightmost position in a binary tree |
View cubic-model-animation.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(broom) | |
library(scales) | |
theme_set(theme_light()) | |
US <- read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv") %>% | |
mutate(new_deaths = deaths - lag(deaths)) %>% | |
filter(date >= "2020-02-26") | |
today <- max(US$date) |
View comparing-polynomial-models-covid.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(broom) | |
US <- read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv") %>% | |
mutate(new_deaths = deaths - lag(deaths)) %>% | |
filter(date >= "2020-02-26") | |
models <- tibble(degrees = 2:4) %>% | |
mutate(model = map(degrees, ~ lm(log(new_deaths + 1) ~ poly(date, .), data = US))) |
View simulate-dice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 100,000 simulations | |
n <- 1e5 | |
dice <- t(rmultinom(n, 5, rep(1, 6) / 6)) | |
# First rules for 1s and 5s | |
ones_score <- c(0, 100, 200, 1000, 5000, 10000)[dice[, 1] + 1L] | |
fives_score <- c(0, 50, 100, 0, 0, 0)[dice[, 5] + 1L] | |
# 100 x n score | |
most_common <- apply(dice, 1, which.max) |
View underpowered-simulation.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Code behind this tweet: | |
# https://twitter.com/drob/status/1126988304090574848 | |
library(tidyverse) | |
library(broom) | |
t_tests <- crossing(pi0 = .75, | |
effect_size = .25, | |
trial = 1:10000, |
View dice-rolls.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Code behind this tweet: https://twitter.com/drob/status/1100182329350336513 | |
library(tidyverse) | |
library(gganimate) | |
# Setup | |
options(gganimate.nframes = 200) | |
set.seed(2019) | |
simulation <- tibble(roll = 1:10000) %>% | |
mutate(result = sample(6, n(), replace = TRUE)) %>% |
View by_tag_year.csv
We can't make this file beautiful and searchable because it's too large.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
year,tag,number,year_total | |
2008,.htaccess,54,58390 | |
2008,.net,5910,58390 | |
2008,.net-2.0,289,58390 | |
2008,.net-3.5,319,58390 | |
2008,.net-4.0,6,58390 | |
2008,.net-assembly,3,58390 | |
2008,.net-core,1,58390 | |
2008,2d,42,58390 | |
2008,32-bit,19,58390 |
View cross validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(modelr) | |
library(tidyverse) | |
library(broom) | |
mtcars %>% | |
crossv_kfold(k = 10) %>% | |
mutate(model = map(train, ~ lm(mpg ~ wt, .)), | |
result = map2(model, test, ~ augment(.x, newdata = .y))) %>% | |
unnest(result) |
View gist:39af4512dff5b7357b534a04a971405d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
separate_sql <- function(expression) { | |
s <- paste(deparse(expression), collapse = "\n") | |
s <- stringr::str_replace(s, "%>%", "%>%\n ") | |
s <- stringr::str_split(s, "\n")[[1]] | |
val <- eval(expression) | |
list(expression = expression, | |
dplyr_code = s, | |
sql = as.character(dbplyr:::remote_query(val)), |
NewerOlder