In: Statistics and Probability
Fifteen six-sided dice are placed in a cup. The cup is shaken and turned over on a table, and the number of 1s is recorded. Use R commands to find the probabilities and histogram for the number of possible 1s. Please show the way I should set this up in R.
The possible values that 1 six sided die can land on are 1 to 6. The probability that a 6 sided die lands on a given value is 1/6
We will use the R command sample() to simulate 1 throw of 15 dice. That is we will simulate as if we are drawing a sample of size 15, with replacement from a jar which has 6 balls numbered 1 to 6
sample(1:6, size=15, prob=rep(1/6,6),replace=TRUE). The first input 1:6 says that the jar has 6 balls numbered 1 to 6. size=15 says select 15 balls from this jar which has 6 balls (so replace the ball using replace=TRUE after selecting each of the 15), prob=rep(1/6,6) says that the probability of selecting any given number from the jar is 1/6. Since there are 6 numbers, we need to set 6 values. hence rep(1/6,6) repeats the value 1/6 six times.
The R code (all statements starting with # are comments, you can remove them)
#set the random seed
set.seed(123)
#set the number of simulations
R<-1000
#initialize a variable to hols the number of 1s
y<-numeric(R)
#get 1000 samples of a throw of 15 dice
for (i in 1:R){
#throw 15 six sided fair dice and get the results
s<-sample(1:6,size=15,prob=rep(1/6,6),replace=TRUE)
#count the number of 1s and store
y[i]<-sum(s==1)
}
#print the probabilities
prob<-data.frame(table(y)/R)
colnames(prob)<-c("# of 1s","Prob")
prob
#plot a histogram
barplot(prob[,2],names.arg=prob[,1],main="Histogram of
1s",xlab="number of 1s",ylab="Probabilities")
box("plot")
#get this output
#get this plot