#Stat 200 Lab #3

1) Probability Review

Formulas used for the Probability Review

  1. \[ P(A \cap B) = P(A|B)P(B)\]
  2. \[ P(A \cup B) = P(A) + P(B) - P(A \cap B) = P(A \cup B) = P(A) + P(B) - P(A|B)P(B) \]

Answers to Probability review

  1. D, used formula above
  2. D, a sample point is a single possible observed value of a variable, there are two possible outcomes when flipping a coin
  3. A, by definition of mutual exclusion
  4. C, by definiton of mutual exclusion
  5. D, by use of the formula above and the knowledge that the events are independent
  6. C, by definition of mutual exclusion
  7. D, by independence

Problem 2) Importing and Exporting data

hotdog<-read.table("/Users/Leland/Desktop/Stat 200/hotdog.txt", header=T, sep="\t")
head(hotdog)
##   TYPE CALORIES SODIUM
## 1 beef      186    495
## 2 beef      181    477
## 3 beef      176    425
## 4 beef      149    322
## 5 beef      184    482
## 6 beef      190    587
hotdognohead<-read.table("/Users/Leland/Desktop/Stat 200/hotdog.txt", sep="\t")
head(hotdognohead)
##     V1       V2     V3
## 1 TYPE CALORIES SODIUM
## 2 beef      186    495
## 3 beef      181    477
## 4 beef      176    425
## 5 beef      149    322
## 6 beef      184    482
  1. When you do not specify that the header is True, R creates column names for the table, and treats the column names of your data as data.
  2. The command will save the first line of data as the column names for the table

Problem 2) hotdog data

hotdog<-read.table("/Users/Leland/Desktop/Stat 200/hotdog.txt", header=T, sep="\t")
head(hotdog)
##   TYPE CALORIES SODIUM
## 1 beef      186    495
## 2 beef      181    477
## 3 beef      176    425
## 4 beef      149    322
## 5 beef      184    482
## 6 beef      190    587
boxplot(hotdog$SODIUM~hotdog$TYPE)

aggregate(hotdog$CALORIES, by=list(hotdog$TYPE), FUN=summary)
##   Group.1 x.Min. x.1st Qu. x.Median x.Mean x.3rd Qu. x.Max.
## 1    beef  111.0     140.5    152.5  156.8     177.2  190.0
## 2    meat  107.0     139.0    153.0  158.7     179.0  195.0
## 3 poultry   86.0     102.0    129.0  122.5     143.0  170.0
  1. The poultry dogs have more sodium on average, the beef dogs have the least amount of sodium on average
  2. See the output of the aggregate command above… This command splits the data into subsets, computes summary statistics for each, and returns the result in a convenient form.

Problem 3)

sample(1:6, size=4, replace=T) #repeat several times.
## [1] 1 1 3 6
result<-matrix(0, nrow=3000, ncol=4) #creates an empty container
for(i in 1:3000){result[i,]<-sample(1:6, size=4, replace=T)}
head(result)
##      [,1] [,2] [,3] [,4]
## [1,]    5    2    6    6
## [2,]    5    2    4    1
## [3,]    1    2    2    3
## [4,]    2    3    4    1
## [5,]    1    3    4    3
## [6,]    2    3    6    4
row.sum<-rowSums(result)
head(row.sum)
## [1] 19 12  8 10 11 15
result$SumOfRolls<-row.sum 
## Warning in result$SumOfRolls <- row.sum: Coercing LHS to a list
betterresult<-data.frame(result) #save the matrix into a dataframe, this will allow you to store the row.sum vector into the dataframe without using the transform command
betterresult$SumOfRolls<-row.sum
FreqTable<-table(betterresult$SumOfRolls)
FreqTable
## 
##   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21 
##   1  12  29  44  96 116 177 247 283 349 310 320 300 243 177 139  70  57 
##  22  23  24 
##  20   8   2
#There is no function for finding the mode, so we need to use a little hack
MostProbableRoll<-names(FreqTable)[FreqTable == max(FreqTable)]
LeastProbableRoll<-names(FreqTable)[FreqTable == min(FreqTable)]
hist(betterresult$SumOfRolls,ylab = 'SumofRolls', main = 'Frequency of sum of 4 die rolls')

  1. Based on the experiment, the most likely roll was
MostProbableRoll
## [1] "13"

and the least likely roll was

LeastProbableRoll
## [1] "4"

A continued) the probability of the most likely roll would be…, the probability of the least likely roll would be…

max(FreqTable)/3000
## [1] 0.1163333
min(FreqTable)/3000
## [1] 0.0003333333
  1. The histogram looks Normally distributed