Assigment 3

1- A researcher wishes to conduct a study of the color preferences of new car buyers.

Suppose that 50% of this population prefers the color red. If 20 buyers are randomly selected, what is the probability that between 9 and 12 (both inclusive) buyers would prefer red?

#Sample size 
n <- 20 
#probability of buyers preference for a red car
x <- 9:12
# 50% prefer the color red 
pi <- 0.5 

#math equation 

#P( x= 9:12 | n = 20 pi = 0.5)

#Binomial distribution 

dbinom1 <- dbinom(x,n,pi)
result <- sum(dbinom1)
print(round(result, digits = 4))
## [1] 0.6167
pro1 <- result * 100 
print("the probability of choosing a red car between 9 and 12 is 61%")
## [1] "the probability of choosing a red car between 9 and 12 is 61%"
?plot
## Help on topic 'plot' was found in the following packages:
## 
##   Package               Library
##   graphics              /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library
##   base                  /Library/Frameworks/R.framework/Resources/library
## 
## 
## Using the first match ...
plot(x    = 9:12, 
     y    = dbinom(x    = 9:12, 
                   size = 20, 
                   prob = .5
                  ), 
     type = 'h',
     main = 'Binomial Distribution (n=20, p=0.5)',
     ylab = 'Probability',
     xlab = '# Successes',
     lwd  = 3
     )

2- A quality control inspector has drawn a sample of 13 light bulbs from a recent production lot.

Suppose 20% of the bulbs in the lot are defective. What is the probability that less than 6 but more than 3 bulbs from the sample are defective?

#binomial distribution
#sample size 
n2 <- 13 

#probability of defective bulbs 
pi2 <- 0.2 

#less than 5 but more than 3 bulbs 

x2 <- 4:5

#math equation 
# P( x = 4:5 | n = 13, pi = 0.2)

?dbinom
dbinom2 <- dbinom( x2, n2, pi2)

result2 <- sum(dbinom2)

print(round(result2, digits = 4))
## [1] 0.2226
plot(x    = 0:13, 
     y    = dbinom(x    = 0:13, 
                   size = 13, 
                   prob = .2
                  ), 
     type = 'h',
     main = 'Binomial Distribution (n=13, p=0.2)',
     ylab = 'Probability',
     xlab = '# Successes',
     lwd  = 3
     )

3- The auto parts department of an automotive dealership sends out a mean of 4.2 special orders daily.

What is the probability that, for any day, the number of special orders sent out will be no more than 3?

#Poisson distribution 
# Set the variables 

x3 <- 3 
mean3 <- 4.2 

#math equation 
# P( x < 3| lambda = 4.2)

?ppois
ppois3 <- ppois( q = 3, lambda = 4.2, lower.tail = TRUE)

result3 <- round(ppois3, digits = 4)
result3
## [1] 0.3954
# double checking results 

sum(dpois(x=0:3, lambda = 4.2))
## [1] 0.3954034

4- A pharmacist receives a shipment of 17 bottles of a drug and has 3 of the bottles tested. If 6 of the 17 bottles are contaminated, what is the probability that less than 2 of the tested bottles are contaminated?

#hyper geometric distribution 
#Set up the Variables 

N4 <- 17 #population size 
n4 <- 3 # sample size 
s4 <- 6 # number of success in population 
f4 <- 11 # failure in population F = N - S

#Math equation 

#P( x < 2 | s=6, f=11, n=3)

# on the dhyper fun x = success in sample, m = success in population 
# n = failure in population, k = sample size 

?dhyper
result4 <- sum(dhyper(x = 0:1, m = 6, n = 11, k = 3))
result4
## [1] 0.7279412
print(paste0("the result is ", round(x = result4, digits = 7)))
## [1] "the result is 0.7279412"

5- A town recently dismissed 6 employees in order to meet their new budget reductions.

