## this is binomial distribution
## start with a plot of the distribution
plot(9:12 ,dbinom(9:12,20,.50),
type='h',
ylab="",
xlab="",
lwd=5)#now calculate the probability
sum(dbinom(9:12, size=20, prob=0.5))[1] 0.6167
## this is binomial distribution
## start with a plot of the distribution
plot(9:12 ,dbinom(9:12,20,.50),
type='h',
ylab="",
xlab="",
lwd=5)#now calculate the probability
sum(dbinom(9:12, size=20, prob=0.5))[1] 0.6167
## this is binomial distribution
## start with a plot
plot(3:6 ,dbinom(3:6,13,.20),
type='h',
ylab="",
xlab="",
lwd=5)## now calculate the probability
sum(dbinom(3:6, size=13, prob=0.20))[1] 0.4913
# this is a poisson distribution
## start with a plot
plot(0:3, dpois(0:3, lambda = 5),
type='h',
ylab="",
xlab="",
lwd=5)# then calculate the probability
sum(dpois(0:3, lambda = 4.2))[1] 0.3954
## notes to keep track of work
# 17 bottles
# 3 bottles tested
# if 6 of 17 are contaminated,whats the prob less than 2 of the 3 tested are as well
# this is a hypergeometric distribution as the bottles aren't being replaced (fixed sample from which 3 bottles are being pulled)
#probability
dhyper(0,6,17-6,3)+dhyper(1,6,17-6,3)[1] 0.7279
# set graph values
x4<-0:3
prob<-dhyper(x4,6,17-6,3)
#plot
barplot(prob, names.arg=x4,
col=cols,
ylab="",
xlab="")Warning in rect(y1, x1, y2, x2, ...): supplied color is neither numeric nor
character
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?
# 6 employees gone
# 6 employees over 50
# 19 employees under 50
# 56 employees total
# whats the probability that 1 or more employee is over 50
# this is a hypergeometric probability since the employees are not being replaced
# start with the probability as vectors need to be defined to plot
x5<-2:6
probability2<-sum(dhyper(x5, 6,25-6,6))
probability2[1] 0.4529
#plot
barplot(x5, probability2,
ylab="",
xlab="",
lwd=1)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.
#this is a normal distribution
## the standard deviation is the square root of the variance, which we are given
# set values
x<-seq(0,2000)
y<-dnorm(x, 800, sqrt(90000))
# plot
plot(x,y, type="l", lwd=1,
xlab="",
ylab="")# calculate probability
pnorm(1460, 800, sqrt(90000))-pnorm(1040, 800, sqrt(90000))[1] 0.198
#this is a normal distribution
# set values
x2<-seq(0,200, by=1)
y2<-dnorm(x2, 106, 4)
# plot
plot(x2,y2,type="l", lwd=4,
xlab="",
ylab="")# calculate probability
pnorm(111, 106, 4)-pnorm(103, 106, 4)[1] 0.6677
## calculate a range for the plot
x3<-seq(3.34-5*0.07, 3.34+5*0.07, length.out=1000)
y3<-dnorm(x3, 3.34,0.07)
# plot
plot(x=x3,y=y3, type="l", lwd=2,
xlab="",
ylab="")## find the lengths
qnorm(0.03,3.34,.07)[1] 3.208
qnorm(0.97,3.34,.07)[1] 3.472
# notes to keep track
# mean is 75.8
# standard deviation is 8.1
# looking for minimum score for top 9% ie, the 91st percentile
# find minimum score
round(qnorm(0.91, mean=75.8, sd=8.1))[1] 87
# set values
x5<-seq(30, 150, length=200)
y5<-dnorm(x5, mean=75.8, sd=8.1)
# plot
plot(x5, y5, type="l",
xlab="",
ylab="")
# add a line to show where the 91st percentile is
abline(v=round(qnorm(0.91, mean=75.8, sd=8.1)))# notes to keep track
# p=0.61 will not crash
# n=155
# lower bounds are 95.6, upper is 96.5
# sd=sqrt(n*p*(1-p))
# approximation formula = pnorm(upper, mean, standard dev)-pnorm(lower, mean, standard dev)
meandef<-155*0.61
sddef<-sqrt(155*0.61*(1-0.61))
## set values for the graph, ensure large enough to capture all values
x6<-seq(meandef-4*sddef, meandef+4*sddef, length=200)
y6<-dnorm(x6, mean=meandef, sd=sddef)
plot(x6,y6, type="l", lwd=3,
xlab="",
ylab="")pnorm(96.5,mean=meandef, sd=sddef)- pnorm(95.5,mean=meandef,sd=sddef)[1] 0.06378