Skip to content

Instantly share code, notes, and snippets.

@expersso
expersso / lda.R
Created February 24, 2019 08:59
Simulating DGP assumed by LDA
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) {
@expersso
expersso / binomial.R
Created January 22, 2019 13:40
PMF and likelihood of binomial RV
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))
@expersso
expersso / rgdp.R
Created November 6, 2018 10:02
Expenditure-side real GDP at chained PPPs
library(tidyverse)
library(scales)
library(ggrepel)
library(patchwork)
library(ggthemes)
library(pwt9)
years <- c(1955, 2014)
pwt9.0 <- pwt9.0 %>%
@expersso
expersso / pairs_plot.R
Created October 29, 2018 16:26
Create scatterplot matrix using the tidyverse
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))
@expersso
expersso / tidy_lags.R
Created October 23, 2018 08:51
Generate lags with tidyverse
# 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"),
@expersso
expersso / convergence_in_distribution.R
Created October 18, 2018 15:01
Illustration of random sequence that convergences in distribution, but not density
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)
)) %>%
@expersso
expersso / regression_simulation.R
Created October 17, 2018 15:16
Visualize effect of letting slopes and intercepts vary by groups in linear regression
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)
}
@expersso
expersso / tidy_excel.R
Last active September 12, 2018 15:11
Wrangle complicated spreadsheet into tidy dataframe
# 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",
@expersso
expersso / ggplot_axis_units.R
Created September 12, 2018 08:58
Function for adding units to axis labels
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 {
@expersso
expersso / german_crime.R
Last active June 19, 2018 09:08
Dataframe of German Crime Statistics
# 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",