Resources :
Skimming over Chapter 5, 6 and 7 in the Open Intro Statistics textbook may be helpful to see the standard error formulas for some of the questions. I will explicitly redirect you to the textbook for some questions.
I have 4 user defined functions below - you do not have to use them, but may find them useful to graphically draw out what is happening in the question.
We write a function which takes in two arguments (numbers here), runs some computations (basic inequality) on them and prints an output based on the computation result -
myp=function(p, alpha){
if(p<alpha){print('REJECT Ho')}else{print('FAIL 2 REJECT')}
}
Test our function to make sure it is performing as intended -
myp(.01, .05) # p is less than alpha
## [1] "REJECT Ho"
myp(.1, .05) # p is greater than alpha
## [1] "FAIL 2 REJECT"
Now, lets write a bit more complex function (takes in many arguments) that is designed to shade the standard normal distribution as the default option for a 5% double sided hypothesis test and can be adapted for other purposes too. You can chnage the arguments of mu, sig, pcts, color,…
shadenorm = function(below=NULL, above=NULL, pcts = c(0.025,0.975), mu=0, sig=1, numpts = 500, color = "gray", dens = 40, justabove= FALSE, justbelow = FALSE, lines=FALSE,between=NULL,outside=NULL){
if(is.null(between)){
below = ifelse(is.null(below), qnorm(pcts[1],mu,sig), below)
above = ifelse(is.null(above), qnorm(pcts[2],mu,sig), above)
}
if(is.null(outside)==FALSE){
below = min(outside)
above = max(outside)
}
lowlim = mu - 4*sig # min point plotted on x axis
uplim = mu + 4*sig # max point plotted on x axis
x.grid = seq(lowlim,uplim, length= numpts)
dens.all = dnorm(x.grid,mean=mu, sd = sig)
if(lines==FALSE){
plot(x.grid, dens.all, type="l", xlab="X", ylab="Density") # label y and x axis
}
if(lines==TRUE){
lines(x.grid,dens.all)
}
if(justabove==FALSE){
x.below = x.grid[x.grid<below]
dens.below = dens.all[x.grid<below]
polygon(c(x.below,rev(x.below)),c(rep(0,length(x.below)),rev(dens.below)),col=color,density=dens)
}
if(justbelow==FALSE){
x.above = x.grid[x.grid>above]
dens.above = dens.all[x.grid>above]
polygon(c(x.above,rev(x.above)),c(rep(0,length(x.above)),rev(dens.above)),col=color,density=dens)
}
if(is.null(between)==FALSE){
from = min(between)
to = max(between)
x.between = x.grid[x.grid>from&x.grid<to]
dens.between = dens.all[x.grid>from&x.grid<to]
polygon(c(x.between,rev(x.between)),c(rep(0,length(x.between)),rev(dens.between)),col=color,density=dens)
}
}
# TEST THE FUNCTION
shadenorm(mu = 0, sig = 1, pcts = c(0.025,0.975))
# shadenorm(mu = 20, sig = 6, pcts = c(0.025,0.975))
shadet = function(below=NULL, above=NULL, pcts = c(0.025,0.975), df=1, numpts = 500, color = "gray", dens = 40, justabove= FALSE, justbelow = FALSE, lines=FALSE,between=NULL,outside=NULL){
if(is.null(between)){
below = ifelse(is.null(below), qt(pcts[1],df), below)
above = ifelse(is.null(above), qt(pcts[2],df), above)
}
if(is.null(outside)==FALSE){
below = min(outside)
above = max(outside)
}
lowlim = -4
uplim = 4
x.grid = seq(lowlim,uplim, length= numpts)
dens.all = dt(x.grid,df)
if(lines==FALSE){
plot(x.grid, dens.all, type="l", xlab="X", ylab="Density")
}
if(lines==TRUE){
lines(x.grid,dens.all)
}
if(justabove==FALSE){
x.below = x.grid[x.grid<below]
dens.below = dens.all[x.grid<below]
polygon(c(x.below,rev(x.below)),c(rep(0,length(x.below)),rev(dens.below)),col=color,density=dens)
}
if(justbelow==FALSE){
x.above = x.grid[x.grid>above]
dens.above = dens.all[x.grid>above]
polygon(c(x.above,rev(x.above)),c(rep(0,length(x.above)),rev(dens.above)),col=color,density=dens)
}
if(is.null(between)==FALSE){
from = min(between)
to = max(between)
x.between = x.grid[x.grid>from&x.grid<to]
dens.between = dens.all[x.grid>from&x.grid<to]
polygon(c(x.between,rev(x.between)),c(rep(0,length(x.between)),rev(dens.between)),col=color,density=dens)
}
}
# TEST THE FUNCTION
shadet(df = 4, pcts = c(0.025,0.975)) # see the area under the tails are further away from the mean 0..
# shadet(df = 120, pcts = c(0.025,0.975)) # t dist converges to normal when we have high degrees o freedom..
shadechi = function(below=NULL, above=NULL, pcts = c(0.025,0.975), df=1, numpts = 500, color = "gray", dens = 40, justabove= FALSE, justbelow = FALSE, lines=FALSE,between=NULL,outside=NULL){
if(is.null(between)){
below = ifelse(is.null(below), qchisq(pcts[1],df), below)
above = ifelse(is.null(above), qchisq(pcts[2],df), above)
}
if(is.null(outside)==FALSE){
below = min(outside)
above = max(outside)
}
lowlim = 0
uplim = qchisq(.99,df)
x.grid = seq(lowlim,uplim, length= numpts)
dens.all = dchisq(x.grid,df)
if(lines==FALSE){
plot(x.grid, dens.all, type="l", xlab="X", ylab="Density")
}
if(lines==TRUE){
lines(x.grid,dens.all)
}
if(justabove==FALSE){
x.below = x.grid[x.grid<below]
dens.below = dens.all[x.grid<below]
polygon(c(x.below,rev(x.below)),c(rep(0,length(x.below)),rev(dens.below)),col=color,density=dens)
}
if(justbelow==FALSE){
x.above = x.grid[x.grid>above]
dens.above = dens.all[x.grid>above]
polygon(c(x.above,rev(x.above)),c(rep(0,length(x.above)),rev(dens.above)),col=color,density=dens)
}
if(is.null(between)==FALSE){
from = min(between)
to = max(between)
x.between = x.grid[x.grid>from&x.grid<to]
dens.between = dens.all[x.grid>from&x.grid<to]
polygon(c(x.between,rev(x.between)),c(rep(0,length(x.between)),rev(dens.between)),col=color,density=dens)
}
}
# TEST THE FUNCTION
shadechi(df = 2, pcts=c(.05)) # change pcts and see what happen
shadechi(df = 18, pcts=c(.05)) # change df and see what happens
Using traditional methods, it takes 109 hours to receive a basic driving license. A new license training method using Computer Aided Instruction (CAI) has been proposed. A researcher used the technique with 190 students and observed that they had a mean of 110 hours. Assume the standard deviation is known to be 6. A level of significance of 0.05 will be used to determine if the technique performs differently than the traditional method. Make a decision to reject or fail to reject the null hypothesis. Show all work in R.
Given: \(\mu=109, n=190, \bar{x}=110, \sigma=6, \alpha=.05\).
To Do: Determine if the technique performs differently than the traditional method. Burden of proof falls on alternative hypothesis.
#input given values
mu <- 109
xbar <- 110
sigma <- 6
n <- 190
alpha <- 0.05
#calc z score
z <- (xbar - mu) / (sigma / sqrt(n))
#two -tailed p-value
p_value <- 2 * (1 - pnorm(abs(z)))
cat("Z =", z, "\n","P-value =", p_value,"\n",myp(p_value,alpha))
## [1] "REJECT Ho"
## Z = 2.297341
## P-value = 0.0215993
## REJECT Ho
We can reject the null hypothesis, which means that we can conclude that the CAI method performs differently than the traditional one.
Our environment is very sensitive to the amount of ozone in the upper atmosphere. The level of ozone normally found is 5.3 parts/million (ppm). A researcher believes that the current ozone level is at an insufficient level. The mean of 5 samples is 5.0 parts per million (ppm) with a standard deviation of 1.1. Does the data support the claim at the 0.05 level? Assume the population distribution is approximately normal.
Given: \(\mu=5.3, n=5, \bar{x}=5, \sigma=1.1, \alpha=.05\).
To Do: Researcher believes that the current ozone level is at an insufficient level - does the data support the claim at the 0.05 level?
#input given values
mu <- 5.3
xbar <- 5
sigma <- 1.1
n <- 5
alpha <- 0.05
#z-score
z <- (xbar - mu) / (sigma / sqrt(n))
#insufficient = less than = one-tailed p-value
p_value <- pnorm(z)
cat("Q2: Z =", z,"\n", "P-value =", p_value,"\n", myp(p_value, alpha), "\n")
## [1] "FAIL 2 REJECT"
## Q2: Z = -0.6098367
## P-value = 0.270985
## FAIL 2 REJECT
At the 0.05 significance level we fail to reject the null hypothesis that the current ozone level is at an insufficient level.
Our environment is very sensitive to the amount of ozone in the upper atmosphere. The level of ozone normally found is 7.3 parts/million (ppm). A researcher believes that the current ozone level is not at a normal level. The mean of 51 samples is 7.1 ppm with a variance of 0.49. Assume the population is normally distributed. A level of significance of 0.01 will be used. Show all work and hypothesis testing steps.
Given: \(\mu=7.3, n=51, \bar{x}=7.1, \sigma^2=0.49, \alpha=.01\).
To Do: Researcher believes that the current ozone level is not at normal level. Thus, set a double sided hypothesis.
#input given values
mu <- 7.3
xbar <- 7.1
sigma <- sqrt(0.49)
n <- 51
alpha <- 0.01
#z-score
z <- (xbar - mu) / (sigma / sqrt(n))
#two-tailed p value
p_value <- 2 * (1 - pnorm(abs(z)))
cat("Q3: Z =", z,"\n", "P-value =", p_value, "\n", myp(p_value,alpha), "\n")
## [1] "FAIL 2 REJECT"
## Q3: Z = -2.040408
## P-value = 0.04130969
## FAIL 2 REJECT
The p-value is less than 0.01, so we can reject the null hypothesis. The current ozone level is significantly different than normal.
A publisher reports that 36% of their readers own a laptop. A marketing executive wants to test the claim that the percentage is actually less than the reported percentage. A random sample of 100 found that 29% of the readers owned a laptop. Is there sufficient evidence at the 0.02 level to support the executive’s claim? Show all work and hypothesis testing steps.
Given: \(\pi=.36, n=100, \hat{p}=.29,\alpha=.02\).
To Do: Executive wants to test the claim that the percentage is actually less than the reported percentage. Thus, set a single sided hypothesis.
# input given values
pi <- 0.36
phat <- 0.29
n <- 100
alpha <- 0.02
#z-score for proportions
z <- (phat - pi) / sqrt(pi * (1 - pi) / n)
#less than = one tailed p value
p_value <- pnorm(z)
cat("Q4: Z =", z, "\n","P-value =", p_value,"\n",myp(p_value, alpha), "\n")
## [1] "FAIL 2 REJECT"
## Q4: Z = -1.458333
## P-value = 0.07237434
## FAIL 2 REJECT
A hospital director is told that 31% of the treated patients are uninsured. The director wants to test the claim that the percentage of uninsured patients is less than the expected percentage. A sample of 380 patients found that 95 were uninsured. Make the decision to reject or fail to reject the null hypothesis at the 0.05 level. Show all work and hypothesis testing steps.
Given: \(\pi=.31, n=380, \hat{p}=\dfrac{95}{380}=.25,\alpha=.05\).
To Do: Researcher believes that the current ozone level is not at
normal level. Thus, set a double sided hypothesis.
# Question 5: One-sample proportion test
pi <- 0.31
phat <- 95 / 380
n <- 380
alpha <- 0.05
#z-score
z <- (phat - pi) / sqrt(pi * (1 - pi) / n)
#calc p value
p_value <- pnorm(z)
cat("Q5: Z =", z,"\n", "P-value =", p_value,"\n", myp(p_value, alpha), "\n")
## [1] "REJECT Ho"
## Q5: Z = -2.528935
## P-value = 0.005720462
## REJECT Ho
We can reject the null hypothesis.
A medical researcher wants to compare the pulse rates of smokers and non-smokers. He believes that the pulse rate for smokers and non-smokers is different and wants to test this claim at the 0.1 level of significance. The researcher checks 32 smokers and finds that they have a mean pulse rate of 87, and 31 non-smokers have a mean pulse rate of 84. The standard deviation of the pulse rates is found to be 9 for smokers and 10 for non-smokers. Let \(\mu_1\) be the true mean pulse rate for smokers and \(\mu_2\) be the true mean pulse rate for non-smokers. Show all work and hypothesis testing steps.
Let smoker group be indexed by 1, non-smoker group by 2.
Given: \(n_1 = 32, \mu_1 = 87, n_2 = 31, \mu_2
= 84, \sigma_1 = 9, \sigma_2 = 10 , \alpha = 10\%\).
To Do: Test if the pulse rate for smokers and non-smokers is different at the 0.1 level of significance. Thus, double sided test.
# input given values
xbar1 <- 87
xbar2 <- 84
sigma1 <- 9
sigma2 <- 10
n1 <- 32
n2 <- 31
alpha <- 0.10
#z-score for difference in means
z <- (xbar1 - xbar2) / sqrt((sigma1^2 / n1) + (sigma2^2 / n2))
#two tailed p-value
p_value <- 2 * (1 - pnorm(abs(z)))
cat("Q6: Z =", z, "\n","P-value =", p_value, "\n",myp(p_value, alpha), "\n")
## [1] "FAIL 2 REJECT"
## Q6: Z = 1.25032
## P-value = 0.2111829
## FAIL 2 REJECT
Q6:
Z = 1.25032
P-value = 0.2111829
The P value is greater than our alpha so we fail to reject the null hypothesis. The pulse rates are different at this significance level.
Given two independent random samples with the following results: \(n_1=11, \bar{x}_1=127, \sigma_1=33, n_2=18, \bar{x}_2=157, \sigma_2=27\)
Use this data to find the 95% confidence interval for the true difference between the population means. Assume that the population variances are not equal and that the two populations are normally distributed.
To Do: Create a 95% confidence interval for true difference between the population means.
# input given values
xbar1 <- 127
xbar2 <- 157
sigma1 <- 33
sigma2 <- 27
n1 <- 11
n2 <- 18
alpha <- 0.05
#standard error calc
se <- sqrt((sigma1^2 / n1) + (sigma2^2 / n2))
#degrees of freedom
df <- min(n1 - 1, n2 - 1)
#critical t value calc
t_crit <- qt(1 - alpha/2, df)
#margin of error = critical t times std error
margin <- t_crit * se
#confidence interval
lower <- (xbar1 - xbar2) - margin
upper <- (xbar1 - xbar2) + margin
cat("Q7: CI = [", lower, ",", upper, "]\n")
## Q7: CI = [ -56.31657 , -3.683426 ]
The 95% confidence interval for true difference between the population means:
CI = [ -56.31657 , -3.683426 ]
Two men, A and B, who usually commute to work together decide to conduct an experiment to see whether one route is faster than the other. The men feel that their driving habits are approximately the same, so each morning for two weeks one driver is assigned to route I and the other to route II. The times, recorded to the nearest minute, are shown in the following table. Using this data, find the 98% confidence interval for the true mean difference between the average travel time for route I and the average travel time for route II.
r1 = c (32, 27, 34, 24, 31, 25, 30, 23, 27, 35)
r2 = c (28, 28, 33, 25, 26, 29, 33, 27, 25, 33)
Let \(d1 =\) (route I travel time) − (route II travel time).
Assume that the populations of travel times are normally distributed for both routes. Show all work and hypothesis testing steps.
To Do: Find the 98% confidence interval for the true mean difference between the average travel time for route I and the average travel time for route II.
# Step 1: Input commute times for Route I and Route II
r1 <- c(32, 27, 34, 24, 31, 25, 30, 23, 27, 35)
r2 <- c(28, 28, 33, 25, 26, 29, 33, 27, 25, 33)
# Step 2: Calculate differences
d <- r1 - r2
# Step 3: Compute sample statistics
n <- length(d)
mean_diff <- mean(d)
sd_diff <- sd(d)
# Step 4: Set confidence level and find critical t-value
alpha <- 0.02
df <- n - 1
t_crit <- qt(1 - alpha/2, df)
# Step 5: Calculate margin of error
margin <- t_crit * sd_diff / sqrt(n)
# Step 6: Construct confidence interval
lower <- mean_diff - margin
upper <- mean_diff + margin
# Step 7: Output the result
cat("98% Confidence Interval for mean difference:", round(lower, 2), "to", round(upper, 2), "\n")
## 98% Confidence Interval for mean difference: -2.77 to 2.97
The 98% confidence interval for mean difference is from -2.77 to 2.97 minutes
The U.S. Census Bureau conducts annual surveys to obtain information on the percentage of the voting-age population that is registered to vote. Suppose that 391 employed persons and 510 unemployed persons are independently and randomly selected, and that 195 of the employed persons and 193 of the unemployed persons have registered to vote. Can we conclude that the percentage of employed workers (p1) who have registered to vote, exceeds the percentage of unemployed workers (p2) who have registered to vote? Use a significance level of 0.05 for the test. Show all work and hypothesis testing steps.
Q: Can we conclude that the percentage of employed workers (p1) who have registered to vote, exceeds the percentage of unemployed workers (p2) who have registered to vote?
#input given values
x1 <- 195
n1 <- 391
x2 <- 193
n2 <- 510
p1 <- x1 / n1
p2 <- x2 / n2
alpha <- 0.05
# Pooled proportion calc
p_pool <- (x1 + x2) / (n1 + n2)
# standard error calc
se <- sqrt(p_pool * (1 - p_pool) * (1/n1 + 1/n2))
# Calculate z-score
z <- (p1 - p2) / se
# "exceeds"= greater than = One-tailed p-value
p_value <- 1 - pnorm(z)
myp(p_value, alpha)
## [1] "REJECT Ho"
We can reject the null hypothesis, that the percentage of employed workers (p1) who have registered to vote, exceeds the percentage of unemployed workers (p2) who have registered to vote.