For questions 1,2, and 4 use 1000 simulations to get your answer.

  1. You are studying an organisms behavior however the sex can only be determined by dissection after the experiment. You only have enough time and money to observe 10 individuals. What proportion of the time will you have at least three males and three females in your study. Assume that males and females are equally common.
# method one
females <- rbinom(1000, 10, prob=.5)
sum(colSums(rbind(females > 2, females < 8)) == 2)/1000
## [1] 0.9
# method two
successes <- c()
for(i in 1:1000){
  females <- sum(sample(c("M", "F"), 10, replace=T) == "F")
  successes[i] <- females > 2 && females < 8
}
sum(successes)/1000
## [1] 0.874
  1. Now reevaluate if males make up 75% of the population and females make up 25% of the population.
# method one
females <- rbinom(1000, 10, prob = .25)
sum(colSums(rbind(females > 2, females < 8)) == 2) / 1000
## [1] 0.489
# method two
successes <- c()
for(i in 1:1000){
  females <- sum(sample(c("M", "F"), 10, 
                        replace = T, 
                        prob = c(.75, .25)) == "F")
  successes[i] <- females > 2 && females < 8
}
sum(successes)/1000
## [1] 0.465
  1. Probability of flipping heads on a coin 4 times in a row (assume the coin is fair?
.5^4
## [1] 0.0625
  1. We have 68 students in our class. What is the average number of students that will flip 4 heads in a row across 1000 simulations.
nums <- c()
for(i in 1:1000){
  nums[i] <- sum(rbinom(68, 4, prob=.5) == 4)
}
mean(nums)
## [1] 4.176
  1. You create a vector by typing: x <- c(234, 874, 239, 598) into R. Demonstrate 3 ways that you can extract 874 and 598 from the matrix:
x <- c(234, 874, 239, 598)
x[c(2,4)]
## [1] 874 598
x[c(F,T,F,T)]
## [1] 874 598
x[-c(1,3)]
## [1] 874 598
  1. You create a matrix by typing x <- matrix(1:20, 10, 2) into R. Demonstrate three ways in which you could extract the values 17, 18, 19 from the matrix.
x <- matrix(1:20, 10, 2)
x[7:9, 2]
## [1] 17 18 19
x[c(7,8,9), 2]
## [1] 17 18 19
x[c(F,F,F,F,F,F,T,T,T,F),c(F,T)]
## [1] 17 18 19