Skip to content

Instantly share code, notes, and snippets.

@Athospd
Last active November 4, 2019 13:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Athospd/583174785911d55489fa93c90eb6603e to your computer and use it in GitHub Desktop.
Save Athospd/583174785911d55489fa93c90eb6603e to your computer and use it in GitHub Desktop.
library(tidyverse)
library(gganimate)
library(broom)
library(rsample)
library(purrr)
library(modelr)
library(magick)
set.seed(1)
cars_bs <- cars %>%
bootstrap(1000) %>%
mutate(
lm = map(strap, ~lm(dist ~ speed, data = .)),
coeficientes = map(lm, tidy)
# predicoes = map(lm, augment)
) %>%
unnest(coeficientes) %>%
mutate(
frame = parse_number(.id)
)
retas <- cars_bs %>%
select(frame, term, estimate) %>%
spread(term, estimate) %>%
ggplot() +
geom_point(aes(x = speed, y = dist), alpha = 0.5, data = cars) +
geom_abline(aes(intercept = `(Intercept)`, slope = speed, group = frame), alpha = 0.2, colour = "salmon") +
transition_states(frame, 1, 1) +
ggtitle("# retas = {frame}") +
shadow_mark()
retas_gif <- animate(retas, width = 270, height = 270, nframes = 1000, fps = 50, end_pause = 10)
hist_intercept <- cars_bs %>%
select(frame, term, estimate) %>%
nest(terms = c(term, estimate)) %>%
mutate(
terms = accumulate(terms, ~ bind_rows(.x, .y))
) %>%
unnest(terms) %>%
ggplot() +
geom_histogram(aes(x = estimate), bins = 15) +
facet_wrap(~term, scales = "free_x") +
coord_cartesian(ylim = c(0, 200)) +
transition_states(frame, 1, 1)
hist_intercept_gif <- animate(hist_intercept, width = 2*272, height = 270, nframes = 1000, fps = 50, end_pause = 10, ref_frame = 999)
distrib_conj <- cars_bs %>%
select(frame, term, estimate) %>%
spread(term, estimate) %>%
ggplot() +
geom_point(aes(x = `(Intercept)`, y = speed), alpha = 0.2, colour = "black", size = 2) +
transition_states(frame, 1, 1) +
ggtitle("# retas = {frame}") +
shadow_mark() +
enter_grow() +
exit_shrink(size = 1)
distrib_conj_gif <- animate(distrib_conj, width = 270, height = 270, nframes = 1000, fps = 50, end_pause = 10)
retas_mgif <- image_read("retas_gif.gif")
hist_intercept_mgif <- image_read("hist_intercept_gif.gif")
distrib_conj_gif <- image_read("distrib_conj_gif.gif")
combined_gif <- image_read("combined_gif.gif")
# combined_gif <- image_append(c(retas_mgif[1], hist_intercept_mgif[1]))
pb <- progress::progress_bar$new(total = 400)
for(i in 2:1000){
pb$tick()
combined <- image_append(c(retas_mgif[i], hist_intercept_mgif[i]))
combined_gif <- c(combined_gif, combined)
}
gganimate::save_animation(combined_gif, "combined_gif.gif")
library(tidyverse)
library(readxl)
library(janitor)
pega_cabecalho <- function(path) {
read_excel(path, skip = 1, n_max = 5, col_names = TRUE) %>%
clean_names %>%
fill(data) %>%
group_by(data) %>%
summarise(local = paste(local, collapse = ", "))
}
carrega_e_limpa_planilha <- function(path) {
dados <- read_excel(path, skip = 6) %>%
filter_at(vars(-`Ordem de inscrição`), any_vars(!is.na(.))) %>%
clean_names
}
dados <- tibble(arquivo = list.files("xlsx", full.names = TRUE)) %>%
mutate(
cabecalho = map(arquivo, pega_cabecalho),
dados = map(arquivo, carrega_e_limpa_planilha),
dados = map2(cabecalho, dados, crossing)
) %>%
select(-cabecalho) %>%
unnest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment