Last active
March 1, 2025 14:38
-
-
Save jrosell/4117052a56a8358c1c36fdb58d09bfa1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(magick) | |
library(purrr) | |
library(ggplot2) | |
# Define variables | |
FRAMES <- 30 | |
MEAN1 <- 0 # Mean of the first group | |
MEAN2 <- 0.2 # Mean of the second group | |
FROM <- 2 | |
TO <- 1500 | |
set.seed(1) | |
images_list <- seq(FROM, TO, length.out = FRAMES) |> | |
map(\(n) { | |
n <- as.integer(n) | |
data1 <- rnorm(n, mean = MEAN1, sd = 1) | |
data2 <- rnorm(n, mean = MEAN2, sd = 1) | |
t_test <- t.test(data1, data2) | |
p_value <- t_test$p.value | |
plot_data <- data.frame( | |
value = c(data1, data2), | |
group = factor(rep(c("Normal(0, 1)", "Normal(0.2, 1)"), each = n)) | |
) | |
img <- image_graph(600, 400, res = 96) | |
plot <- ggplot(plot_data, aes(x = value, fill = group)) + | |
geom_density(alpha = 0.3) + | |
labs( | |
title = "Comparison of Means", | |
subtitle = paste0("T-test p-value: ", round(p_value, 4), " (n = ", n, ")"), | |
x = "Value", | |
y = "Density", | |
fill = "" | |
) + | |
scale_fill_manual(values = c("blue", "red")) + | |
theme_bw() + | |
theme(legend.position = "top") | |
print(plot) | |
dev.off() | |
img | |
}) | |
animation <- image_animate(image_join(images_list), delay = 200, optimize = TRUE, loop = 0) | |
print(animation) | |
image_write(animation, "T_Test_Comparison.gif") |
Author
jrosell
commented
Nov 14, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment