# libraries:
library(tidyverse)
library(ggplot2)
library(gganimate)
library(hrbrthemes)
t <- 100
x <- NULL
for(i in 1:t){
x[i] <- rbinom(1, 1, 0.50)
}
d <- data.frame(n=1:length(x), x, p = cumsum(x))
d$p <- d$p / d$n
# See the generted data
head(d)
## n x p
## 1 1 0 0.0000000
## 2 2 0 0.0000000
## 3 3 1 0.3333333
## 4 4 0 0.2500000
## 5 5 1 0.4000000
## 6 6 0 0.3333333
n is Sample number, x is the outcome of that sample, p is the estimated probability till that sample.
# Plot Animated Graph
d %>%
ggplot( aes(x=n, y=p)) +
geom_line() +
geom_point() +
xlim(0, 100) +
ylim(0, 1) +
geom_hline(yintercept = 0.50, alpha = 0.4, color = "red") +
# scale_color_viridis(discrete = TRUE) +
ggtitle("Convergence of Estimate with Sample Size") +
theme_ipsum() +
ylab("Estimated Probability") +
transition_reveal(n)