############################################### ## Author: Joshua M. Tebbs ## Date: 11 December 2011 ## Update: 29 December 2017 ## STAT 509 course notes: R Code ## Chapter 2 ############################################### # Example 2.2 # Page 8-10 # 2002 Oakland A's example # Simulate each game during the season game.outcome = rbinom(162,1,0.571) # Create plot of relative frequencies # Initialise winning.percentage = game.outcome*0 for (i in 1:162){ winning.percentage[i] = sum(game.outcome[1:i])/i } plot(winning.percentage,ylab="Winning Percentage",xlab="Game",type="o",pch=20) abline(h=0.571,lty=2) # Simulate one 20-game stretch games = rbinom(20,1,0.571) games sum(games) # Simulate 1000 20-game stretches games = rbinom(1000,20,0.571) # Number of times (out of 1000) that 20 straight games are won length(games[games>19])