

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
R code and visualizations to study the probability distribution of the sum and maximum value when rolling two dice. The sample space consists of 36 simple events, and the document demonstrates how to calculate summary statistics and create histograms using r.
Typology: Study notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Rolling Two Dice Random Variables Using R Stat 341 - Fall 2008
In some examples, the sample space of the experiment is small enough to easily use R to study the distribution of the random variables arising from the particular sample space. In this help file, we will look at R code for the sample space of rolling two dice. The sample space consists of 36 simple events, each having the same probability 1/36. The following R code will set up the sample space S in a matrix with two columns (the outcomes on the two dice) and 36 rows (the 36 possible outcomes).
Stwodice<- scan() 1 1 1 2 1 3 1 4 1 5 1 6 2 1 2 2 2 3 2 4 2 5 2 6 3 1 3 2 3 3 3 4 3 5 3 6 4 1 4 2 4 3 4 4 4 5 4 6 5 1 5 2 5 3 5 4 5 5 5 6 6 1 6 2 6 3 6 4 6 5 6 6
Stwodice<- matrix(Stwodice, byrow = T, ncol = 2)
Applying the sum function or the max function to the rows of this matrix will produce the 36 sums and 36 maximum values corresponding to the 36 simple events in S.
sumtwodice<- apply(Stwodice, 1, sum) maxtwodice<- apply(Stwodice, 1, max)
We can then study the distribution of the two random variables using histograms and summary statistics. For example, to find the smallest and largest possible values of the sums, you can type
min(sumtwodice) max(sumtwodice)
To find other summary statistics of the possible sums, like the mean, median, five number summary, and standard deviation, you can type
mean(sumtwodice) #mean sqrt(var(sumtwodice)) #std. dev. fivenum(sumtwodice) #five number summary
To get a picture of the distribution of the possible sums, you can make a probability histogram. For these values, you should set up the histogram so that the possible values are centered in the bars of the histogram. For example, for the sums, you should set up your histogram as
sumdicebreaks<- c(1:12) + 0. hist(sumtwodice, breaks = sumdicebreaks, prob = T)
Here is a picture of the probability distribution of the sums.
2 4 6 8 10 12
Probability Distribution of the Sum of Two Dice
Sum of Two Dice
Probability
Similar code in R will give you the probability histogram and summary statistics for the distribution of the largest or maximum values of the two dice.
min(maxtwodice) #minimum value max(maxtwodice) #maximum value mean(maxtwodice) #mean sqrt(var(maxtwodice)) #std. dev. fivenum(maxtwodice) #five number summary maxdicebreaks<- c(0:6) + 0.5 #set the breaks for the largest value hist(maxtwodice, breaks = maxdicebreaks, prob = T)
Here is a picture of the probability distribution of the maximum values.
1 2 3 4 5 6
0.^
Probability Distribution of Maximum Value of Two Dice
Maximum Value of Two Dice
Probability