Skip to content

Instantly share code, notes, and snippets.

@ismayc
Created July 20, 2017 01:05
Show Gist options
  • Save ismayc/49a7ccd6eac5fe70f11cae6fb05384b4 to your computer and use it in GitHub Desktop.
Save ismayc/49a7ccd6eac5fe70f11cae6fb05384b4 to your computer and use it in GitHub Desktop.
library(nycflights13)
library(tidyverse)
library(stringr)
# devtools::install_github("ismayc/infer")
library(infer)
set.seed(2017)
fli_small <- flights %>%
na.omit() %>%
sample_n(size = 10000) %>%
mutate(half_year = case_when(
between(month, 1, 6) ~ "h1",
between(month, 7, 12) ~ "h2"
)) %>%
mutate(day_hour = case_when(
between(hour, 1, 12) ~ "morning",
between(hour, 13, 24) ~ "not morning"
)) %>%
select(arr_delay, dep_delay, half_year,
day_hour, origin, carrier)
test <- fli_small %>%
specify(response = arr_delay) %>% # alt: arr_delay ~ NULL (or arr_delay ~ 1)
hypothesize(null = "point", mu = 50)
# dplyr::progress_estimated way
# Suggested at https://stackoverflow.com/questions/42725428/how-to-add-progress-bar-inside-dplyr-chain-in-r
print_tick_function <- function(x, p) {
p$tick()$print()
data.frame(x)
}
generate_progress <- function(dataframe, ...){
p <- progress_estimated(nrow(dataframe))
dataframe %>%
rowwise() %>%
do(print_tick_function(., p)) %>%
generate(...)
}
calculate_progress <- function(dataframe, ...){
p <- progress_estimated(nrow(dataframe))
dataframe %>%
rowwise() %>%
do(print_tick_function(., p)) %>%
calculate(...)
}
# Way, way slower likely due to the rowwise and do()
fli_small %>%
specify(response = arr_delay) %>% # alt: arr_delay ~ NULL (or arr_delay ~ 1)
hypothesize(null = "point", mu = 50) %>%
generate_progress(reps = 5, type = "bootstrap") %>%
calculate_progress(stat = "mean") -> mean_temp
fli_small %>%
specify(response = arr_delay) %>% # alt: arr_delay ~ NULL (or arr_delay ~ 1)
hypothesize(null = "point", mu = 50) %>%
generate(reps = 5, type = "bootstrap") %>%
calculate(stat = "mean") -> mean_temp
# Any way to do this without converting to rowwise()?
# Seems like it will really only work with *apply() and map_* functions
# or with for() loops where you can specify how many iterations it goes over
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment