#===================================================== # R program to simulate the law of large numbers. #===================================================== #----------------------------------------------------- # Simulate every number of trials from 5 to max.n. # Store proportions of successes in p.obs. #----------------------------------------------------- p=.45 # Probability of success. max.n=500 # Maximum number of trials. n.vec=5:max.n # Vector of numbers of trials. p.obs=numeric(length(n.vec)) for (i in 1:length(n.vec)) { p.obs[i]=rbinom(1,n.vec[i],p)/n.vec[i] } #----------------------------------------------------- # Plot p.obs versus n.vec. #----------------------------------------------------- plot(n.vec,p.obs,type="l",lty=1,xlab="number of trials", ylab="proportion of successes") abline(h=p,lty=2)