Skip to content

Instantly share code, notes, and snippets.

@maxdrohde
Created December 14, 2020 09:00
Show Gist options
  • Save maxdrohde/c8a11cc0a8b66b63651b16c63a1729a4 to your computer and use it in GitHub Desktop.
Save maxdrohde/c8a11cc0a8b66b63651b16c63a1729a4 to your computer and use it in GitHub Desktop.
Sample Mean Distribution -- Gamma
library(tidyverse)
library(gganimate)
# Grid for plotting
x <- seq(0,10, length.out = 1e3)
# Sample Sizes
n <- 1:20
# Create the densities at each value of the grid
data <- map(n, ~tibble(x=x, y=dgamma(x, shape=3*.x, rate=1*.x)))
df <-
tibble(n, data) %>%
unnest(data)
# Create animation
anim <-
df %>%
ggplot(aes(x=x, y=y)) +
geom_line() +
labs(subtitle = "Sample Size: {closest_state}",
title = "Sampling distribution of the mean for i.i.d Gamma(3,1)",
x= "x",
y= "Probability Density",
caption = "Sample Mean ~ Gamma(3n, n)") +
cowplot::theme_cowplot(font_family = "Source Sans Pro",
font_size = 10) +
theme(plot.title = element_text(size=10)) +
theme(legend.position = "none")+
transition_states(n, state_length = 1, transition_length = 2) +
shadow_mark(alpha=0.3, color="gray") +
ease_aes('quartic-in-out')
# Render animation
out <- animate(anim,
duration=10,
fps=60,
height = 3,
width = 4,
units = "in",
res = 300,
renderer = ffmpeg_renderer())
# Save to mp4
anim_save(animation = out, filename = "gamma_anim.mp4")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment