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(gtools) | |
library(topicmodels) | |
library(tidytext) | |
rcat <- function(n, x, p) { | |
sample(x, size = n, replace = TRUE, prob = p) | |
} | |
generate_document <- function(z, k, w, beta) { |
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(patchwork) | |
strip_lblr <- function(s) as_labeller(function(x) paste0(s, " = ", x)) | |
p_brks <- seq(0, 1, .25) %>% set_names() %>% set_names(MASS::fractions(.)) | |
x <- 4 | |
df <- crossing(k = 0:x, p = seq(0, 1, .05)) %>% | |
mutate(PX_k = dbinom(k, x, p)) |
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(scales) | |
library(ggrepel) | |
library(patchwork) | |
library(ggthemes) | |
library(pwt9) | |
years <- c(1955, 2014) | |
pwt9.0 <- pwt9.0 %>% |
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(patchwork) | |
plot_pair <- function(data, x, y) { | |
ggplot(data, aes_string(x = x, y = y, color = "Species", shape = "Species")) + | |
geom_point() + | |
scale_color_brewer(palette = "Dark2") + | |
theme(legend.position = "none", plot.title = element_text(size = 7)) + | |
labs(x = NULL, y = NULL, title = paste0(y, " ~ ", x)) |
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
# A rewrite of https://gist.github.com/seanjtaylor/f0f4815427a076c2544fc6a1a80e1dbd | |
# using some tidyverse tricks to create lagged variables | |
library(tidyverse) | |
library(glmnet) | |
library(broom) | |
wide_ <- "https://github.com/johnmyleswhite/room_temperatures/raw/master/temperatures.csv" %>% | |
read_csv() %>% | |
mutate(room = str_replace(room, "Room ", "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(gganimate) | |
f <- function(n, x) 1 - cos(2 * pi * n * x) | |
g <- function(.f, x) map_dbl(x, ~integrate(function(z) .f(z), 0, .x)$value) | |
df <- cross_df(list( | |
x = seq(0, 1, length.out = 100), | |
n = c(1, 2, 3, 5, 10, 15, 25) | |
)) %>% |
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) | |
set.seed(1) | |
make_regression_equation <- function(df) { | |
df %>% | |
summarise(eq = paste0(round(estimate, 1), term, collapse = " + ")) %>% | |
mutate(eq = paste0("y = ", eq), | |
eq = str_remove(eq, "\\(Intercept\\)")) %>% | |
pull(eq) | |
} |
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
# Inspired by http://www.brodrigues.co/blog/2018-09-11-human_to_machine/ | |
library(tidyverse) | |
library(readxl) | |
# Extract all data from one sheet into tidy dataframe | |
get_sheet <- function(sheet, path = "time-use.xlsx") { | |
ranges <- list( | |
col = "C3:BS5", |
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) | |
add_units <- function(p, unit, scale = "x", outside = TRUE) { | |
params <- ggplot_build(p)$layout$panel_params[[1]] | |
if(scale == "x") { | |
brks <- params$x.major_source | |
scale <- scale_x_continuous | |
} else { |
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
# 1987-1990: old federal states (Bundesländer) | |
# 1991-1992: old federal states and Berlin metropolitan area | |
# from 1993: all federal states | |
library(tidyverse) | |
nms <- c("key", "crime", "year", "total", "per_100k_pre2013_census", | |
"per_100k_post2013_census", "attempts_total", | |
"attempts_share_of_total", "with_firearm_threat", "with_firearm_shot", | |
"percent_cases_solved", "total_suspects", "non_german_suspects", |
NewerOlder