library(tidyverse) #loading all library needed for this assignment
# library(readxl)
library(readr)
# library(plyr)
library(dplyr)
# library(dice)
# #library(VennDiagram)
# #library(help = "dice")
# library(DBI)
# library(dbplyr)
# library(data.table)
# library(rstudioapi)
# library(RJDBC)
# library(odbc)
# library(RSQLite)
# library(rvest)
# library(stringr)
# library(readtext)
# library(ggpubr)
# library(fitdistrplus)
library(ggplot2)
library(moments)
library(qualityTools)
# library(normalp)
# library(utils)
# library(MASS)
library(qqplotr)
library(DATA606)
##
## 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, 3rd Edition. You can read this by typing
## vignette('os3') 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.
Area under the curve, part I. (4.1, p.142) What percent of a standard normal distribution N(u = 0, rho = 1) is found in each region? Be sure to draw a graph.
round_df <- function(x){
x <- round(x/0.5)*0.5
x
}
round_df(3.466)
## [1] 3.5
#pnorm(1)
pnorm(1190, mean = 1100, sd = 200)
## [1] 0.6736448
signif_df <- function(a){
a <- signif(a, digits = 4)
a
}
signif_df(0.849404030)
## [1] 0.8494
# (a)...Z < - 1.35...range of Z being 2 of any |x|
getLabs()
## [1] "Lab1" "Lab2" "Lab3" "Lab4" "Lab5a" "Lab5b" "Lab6" "Lab7" "Lab8"
## [10] "Lab9"
demo(package = 'DATA606')
normalPlot(mean = 0, sd = 1, bounds = c(-4, -1.35), tails = FALSE)
# x <- seq(-4,4,length=200); y <- dnorm(x,mean=0, sd=1)
# plot(x, y, type = "l", lwd = 2, xlim = c(-3.5,3.5), ylab='', xlab='z-score', yaxt='n')
# normalPlot(fdo, threeWay = FALSE, na.last = NA, alpha = 0.05, response = NULL,sig.col = c("red1", "red2", "red3"), sig.pch = c(1, 2, 3), main,ylim, xlim, xlab, ylab, pch, col, border = "red", ...)
#calling a rounding function, Also I think this normal distribution could have been a funciton , then call for a, b, c, d
x = signif_df(pnorm(-1.35))
print(paste0(" (a) for Z < -1.35, the percent of a standard normal distribution N(u = 0, rho = 1) is found in the red region, Z = ", x))
## [1] " (a) for Z < -1.35, the percent of a standard normal distribution N(u = 0, rho = 1) is found in the red region, Z = 0.08851"
nline <- '-------------------------------------------------------------------------------------------------------------------------------------------------------------'
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
cat("percent of a standard normal distribution N(u = 0, rho = 1) is found in the red region, Z = ", x)
## percent of a standard normal distribution N(u = 0, rho = 1) is found in the red region, Z = 0.08851
sprintf("percent of a standard normal distribution N(u = 0, rho = 1) is found in the red region, Z = ", x)
## [1] "percent of a standard normal distribution N(u = 0, rho = 1) is found in the red region, Z = "
message("percent of a standard normal distribution N(u = 0, rho = 1) is found in the red region, Z = ", x)
## percent of a standard normal distribution N(u = 0, rho = 1) is found in the red region, Z = 0.08851
normalPlot(mean = 0, sd = 1, bounds = c(1.48, 4), tails = FALSE)
pnorm(1.48)
## [1] 0.9305634
x = signif_df(1 - pnorm(1.48))
print(paste0("(b)for Z > 1.48, the percent of a standard normal distribution N(u = 0, rho = 1) is found in the red region, Z = ", x))
## [1] "(b)for Z > 1.48, the percent of a standard normal distribution N(u = 0, rho = 1) is found in the red region, Z = 0.06944"
nline <- '-------------------------------------------------------------------------------------------------------------------------------------------------------------'
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
normalPlot(mean = 0, sd = 1, bounds = c(-0.4, 1.5), tails = FALSE)
x = signif_df(pnorm(1.5) - pnorm(-0.4))
print(paste0("(c) for -0.4 < Z < 1.5, the percent of a standard normal distribution N(u = 0, rho = 1) is found in the red region, Z = ", x))
## [1] "(c) for -0.4 < Z < 1.5, the percent of a standard normal distribution N(u = 0, rho = 1) is found in the red region, Z = 0.5886"
nline <- '-------------------------------------------------------------------------------------------------------------------------------------------------------------'
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
normalPlot(mean = 0, sd = 1, bounds = c(-2, 2), tails = FALSE)
x = signif_df(pnorm(-2)*2)
print(paste0("(d) for -2 > Z > 2 0r |Z| > 2, the percent of a standard normal distribution N(u = 0, rho = 1) is found in the red region, Z = ", x))
## [1] "(d) for -2 > Z > 2 0r |Z| > 2, the percent of a standard normal distribution N(u = 0, rho = 1) is found in the red region, Z = 0.0455"
nline <- '-------------------------------------------------------------------------------------------------------------------------------------------------------------'
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
Z_scoreDf <- function (x,mean, sigmas){
Z <- signif_df((x-mean)/sigmas)
return (Z)
}
Z_scoreDf(4979,4313,583 )
## [1] 1.142
nline <- '-------------------------------------------------------------------------------------------------------------------------------------------------------------'
print(("(a) Write down the short-hand for these two normal distributions."))
## [1] "(a) Write down the short-hand for these two normal distributions."
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
print(("for men of ages 30 - 34, N(mean = 4313, sigma = 583"))
## [1] "for men of ages 30 - 34, N(mean = 4313, sigma = 583"
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
print(("for women of ages 25 - 29, N(mean = 5261, sigma = 807"))
## [1] "for women of ages 25 - 29, N(mean = 5261, sigma = 807"
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
print(("(b) What are the Z-score for Leo and Mary finishing times? What do these Z-scores tell you. we know the best time is shortest"))
## [1] "(b) What are the Z-score for Leo and Mary finishing times? What do these Z-scores tell you. we know the best time is shortest"
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
print(paste0(" Z-score for Leo, Z = ", Z_scoreDf(4948,4313,583)))
## [1] " Z-score for Leo, Z = 1.089"
print((" Leo time is about 1.16 times the standard deviation of the group, meaning he is slower than the rest of the group."))
## [1] " Leo time is about 1.16 times the standard deviation of the group, meaning he is slower than the rest of the group."
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
print(paste0(" Z-score for Mary, Z = ", Z_scoreDf(5513,5261,807)))
## [1] " Z-score for Mary, Z = 0.3123"
print((" Mary time is about 0.31 times the standard deviation of the group, meaning he is faster than the rest of the group."))
## [1] " Mary time is about 0.31 times the standard deviation of the group, meaning he is faster than the rest of the group."
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
print(("(c) Did Leo or Mary rank better in their respective groups? Explain your reasoning"))
## [1] "(c) Did Leo or Mary rank better in their respective groups? Explain your reasoning"
print(("based on the answer from (b) Mary did betteer than Leo within their respective group."))
## [1] "based on the answer from (b) Mary did betteer than Leo within their respective group."
Leo = signif_df(1-pnorm(Z_scoreDf(4948,4313,583)))*100
Mary = signif_df(1-pnorm(Z_scoreDf(5513,5261,807)))*100
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
print(paste0(" (d)How many percent of the group did Leo race faster than, Leo = %", Leo))
## [1] " (d)How many percent of the group did Leo race faster than, Leo = %13.81"
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
print(paste0(" (d)How many percent of the group did Mary race faster than, Leo = %", Mary))
## [1] " (d)How many percent of the group did Mary race faster than, Leo = %37.74"
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
print(("(e) overall race, based on the answer from (b) and the percentage Mary did betteer than Leo within their respective group."))
## [1] "(e) overall race, based on the answer from (b) and the percentage Mary did betteer than Leo within their respective group."
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
print(("(a) 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. This is valid if the probabilities those heights fall within 1, 2, 3 within standard deviation of the mean ..."))
## [1] "(a) 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. This is valid if the probabilities those heights fall within 1, 2, 3 within standard deviation of the mean ..."
# this calculation can be done with a for loop
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
Z1 = qnorm((0.68))
Z2 = qnorm((0.96))
Z3 = qnorm((0.997))
X1 = signif_df( 61.52 + Z1*4.58)
X2 = signif_df (61.52 + Z2*4.58)
X3 = signif_df( 61.52 + Z3*4.58)
a1 =signif_df( 61.52 + 4.58)
a2 = signif_df (61.52 + 2*4.58)
a3 = signif_df (61.52 + 3*4.58)
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
sprintf("Probabilities for 1, 2, and 3 standard deviations of the
mean in a normal distribution: %f , %e, %g ", a1, a2, a3)
## [1] "Probabilities for 1, 2, and 3 standard deviations of the\nmean in a normal distribution: 66.100000 , 7.068000e+01, 75.26 "
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
sprintf("Probabilities for falling within 1, 2, and 3 standard deviations of the
mean in a normal distribution: %f , %e, %g ", X1, X2, X3)
## [1] "Probabilities for falling within 1, 2, and 3 standard deviations of the\nmean in a normal distribution: 63.660000 , 6.954000e+01, 74.1 "
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
print((" Yes (a) fall within 1, 2, 3 within standard deviation of the mean ..."))
## [1] " Yes (a) fall within 1, 2, 3 within standard deviation of the mean ..."
print((" (d) ...Yes these data appear to follow a normal distribution: The heights of the 25 females are well distributed ..based on the Normal Q-Q Plot (Theoretical Quantiles shows dots falliing on the line fall, based on height Histogram the line superimposed on the histograms shows the bell-shaped"))
## [1] " (d) ...Yes these data appear to follow a normal distribution: The heights of the 25 females are well distributed ..based on the Normal Q-Q Plot (Theoretical Quantiles shows dots falliing on the line fall, based on height Histogram the line superimposed on the histograms shows the bell-shaped"
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.
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
sprintf(" (a) What is the probability that the 10th transistor produced is the first with a defect? Answer: %f ", a = dgeom(10-1, 0.02))
## [1] " (a) What is the probability that the 10th transistor produced is the first with a defect? Answer: 0.016675 "
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
sprintf(" (b) What is the probability that the machine produces no defective transistors in a batch of 100? Answer: %e ", b = dbinom(0, 100,0.02))
## [1] " (b) What is the probability that the machine produces no defective transistors in a batch of 100? Answer: 1.326196e-01 "
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
sprintf("(c) On average, how many transistors would you expect to be produced before the first with a defect? What is the standard deviation? Answer: %f, %g", c = 1/0.02, c2 = sqrt((1-0.02)/0.02^2))
## [1] "(c) On average, how many transistors would you expect to be produced before the first with a defect? What is the standard deviation? Answer: 50.000000, 49.4975"
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
d1 = signif_df( 1/0.05)
d1
## [1] 20
d2 = signif_df (sqrt((1-0.05)/0.05^2))
d2
## [1] 19.49
print("(d) Another machine that also produces transistors has a 5 percent 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? Answer: 20 , 19.49")
## [1] "(d) Another machine that also produces transistors has a 5 percent 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? Answer: 20 , 19.49"
# # %a, %e, d1, d2
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
sprintf(" (e) 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? Answer: This will decrease the mean and standard deviation of the wait time until success")
## [1] " (e) 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? Answer: This will decrease the mean and standard deviation of the wait time until success"
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
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.
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
sprintf(" (a) Use the binomial model to calculate the probability that two of them will be boys. Answer: %f ", a = dbinom(2,3, 0.51))
## [1] " (a) Use the binomial model to calculate the probability that two of them will be boys. Answer: 0.382347 "
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
#This is combination of 2 boys in 3 children...meaning 1b2b3g, 1b2g3b, 1g2b3b
# P(b) = 0.51, P(g) = 1-P(b) = 0.49
# P(2b in 3children) = P(b)*P*b)*P(g) + P(b)*P(g)*P(b) + P(g)*P(b)*P(b)
P=0.51∗0.51∗0.49+0.51∗0.49∗0.51+0.49∗0.51∗0.51
P
## [1] 0.382347
sprintf(" (b) 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. Answer: %f , yes answer matches part a and b", P)
## [1] " (b) 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. Answer: 0.382347 , yes answer matches part a and b"
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
sprintf(" (c) 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).. Answer: this is combination of 3 boys in 8 kids %f ", c = choose(8,3))
## [1] " (c) 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).. Answer: this is combination of 3 boys in 8 kids 56.000000 "
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
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.
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
sprintf(" (a) What is the probability that on the 10th try she will make her 3rd successful serve? Answer: %f ", a = dnbinom(7,3, 0.15))
## [1] " (a) What is the probability that on the 10th try she will make her 3rd successful serve? Answer: 0.038950 "
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
print(" (b) Suppose she has made two successful serves in nine attempts. What is the probability that her 10th serve will be successful? Answer: the event are independent to each other with two successful serves made. Therefore, P = 15%")
## [1] " (b) Suppose she has made two successful serves in nine attempts. What is the probability that her 10th serve will be successful? Answer: the event are independent to each other with two successful serves made. Therefore, P = 15%"
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"
print(" (c) 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? Answer: part (b) falls in independent event, part (a) consecutive events meaning join event together ")
## [1] " (c) 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? Answer: part (b) falls in independent event, part (a) consecutive events meaning join event together "
print(nline)
## [1] "-------------------------------------------------------------------------------------------------------------------------------------------------------------"