Skip to content

Instantly share code, notes, and snippets.

@ajstewartlang
Last active October 13, 2018 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajstewartlang/2271ec208c4740a4dbdcebab390c80fe to your computer and use it in GitHub Desktop.
Save ajstewartlang/2271ec208c4740a4dbdcebab390c80fe to your computer and use it in GitHub Desktop.
# Note, this uses the new version of gganimate by Thomas Lin Pedersen (@thomasp85) available from:
# https://github.com/thomasp85/gganimate
# Apologies for clunkiness of code below...
library(tidyverse)
library(gganimate)
data <- NULL
sample <- NULL
d <- NULL
e <- NULL
for (i in (1:100)) {
set.seed(i+1234)
a <- rnorm(25, 1000, 50)
b <- rep(i, 25)
c <- mean(a)
c_1 <- rep(c, 25)
d <- mean(c(c_1, data$sample_mean))
e <- mean_cl_boot(c(a, data$DV))[1,1] - mean_cl_boot(c(a, data$DV))[1,2]
sample <- as.data.frame(cbind(b, a, c, d, e))
colnames(sample) <- c("sample", "DV", "sample_mean", "running_mean", "cl")
data <- rbind(sample, data)
}
ggplot(data, aes(x = sample, y = running_mean)) + geom_point(size = 2, colour="red") +
geom_errorbar(aes(ymin = running_mean - cl, ymax = running_mean + cl)) +
geom_hline(yintercept = 1000, colour = "blue") +
ylim(980,1020) +
labs(x = "Sample Number", y = "Moving Average",
title = "Moving average gets closer to the population \nmean (blue line) and CI bands narrow as \nsampling increases.") +
theme_minimal() +
theme(text = element_text(size = 15)) +
transition_time(sample) + shadow_mark(size = 5, colour = "grey")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment