library(ggplot2)
- In the spinner problem (see Example 2.1) divide the unit
circumference into three arcs of length 1/2, 1/3, and 1/6. Write a
program to simulate the spinner experiment 1000 times and print out what
fraction of the outcomes fall in each of the three arcs. Now plot a bar
graph whose bars have width 1/2, 1/3, and 1/6, and areas equal to the
corresponding fractions as determined by your simulation. Show that the
heights of the bars are all nearly the same.
R Markdown
half = 0
third = 0
sixth = 0
for (i in 1:1000) {
spinner = runif(1)
if(spinner < .5) {half = half + 1}
if(spinner >= .5 & spinner <= (.5+(1/3))) {third = third + 1}
if(spinner > (.5+(1/3))){sixth = sixth + 1}
}
matrix_spin <- matrix(c(half/1000,third/1000,sixth/1000,1/2,1/3,1/6),ncol=3, byrow = T)
barplot(matrix_spin, main="Simulation Experiment vs Expected",
xlab="C", col=c("coral","darkseagreen3"),
beside=TRUE, names.arg = c("1/2 spin", "1/3 spin", "1/6 spin"))

print(matrix_spin)
## [,1] [,2] [,3]
## [1,] 0.485 0.3290000 0.1860000
## [2,] 0.500 0.3333333 0.1666667