#tinytex::install_tinytex()
library('DATA606')
## Loading required package: shiny
## Loading required package: openintro
## Warning: package 'openintro' was built under R version 4.1.2
## 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.2
## Loading required package: maps
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.1.2
## 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.
#$Z < -1.35$ = 0.089
pnorm(-1.35)
## [1] 0.08850799
normal_plot(mean = 0, sd = 1, cv = c(-5, -1.35))
#$Z > 1.48$ = 0.069
1-pnorm(1.48)
## [1] 0.06943662
normal_plot(mean = 0, sd = 1, cv = c(1.48, 5))
#$-0.4 < Z < 1.5$ = 0.589
pnorm(1.5)- pnorm(-0.4)
## [1] 0.5886145
normal_plot(mean = 0, sd = 1, cv = c(-0.4, 1.5))
#$|Z| > 2$ = 0.046
pnorm(-2) + (1-pnorm(2))
## [1] 0.04550026
normal_plot(mean = 0, sd = 1, cv = 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.
leo_time <- 4948
mary_time <- 5513
#Men's Mean and SD
m_mean <- 4313
m_sd <- 583
#Women's Mean and SD
w_mean <- 5261
w_sd <- 807
#z-score = (value - mean) / sd
#Z-Score for Leo
z_score_Leo <- (leo_time - m_mean) / m_sd
#Z-Score for Mary
z_score_Mary <- (mary_time - w_mean) / w_sd
print(c(z_score_Leo, z_score_Mary))
## [1] 1.0891938 0.3122677
1-pnorm(z_score_Leo)
## [1] 0.1380342
1-pnorm(z_score_Mary)
## [1] 0.3774186
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} \]
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)
#Mean of Heights
H_mean <- mean(heights)
H_sd <- sd(heights)
#Z-Score Heights
z_score_H <- (heights - H_mean) / H_sd
z_score_H
## [1] -1.6406080 -1.4224420 -1.2042761 -1.2042761 -0.9861101 -0.7679442
## [7] -0.7679442 -0.5497782 -0.3316122 -0.3316122 -0.3316122 -0.1134463
## [13] -0.1134463 0.1047197 0.1047197 0.3228856 0.3228856 0.3228856
## [19] 0.5410516 0.7592175 0.7592175 1.1955494 1.1955494 1.6318813
## [25] 2.5045451
#Percent to compare with 68% (±1 SD)
pnorm(H_mean + H_sd, mean = H_mean, sd = H_sd) - pnorm(H_mean - H_sd, mean = H_mean, sd = H_sd)
## [1] 0.6826895
#Percent to compare with 95% (±2 SD)
pnorm(H_mean + (2 * H_sd), mean = H_mean, sd = H_sd) - pnorm(H_mean - (2 * H_sd), mean = H_mean, sd = H_sd)
## [1] 0.9544997
#Percent to compare with 99.7% (±3 SD)
pnorm(H_mean + (3 * H_sd), mean = H_mean, sd = H_sd) - pnorm(H_mean - (3 * H_sd), mean = H_mean, sd = H_sd)
## [1] 0.9973002
# Use the DATA606::qqnormsim function
DATA606::qqnormsim(heights)
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.
#Geometric distribution in R
dgeom(10 - 1, 0.02)
## [1] 0.01667496
#Binomal distribution in R
dbinom(0, 100, 0.02)
## [1] 0.1326196
#Mean of expected transistors expected to be produced before first defect
p <- 0.02
mean <- 1 / p
mean
## [1] 50
#Standard deviation of expected transistors expected to be produced before first defect
p <- 0.02
sd <- sqrt((1 - p) / p^2)
sd
## [1] 49.49747
#Mean of expected transistors expected to be produced before first defect
p <- 0.05
mean <- 1 / p
mean
## [1] 20
#Standard deviation of expected transistors expected to be produced before first defect
p <- 0.05
sd <- sqrt((1 - p) / p^2)
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.
#Binomial Distribution
dbinom(2, 3, 0.51)
## [1] 0.382347
Probability is 38.23% matching the answer to part (a).
1 | G B B | 0.49 * 0.51 * 0.51
2 | B B G | 0.51 * 0.51 * 0.49
3 | B G B | 0.51 * 0.49 * 0.51
#Scenarios for 2 boys
P1 <- 0.49 * 0.51 * 0.51
P2 <- 0.51 * 0.51 * 0.49
P3 <- 0.51 * 0.49 * 0.51
P <- P1 + P2 + P3
P
## [1] 0.382347
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.
#Negative Binomial Distribution
dnbinom(7, 3, 0.15)
## [1] 0.03895012