library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.1.1
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.4 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.1 v forcats 0.5.1
## Warning: package 'tibble' was built under R version 4.1.1
## Warning: package 'readr' was built under R version 4.1.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(fastGraph)
## Warning: package 'fastGraph' was built under R version 4.1.1
library(DATA606)
## Loading required package: shiny
## Loading required package: openintro
## Warning: package 'openintro' was built under R version 4.1.1
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
## Loading required package: OIdata
## Loading required package: RCurl
## Warning: package 'RCurl' was built under R version 4.1.1
##
## Attaching package: 'RCurl'
## The following object is masked from 'package:tidyr':
##
## complete
## Loading required package: maps
## Warning: package 'maps' was built under R version 4.1.1
##
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
##
## map
## Loading required package: markdown
##
## Welcome to CUNY DATA606 Statistics and Probability for Data Analytics
## This package is designed to support this course. The text book used
## is OpenIntro Statistics, 4th Edition. You can read this by typing
## vignette('os4') or visit www.OpenIntro.org.
##
## The getLabs() function will return a list of the labs available.
##
## The demo(package='DATA606') will list the demos that are available.
##
## Attaching package: 'DATA606'
## The following objects are masked from 'package:openintro':
##
## calc_streak, present, qqnormsim
## The following object is masked from 'package:utils':
##
## demo
Area under the curve, Part I. (4.1, p. 142) What percent of a standard normal distribution \(N(\mu=0, \sigma=1)\) is found in each region? Be sure to draw a graph.
For a standard normal distribution, N(μ=0,σ=1) and x = Z * sd + mu
pnorm(-1.35)
## [1] 0.08850799
shadeDist(-1.35)
#P(Z > 1.48) = 1 - P(Z<1.48)
1 - pnorm(1.48)
## [1] 0.06943662
shadeDist(1.48, lower.tail = FALSE)
# P(−0.4<Z<1.5) = P(Z<1.5) - P(Z<-0.4)
pnorm(1.5) - pnorm(-0.4)
## [1] 0.5886145
shadeDist(c(-0.4,1.5), lower.tail = FALSE)
\(P(|Z|>2) = P(Z < -2 or Z > 2) = P(Z< -2) + P(Z > 2)\)
\(P(Z< -2) + ( 1 - P(Z < 2))\)
pnorm(-2) + 1 - pnorm(2)
## [1] 0.04550026
shadeDist(c(-2,2))
Triathlon times, Part I (4.4, p. 142) 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:
Remember: a better performance corresponds to a faster finish.
(a) short-hand for the two normal Distribution
For Men (Age 30-34 Group): \(N(µ = 4313, σ = 583)\)
For Women (Age 25-29 Group): \(N(µ = 5261, σ = 807)\)
(b) Z-scores for Leo’s and Mary’s finishing times
z - scores: \(z = x−μ/σ\)
For leo, x = 4948
Leo Z-score = (4948 - 4313)/583 = 1.09
For mary, x = 5513
Mary Z-score = (5513 - 5261)/807 = 0.31
Leo is 1.09 standard deviations slower than the mean for his men group. Mary is 0.31 standard deviations slower than the mean for her women group.
(c) Leo’s and Mary’s rank in their respective group
# To calculate Leo Percentile, x = 4948
pnorm(4948, mean = 4313, sd=583)
## [1] 0.8619658
Leo: Percentile = 0.8620 = 86.20%
# To calculate Mary's Percentile, x = 5513
pnorm(5513, mean = 5261, sd=807)
## [1] 0.6225814
Mary: Percentile = 0.6226 = 62.26%
Leo did better than Mary in their respective group since he is in the 86.2 percentile of his age group while Mary is in the 62.26 percentile of her age group.
(d) Leo finish more than 86.20% of the triathletes
(e) Mary finish more than 62.26% of the triathletes
(f) If the distribution of the finishing time are not nearly normal, the answers to b - e will be different because the z-score calculated is with the assumption of a near normal distribution. If the normal distribution is skewed, then the z-score will also be skewed.
Heights of female college students Below are heights of 25 female college students.
\[ \stackrel{1}{54}, \stackrel{2}{55}, \stackrel{3}{56}, \stackrel{4}{56}, \stackrel{5}{57}, \stackrel{6}{58}, \stackrel{7}{58}, \stackrel{8}{59}, \stackrel{9}{60}, \stackrel{10}{60}, \stackrel{11}{60}, \stackrel{12}{61}, \stackrel{13}{61}, \stackrel{14}{62}, \stackrel{15}{62}, \stackrel{16}{63}, \stackrel{17}{63}, \stackrel{18}{63}, \stackrel{19}{64}, \stackrel{20}{65}, \stackrel{21}{65}, \stackrel{22}{67}, \stackrel{23}{67}, \stackrel{24}{69}, \stackrel{25}{73} \]
# Use the DATA606::qqnormsim function
To determine if it follows the 68-95-99.7% Rule, we check each of the categories:
length(heights)
## [1] 25
mean_height <- mean(heights) # 61.52
sd_height <- sd(heights) # 4.583667
mean_height
## [1] 61.52
sd_height
## [1] 4.583667
#mean_height + sd_height #66.10367
#mean_height - sd_height #56.93633
#mean_height + 2*sd_height #70.68733
#mean_height - 2*sd_height # 52.35267
#mean_height + 3*sd_height # 75.271
#mean_height - 3*sd_height # 47.769
within_1sd <- heights[which(heights < mean_height + sd_height & heights > mean_height - sd_height)]
within_2sd <- heights[which(heights < mean_height + 2*sd_height & heights > mean_height - 2*sd_height)]
within_3sd <- heights[which(heights < mean_height + 3*sd_height & heights > mean_height - 3*sd_height)]
68% rule: 68 percent of the data should fall within 1 standard deviation of the mean.
length(within_1sd)/length(heights)
## [1] 0.68
95% rule: 95 percent of the data should fall within 2 standard deviations of the mean.
length(within_2sd)/length(heights)
## [1] 0.96
99.7% rule: 99.7 percent of the data should fall within 3 standard deviations of the mean.
length(within_3sd)/length(heights)
## [1] 1
qqnormsim(heights)
(b) Yes, the data seem to follow the normal distribution. the histogram does not look perfectly symmetric, but the the Bell curve shows a good approximation of a normal distribution. Also, the quantiles plot is close to a line, which is a condition also for a normal distribution. So, we can say, that the distribution is nearly normal.
Defective rate. (4.14, p. 148) 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.
Transistor has 2% defective rate.that is 2/100 = 0.02
P <- (1-.02)^(10-1)*0.02
P
## [1] 0.01667496
A <- (1-.02)^100
A
## [1] 0.1326196
(c) Transitor produced before first defect
# expected value = EV = 1/p
EV <- 1/0.02
EV
## [1] 50
To calculate the Standard Deviation
# sd = sqrt((1-p))/p*p)
sd <- sqrt((1-.02)/(.02*.02))
sd
## [1] 49.49747
(d)
Another Machine with 5% defective rate.
# expected value = ev = 1/p
ev <- 1/.05
ev
## [1] 20
Standard Deviation of Machine with 5% defective
# sd = sqrt((1-p))/p*p)
sd <- sqrt((1-.05)/(.05*.05))
sd
## [1] 19.49359
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.
(a) probability that two of them will be boys
p <- dbinom(2, 3, 0.51)
p
## [1] 0.382347
(b) Possibilities of 2 boys from 3 Children
# p = P(gbb) + P(bbg) + P(bgb)
p <- (0.49 * 0.51 * 0.51) + (0.51 * 0.51 * 0.49) + (0.51 * 0.49 * 0.51)
p
## [1] 0.382347
(c) It will be tedious to calculate the probability of a couple having 3 boys from 8 children using method (b) because there will be (83C=56) possible way in which this could occur. This will involve us calculating 56 possible probability. However, if we are to use method (a), it is quite easy; since it just need one line of code to get the result as shown below.**
p_3boys <- dbinom(3, 8, 0.51)
p_3boys
## [1] 0.2098355
Serving in volleyball. (4.30, p. 162) 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.
(a) Probability of 3rd success on the 10th serve
Probability is 0.0389
p_ms <- .15
n <- 10
k <- 3
p <- choose(n-1, k-1) * (1-p_ms)^(n-k)*p_ms^k
p
## [1] 0.03895012
The probability that her 10th serve will be successful is still 0.15. This is because the serves are independent of each other. so the probability of a successful serve is 0.15
Solution (a) is dealing with a join probability, while (b) is dealing with a single independent trial (the probability that her 10th trail will be suceessful).