D606 Homework Chapter 3

Kyle Gilde

Feb. 19, 2017

OpenIntro Statistics

Practice: 3.1 (see normalPlot), 3.3, 3.17 (use qqnormsim from lab 3), 3.21, 3.37, 3.41 Graded: 3.2 (see normalPlot), 3.4, 3.18 (use qqnormsim from lab 3), 3.22, 3.38, 3.42

Load Packages

knitr::opts_chunk$set(#echo=FALSE, 
                      warning=FALSE, 
                      message=FALSE,
                      tidy=TRUE,
                      #comment = "",
                      dev="png", 
                      dev.args=list(type="cairo"))

#https://cran.r-project.org/web/packages/prettydoc/vignettes/
#https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf

load.packages <- c("DATA606","prob")


ipak <- function(pkg){
    #FUNCTION SOURCE: https://gist.github.com/stevenworthington/3178163
    new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
    if (length(new.pkg)) 
        install.packages(new.pkg, dependencies = TRUE)
    sapply(pkg, require, character.only = TRUE)
}
ipak(load.packages)
## Loading required package: DATA606
## 
## Attaching package: 'DATA606'
## The following object is masked from 'package:utils':
## 
##     demo
## Loading required package: prob
## Loading required package: combinat
## 
## Attaching package: 'combinat'
## The following object is masked from 'package:utils':
## 
##     combn
## Loading required package: fAsianOptions
## Loading required package: timeDate
## Loading required package: timeSeries
## Loading required package: fBasics
## 
## Rmetrics Package fBasics
## Analysing Markets and calculating Basic Statistics
## Copyright (C) 2005-2014 Rmetrics Association Zurich
## Educational Software for Financial Engineering and Computational Science
## Rmetrics is free software and comes with ABSOLUTELY NO WARRANTY.
## https://www.rmetrics.org --- Mail to: info@rmetrics.org
## Loading required package: fOptions
## 
## Rmetrics Package fOptions
## Pricing and Evaluating Basic Options
## Copyright (C) 2005-2014 Rmetrics Association Zurich
## Educational Software for Financial Engineering and Computational Science
## Rmetrics is free software and comes with ABSOLUTELY NO WARRANTY.
## https://www.rmetrics.org --- Mail to: info@rmetrics.org
## Loading required package: hypergeo
## Loading required package: VGAM
## Loading required package: stats4
## Loading required package: splines
## 
## Attaching package: 'VGAM'
## The following object is masked from 'package:hypergeo':
## 
##     is.zero
## The following object is masked from 'package:fAsianOptions':
## 
##     erf
## 
## Attaching package: 'prob'
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, union

3.2 Area under the curve, Part II.

What percent of a standard normal distribution N(mu = 0,= 1) is found in each region? Be sure to draw a graph.

  1. Z > -1.13 (b) Z < 0.18 (c) Z > 8 (d) |Z| < 0.5
normalPlot(bounds = c(-1.13, Inf))

normalPlot(bounds = c(-Inf, 0.18))

normalPlot(bounds = c(8, Inf))

normalPlot(bounds = c(-0.5, 0.5))

##3.4 Triathlon times, Part I.

In triathlons, it is common for racers to be placed into age and gender groups. Friends Leo and Mary both completed the Hermosa Beach Triathlon, where Leo competed in the Men, Ages 30 - 34 group while Mary competed in the Women, Ages 25 - 29 group. Leo completed the race in 1:22:28 (4948 seconds), while Mary completed the race in 1:31:53 (5513 seconds). Obviously Leo finished faster, but they are curious about how they did within their respective groups. Can you help them? Here is some information on the performance of their groups:

+The finishing times of the Men, Ages 30 - 34 group has a mean of 4313 seconds with a standard deviation of 583 seconds.

+The finishing times of the Women, Ages 25 - 29 group has a mean of 5261 seconds with a standard deviation of 807 seconds.

+The distributions of finishing times for both groups are approximately Normal.

Remember: a better performance corresponds to a faster finish.

  1. Write down the short-hand for these two normal distributions. \(N(\mu=4313, \sigma=583)\) \(N(\mu=5261, \sigma=807)\)

  2. What are the Z-scores for Leo’s and Mary’s finishing times? What do these Z-scores tell you?

leo <- 4948
mary <- 5513

male.mu <- 4313
male.sd <- 583

female.mu <- 5261
female.sd <- 807

leo.z <- (leo - male.mu)/male.sd
mary.z <- (mary - female.mu)/female.sd

leo.z
## [1] 1.089194
mary.z
## [1] 0.3122677
print("Both of them had times greater than their groups' average, and Mary's z-score is relatively closer to her group's mean than Leo's.")
## [1] "Both of them had times greater than their groups' average, and Mary's z-score is relatively closer to her group's mean than Leo's."
  1. Did Leo or Mary rank better in their respective groups? Explain your reasoning. Since her z-score is less than Leo’s, Mary performed better vis-a-vis her group than Leo performed in relation to his group.

  2. What percent of the triathletes did Leo finish faster than in his group?

pnorm(leo, male.mu, male.sd, lower.tail = F)
## [1] 0.1380342
  1. What percent of the triathletes did Mary finish faster than in her group?
