2

In R software, I am trying to generate a simulation plot for bernoulli trial for different sample sizes as below Simulation of Bernoulli Trials for increasing sample size

I have generated the proportions for given sample size and repeated the process 1000 times. But I dont understand, how to plot the graph as given figure.

m=1000 ###### replications
n=10  ###### sample size
my=function(){
n=10 ######Trials
u=rbinom(n,1,0.5)
}
Simulated_data=t((replicate(m, my())))
Proportions<-apply(Simulated_data,1,sum)/n
Proportions
Bilal Para
  • 65
  • 1
  • 8

1 Answers1

2

I think what you want is this.

R <- 50
n <- 1000

set.seed(42)
res <- replicate(R, rbinom(n, 1, .5))
p <- apply(res, 2, function(x) cumsum(x)/seq_along(x))
plot(1:1000, ylim=c(0, 1), type="n")
apply(p, 2, function(x) lines(1:1000, x, col="grey50"))
abline(h=.5)

enter image description here

jay.sf
  • 60,139
  • 8
  • 53
  • 110