Assignment 4

Name: [Joseph Kehoe] ID: [920697315] Date: [10-25-24]

R Markdown

In this assignment, you will use the R programming language to simulate two common probability experiments: flipping a coin and rolling a six-sided dice. The goal is to simulate these experiments 50 times each, calculate the experimental probability of each outcome, and then compare it to the theoretical probability. Additionally, you will create discrete probability distributions for these outcomes and visualize them using R.

PART ONE (4 points)

PART 1 - Simulation: Simulate 50 coin flips and 50 dice rolls.

# Simulate 50 coin flips (2 points)
library(tigerstats)
## Loading required package: abd
## Loading required package: nlme
## Loading required package: lattice
## Loading required package: grid
## Loading required package: mosaic
## Registered S3 method overwritten by 'mosaic':
##   method                           from   
##   fortify.SpatialPolygonsDataFrame ggplot2
## 
## The 'mosaic' package masks several functions from core packages in order to add 
## additional features.  The original behavior of these functions should not be affected by this.
## 
## Attaching package: 'mosaic'
## The following objects are masked from 'package:dplyr':
## 
##     count, do, tally
## The following object is masked from 'package:Matrix':
## 
##     mean
## The following object is masked from 'package:ggplot2':
## 
##     stat
## The following objects are masked from 'package:stats':
## 
##     binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
##     quantile, sd, t.test, var
## The following objects are masked from 'package:base':
## 
##     max, mean, min, prod, range, sample, sum
## Welcome to tigerstats!
## To learn more about this package, consult its website:
##  http://homerhanumat.github.io/tigerstats
coin<- c("H","T")
toss<- sample(coin, size = 50, replace=TRUE)
print(toss)
##  [1] "H" "T" "H" "H" "T" "H" "H" "H" "T" "H" "H" "H" "T" "T" "H" "T" "T" "H" "T"
## [20] "T" "H" "T" "T" "T" "H" "T" "T" "H" "H" "T" "H" "T" "T" "H" "H" "H" "H" "H"
## [39] "T" "T" "T" "T" "T" "H" "H" "T" "T" "H" "H" "T"
# Simulate 50 rolls of a six-sided dice (2 points)
dice<- c(1:6)
roll<- sample(dice, size = 50, replace = TRUE)

PART TWO (5 points)

Part 2 - Probability Calculation: Compute the experimental probabilities based on the simulation and compare them to theoretical probabilities.

# Calculate the experimental probability for each outcome of coin flips (Heads or Tails) (2 points)
heads_vector<- toss == "H"
heads_vector
##  [1]  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
## [13] FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE
## [25]  TRUE FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE  TRUE  TRUE  TRUE
## [37]  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE
## [49]  TRUE FALSE
heads <- sum(toss == "H")
heads
## [1] 25
prob_heads <- heads/50
print(prob_heads)
## [1] 0.5
# Calculate the experimental probability for each outcome of dice rolls (1,2,3,4,5,6) (2 points)
roll_counts <- table(roll)
roll_counts
## roll
##  1  2  3  4  5  6 
##  6 11 10  9  6  8
exp_prob_dice <- roll_counts / sum(roll_counts)
print(exp_prob_dice)
## roll
##    1    2    3    4    5    6 
## 0.12 0.22 0.20 0.18 0.12 0.16

Question: The theoretical probability for each outcome in a fair coin flip is ½ and for a fair six-sided die roll is ⅙. Compare your experimental probabilities with the theoretical probabilities for coin and dice. Discuss any discrepancies and possible reasons for them. (1 point) For the coin we got a 52% heads probability and for the die we got higher than expected chance for 1, 2, 4, and 5 and a lower chance for 3 and 6. This is because we aren’t flipping a coin or rolling a die all of the times possible, but a limited number of times. Each flip/roll has its own probability which is not dependent on the result of the previous event.

PART THREE (6 points)

Part 3 - Creating Data Frame and Visualization: Create a data frame of both probabilities. Then, create a bar plot of the coin and dice probabilities with barplot() or ggplot. Remember that the argument in the barplot() should be a vector or table, and the argument inside the ggplot() should be a dataframe.

#library(ggplot2)

# Create a data frame that represents the probability distribution of the outcomes of coin flips. (1 point)
coin_frame<- data.frame(toss)

# Create a data frame that represents the discrete probability distribution of the outcomes of dice rolls. (1 point)
dice_frame<- data.frame(roll_counts)
dice_table<- table(roll_counts)
# bar plot for coin flips (2 points)
toss_table1<-table(toss)
barplot(toss_table1)

# bar plot for dice rolls (2 points)
barplot(exp_prob_dice)