The town had 6 employees over 50 years of age and 19 under 50. If the dismissed employees were selected at random, what is the probability that more than 1 employee was over 50?

# Hypergeometric distribution 
# identify the variables 
# let x be the random variable of employees over fifty 

N5 <- 25 
S5 <- 6
n5 <- 6
F5 <- 19

# Math equation 
#P(x > 1 | s = 6, f = 19, n = 6)

result5 <- sum(dhyper(x = 2:6, m = 6, n = 19, k = 6))
result5 
## [1] 0.4528515

6- The weights of steers in a herd are distributed normally. The variance is 90,000 and the mean steer weight is 800 lbs. Find the probability that the weight of a randomly selected steer is between 1040 and 1460 lbs. Round your answer to four decimal places.

#identify the variables 

var <- 90000
mean <- 800
sd <- sqrt(90000)

#math equation 

#P(1040 < x < 1460 | mean = 800, standard deviation = 300 )

#best method dont use sum(dnorm) because it will give you a close aprox 
?pnorm
result6 <- pnorm(q = 1460, mean = mean, sd = sd) - pnorm(q=1040, mean = mean, sd = sd)
result6 
## [1] 0.197952

7- The diameters of ball bearings are distributed normally. The mean diameter is 106 millimeters and the standard deviation is 4 millimeters. Find the probability that the diameter of a selected bearing is between 103 and 111 millimeters.

#identify the variables 

mean7 <- 106 
sd7 <- 4 

#math equation 

#P(103 < x < 111| mean = 106, sd = 4)

result7 <- pnorm(q = 111, mean = 106, sd = 4) - pnorm(q=103, mean = 106, sd = 4)
result7
## [1] 0.6677229
#Shorter way of getting the solution 

?diff

diff( x = pnorm(c(103,111), mean = 106, sd = 4))
## [1] 0.6677229

8- The lengths of nails produced in a factory are normally distributed with a mean of 3.34 centimeters and a standard deviation of 0.07 centimeters. Find the two lengths that separate the top 3% and the bottom 3%. These lengths could serve as limits used to identify which nails should be rejected. Round your answer to the nearest hundredth, if necessary.

#identify the variables 

#quantile function 
mean8 <- 3.34
sd8 <- 0.07 

?qnorm

topqnorm <- qnorm(p = 0.97, mean = 3.34, sd = 0.07)
topqnorm
## [1] 3.471656
lowerqnorm <- qnorm(p = 0.03, mean = 3.34, sd = 0.07)
lowerqnorm 
## [1] 3.208344
round(x = c((lowerqnorm), (topqnorm)), digits = 3)
## [1] 3.208 3.472

9- A psychology professor assigns letter grades on a test according to the following

scheme.
A: Top 9% of scores
B: Scores below the top 9% and above the bottom 63%
C: Scores below the top 37% and above the bottom 17%
D: Scores below the top 83% and above the bottom 8%
F: Bottom 8% of scores
Scores on the test are normally distributed with a mean of 75.8 and a standard
deviation of 8.1. Find the minimum score required for an A grade.

#identify the variables 

mean9 <- 75.8 
sd9 <- 8.1 

#minimum score will be on the 91 percentile 

result9 <- qnorm(p = 0.91, mean = 75.8, sd = 8.1)
result9 
## [1] 86.66012
round(result9)
## [1] 87

10- Consider the probability that exactly 96 out of 155 computers will not crash in a day.

Assume the probability that a given computer will not crash in a day is 61%.
Approximate the probability using the normal distribution.

#identify the variables

n <- 155 
pi <- 0.61

#we know the formulas to calculate the mean and the sd 

mean10 <- pi * n  
sd10 <- sqrt(pi * (1-pi) * n)

# Math equation 

# P( x = 96 | pi = 0.61, n = 155)
result10 <- dnorm(x = 96, , mean = 0.61 * 155 , sd = sqrt(0.61 * 0.39 * 155))
result10
## [1] 0.06385071