Skip to content

Instantly share code, notes, and snippets.

@JoFAM
Last active January 15, 2020 15:30
Show Gist options
  • Save JoFAM/6c97105390ee11a5c425a0ea5377a453 to your computer and use it in GitHub Desktop.
Save JoFAM/6c97105390ee11a5c425a0ea5377a453 to your computer and use it in GitHub Desktop.
Small simulation on resampling inspired by the work of Daniel Lakens. Renders a gif animation.
library(gganimate)
# needs packages gifski & transformr
# If they are not installed, please also run
# install.packages(c("gifski", "transformr"))
# Function that does the simulation
create_pvals <- function(n){
x <- rnorm(n)
y <- rnorm(n)
last <- seq(10,n,by=10)
sapply(last, function(i){
t.test(x[1:i],y[1:i])$p.value
})
}
nsample <- 3000
nsim <- 10
alldata <- data.frame(
pvals = as.vector(replicate(10,create_pvals(nsample))),
sim = rep(1:nsim, each = nsample/nsim),
nsample = rep(seq(10,nsample,by=10), times = nsim)
)
ggplot(alldata, aes(y=pvals, x = nsample)) +
geom_line(lwd = 1.5) +
geom_hline(yintercept = 0.05, color = "blue") +
ggtitle("Simulation n: {closest_state}") +
transition_states(sim,
transition_length = 2) +
enter_fade()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment