library(tidyverse)
library(glue)
library(ggbeeswarm)
flights <- nycflights13::flights %>%
filter(arr_delay > 0) %>%
mutate(arr_delay_log = log(arr_delay),
quarter = as.factor(1 + month %/% 4))
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
# Libraries | |
library(tidyverse, quietly = TRUE) | |
library(lubridate, warn.conflicts = FALSE, quietly = TRUE) | |
# Create simulated data set | |
set.seed(123) | |
possible_starts <- seq.POSIXt(ymd_hms("2018-10-30 09:00:00"), ymd_hms("2018-10-30 14:00:00"), by = "min") | |
possible_ends <- seq.POSIXt(ymd_hms("2018-10-30 14:00:00"), ymd_hms("2018-10-30 20:00:00"), by = "min") | |
(work_times <- tibble(worker_id = 1:100, |
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, quietly = TRUE) | |
(df <- tibble(col_a = c("a", "b", "c", "d", "d"), | |
col_b = c("b", "a", "d", "a", "e"))) | |
#> # A tibble: 5 x 2 | |
#> col_a col_b | |
#> <chr> <chr> | |
#> 1 a b | |
#> 2 b a | |
#> 3 c d |
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
# unnest list, mantain index example | |
library(tidyverse, quietly = TRUE) | |
(list_test <- list(1, 1, 1, 1, c(1, 2), 3, 3, c(2, 4))) | |
#> [[1]] | |
#> [1] 1 | |
#> | |
#> [[2]] | |
#> [1] 1 | |
#> |
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
``` r | |
library(tidyverse) | |
# as k increases from 1:p, see number of models created | |
p <- 10 | |
map_dbl(1:p, ~ choose(.x, c(1:p)) %>% sum()) %>% | |
enframe(name = NULL) %>% | |
rename(best_subset = value) %>% | |
mutate(p = 1:p, | |
stepwise = 1 + p * (p + 1) / 2) %>% |
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
``` r | |
library(tidyverse) | |
tibble(x = runif(100), x_inv = 1 - x) %>% | |
mutate(event = map2_lgl(.x = x, | |
.y = x_inv, | |
~sample(c(TRUE, FALSE), | |
size = 1, | |
replace = TRUE, | |
prob = c(.x, .y)) | |
) |
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(dplyr) | |
library(purrr) | |
library(ggplot2) | |
library(rlang) | |
# make "remainder" / throw away components more symmetric (expects balanced) | |
ggplot_continuous_binary <- function(df, covariate, response, rug = TRUE, snip_scales = FALSE, input_bin_size = NULL){ | |
covariate_var <- enquo(covariate) | |
response_var <- enquo(response) # needs to be either a TRUE/FALSE or a 1/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) | |
df_metrics <- tibble( | |
prob = seq.int(0.001, 0.999, length.out = 999), | |
entropy = -2 * (prob * log(prob) + (1-prob)*log(1-prob)), | |
gini_index = 4 * prob * (1 - prob) | |
) %>% | |
gather(entropy, gini_index, key = "purity_metric", value = "value") | |
ggplot(df_metrics, aes(x = prob, y = value, colour = purity_metric))+ |
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(purrr) | |
add_vecs <- function(...){ | |
if(!is.list(...)) stop("must be list input") | |
reduce(..., `+`) | |
} | |
add_vecs(list(1:5, 1:5, 1:5)) | |
#> [1] 3 6 9 12 15 | |
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) | |
contrast_deviation <- function(vec_fct){ | |
new_contrast <- unique(vec_fct) %>% length() %>% contr.sum() | |
contrasts(vec_fct) <- new_contrast | |
vec_fct | |
} | |
# convert all char columns to factors that use deviation coding | |
mpg %>% |
OlderNewer