pnorm(mary, female.mu, female.sd, lower.tail = F)
## [1] 0.3774186
  1. If the distributions of finishing times are not nearly normal, would your answers to parts (b) - (e) change? Explain your reasoning. Yes, the entire z-score is premised on a normal distribution. We wouldn’t know how them performed relative to the group unless we knew all of the group times.

3.18 Heights of female college students.

Below are heights of 25 female college students.

  1. The mean height is 61.52 inches with a standard deviation of 4.58 inches. Use this information to determine if the heights approximately follow the 68-95-99.7% Rule. It’s a close approximation.
heights <- c(54, 55, 56, 56, 57, 58, 58, 59, 60, 60, 60, 61, 61, 62, 62, 63, 
    63, 63, 64, 65, 65, 67, 67, 69, 73)

M <- 61.52
std <- 4.58

sd.1 <- c(M - std, M + std)
sd.2 <- c(M - 2 * std, M + 2 * std)
sd.3 <- c(M - 3 * std, M + 3 * std)

b.w <- function(x, ends) x >= ends[1] & x <= ends[2]
# function source:
# http://www.talkstats.com/showthread.php/28194-Between-Operator

sum(b.w(heights, sd.1))/length(heights)
## [1] 0.68
sum(b.w(heights, sd.2))/length(heights)
## [1] 0.96
sum(b.w(heights, sd.3))/length(heights)
## [1] 1
  1. Do these data appear to follow a normal distribution? Explain your reasoning using the graphs provided below.

They do roughly follow the 68-95-99.7 rule, and they form a petty straight line on a qqplot.

qqnorm(heights)
qqline(heights)

##3.22 Defective rate. A machine that produces a special type of transistor (a component of computers) has a 2% defective rate. The production is considered a random process where each transistor is independent of the others.

  1. What is the probability that the 10th transistor produced is the first with a defect?
p <- 0.02
n <- 10

dgeom(n - 1, p)  #the 1st input is the number of failures
## [1] 0.01667496
  1. What is the probability that the machine produces no defective transistors in a batch of 100?
dbinom(0, 100, p)
## [1] 0.1326196
  1. On average, how many transistors would you expect to be produced before the first with a defect? What is the standard deviation?
1/p
## [1] 50
sqrt((1 - p)/p^2)
## [1] 49.49747
  1. Another machine that also produces transistors has a 5% defective rate where each transistor is produced independent of the others. On average how many transistors would you expect to be produced with this machine before the first with a defect? What is the standard deviation?
p <- 0.05
1/p
## [1] 20
sqrt((1 - p)/p^2)
## [1] 19.49359
  1. Based on your answers to parts (c) and (d), how does increasing the probability of an event affect the mean and standard deviation of the wait time until success?

When p is larger, the event becomes more likely to occur, which makes the expected number of trials before a success & the standard deviation of the waiting time decrease

3.38 Male children.

While it is often assumed that the probabilities of having a boy or a girl are the same, the actual probability of having a boy is slightly higher at 0.51. Suppose a couple plans to have 3 kids.

  1. Use the binomial model to calculate the probability that two of them will be boys.
p <- 0.51
a <- dbinom(2, 3, p)
a
## [1] 0.382347
  1. Write out all possible orderings of 3 children, 2 of whom are boys. Use these scenarios to calculate the same probability from part (a) but using the addition rule for disjoint outcomes. Confirm that your answers from parts (a) and (b) match.
PB <- p
PG <- 1 - PB
my.set <- c(PB, PB, PG)
sample.space <- data.frame(matrix(unlist(unique(permn(my.set))), ncol = 3, byrow = T))
sample.space$prob <- sample.space$X1 * sample.space$X2 * sample.space$X3
b <- sum(sample.space$prob)
a
## [1] 0.382347
b
## [1] 0.382347
round(a, 6) == round(b, 6)
## [1] TRUE
print("The 2 answers match if you round them")
## [1] "The 2 answers match if you round them"
  1. If we wanted to calculate the probability that a couple who plans to have 8 kids will have 3 boys, briefly describe why the approach from part (b) would be more tedious than the approach from part (a). The approach (b) would be much more tedious because you would have to write out 8 columns of the 56 possible combinations.
choose(8, 3)
## [1] 56

3.42 Serving in volleyball.

A not-so-skilled volleyball player has a 15% chance of making the serve, which involves hitting the ball so it passes over the net on a trajectory such that it will land in the opposing team’s court. Suppose that her serves are independent of each other.

  1. What is the probability that on the 10th try she will make her 3rd successful serve?
p <- 0.15
k <- 3
n <- 10
dnbinom(n - k, k, p)
## [1] 0.03895012
# dnbinom(x, y, p). This distribution allows to calculate the probability
# that a number of failures x occurs before y-th success
  1. Suppose she has made two successful serves in nine attempts. What is the probability that her 10th serve will be successful? .15
  2. Even though parts (a) and (b) discuss the same scenario, the probabilities you calculated should be different. Can you explain the reason for this discrepancy? Parts a and b are asking for 2 different things. Part a is looking for the negative binomial, and part b is trying to trip you up. Since the serves are independent, the probability of any one serve will always be 15%.