Homework 5, Data Analysis

Resources :

  1. 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.

  2. 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.

Set Up (4 functions) to better answer the questions.

I. Function to Reject or Not

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,…

II. Function for Shading Normal

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))

III. Function for for Shading t

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..

IV. Function for Shading Chi Square

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   

Question 1 (In class Lecture notes)

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.

mu <- 109
n<-190
x<-110
sigma<-6
alpha<-.05
z <- (x - mu) / (sigma / sqrt(n))
critical_value<-qnorm(1 - alpha/2)
p1<-2 * (1 - pnorm(z,0,1))
print(z)
## [1] 2.297341
print(critical_value)
## [1] 1.959964
print(p1)
## [1] 0.0215993
myp(p1,alpha)
## [1] "REJECT Ho"

Since p value less than alpha, it reject the null hypothesis,the technique performs differently than the traditional method.

Question 2 (Lecture notes)

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?

mu <- 5.3
n<-5
x<-5
sigma<-1.1
alpha<-.05
t2 <- (x - mu) / (sigma / sqrt(n))
p2<-2 * (pnorm(t2,0,1))
print(t2)
## [1] -0.6098367
print(p2)
## [1] 0.54197
myp(p2,alpha)
## [1] "FAIL 2 REJECT"

Since p value is greater than alpha, it fail to reject the null hypothesis.

Question 3 (Lecture notes)

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.

mu <- 7.3
n<-51
x<-7.1
sigma<-sqrt(.49)
alpha<-.01
t3 <- (x - mu) / (sigma / sqrt(n))
p3<-2 * (pnorm(t3,0,1))
print(t3)
## [1] -2.040408
print(p3)
## [1] 0.04130969
myp(p3,alpha)
## [1] "FAIL 2 REJECT"

Since p value is greater than alpha, it fail to reject the null hypothesis.

Question 4 (See Open Stats Textbook - Chapter 5 Section 5.2: Confidence intervals for a proportion)

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.

pi <- 0.36    
n <- 100
p <- 0.29 
alpha <- 0.02
z <- (p - pi) / sqrt(pi * (1 - pi) / n)
p4<-pnorm(z)
critical_value = qnorm(alpha)
print(z)
## [1] -1.458333
print(critical_value)
## [1] -2.053749
print(p4)
## [1] 0.07237434
myp(p4,alpha)
## [1] "FAIL 2 REJECT"

Since p value is greater than alpha, it fail to reject the null hypothesis.

Question 5 (See Open Stats Textbook - Chapter 5)

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.

pi <- 0.31    
n <- 380
p <- 0.25 
alpha <- 0.05
z <- (p - pi) / sqrt(pi*(1-pi)/n)
p5<-pnorm(z)
critical_value = qnorm(alpha)
print(z)
## [1] -2.528935
print(critical_value)
## [1] -1.644854
print(p5)
## [1] 0.005720462
myp(p5,alpha)
## [1] "REJECT Ho"

Since p value less than alpha, it reject the null hypothesis

Question 6 (See Open Stats Section 7.3, Example 7.25 in particular)

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.

n1 <- 32
mu1<-87
n2<-31
mu2<-84
sigma1<-9
sigma2<-10
alpha<-.01
t6<-(mu1 - mu2)/sqrt((sigma1^2 / n1) + (sigma2^2 / n2))
df1<-((sigma1^2 / n1) + (sigma2^2 / n2))^2
df2<-(((sigma1^2 / n1)^2)/(n1-1)) + ((sigma2^2 / n2)^2/(n2-1))
df<-df1/df2
psmoke <- 2*(1- pt(t6, df))
print(t6)
## [1] 1.25032
print(psmoke)
## [1] 0.2160473
myp(psmoke,alpha)
## [1] "FAIL 2 REJECT"
pnon<-2*(1-pt(t6, min(n1-1,n2-1)))
print(pnon)
## [1] 0.220848
myp(pnon,alpha)
## [1] "FAIL 2 REJECT"

Since p value for smokers and non-smokers is greater than alpha, it fail to reject the null hypothesis.

Question 7 (See Open Stats Section 7.3, Example 7.22 in particular)

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.

n1 <- 11
x1<-127
n2<-18
x2<-157
sigma1<-33
sigma2<-27
alpha<-.05
df1<-((sigma1^2 / n1) + (sigma2^2 / n2))^2
df2<-(((sigma1^2 / n1)^2)/(n1-1)) + ((sigma2^2 / n2)^2/(n2-1))
df<-df1/df2
SE<-sqrt(sigma1^2/n1+sigma2^2/n2)
PE<-x1-x2
t7<-qt(.975,df)
print(t7)
## [1] 2.10029
lower<-PE-(t7*SE)
upper<-PE+(t7*SE)
cat("the 95% confidence interval for the true difference between", lower ,"to", upper)
## the 95% confidence interval for the true difference between -54.80655 to -5.193452

Question 8 (See Open Stats Section 6.2 Difference of two proportions, Example 6.2.2 in particular)

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.

r1 <- c(32, 27, 34, 24, 31, 25, 30, 23, 27, 35)
r2 <- c(28, 28, 33, 25, 26, 29, 33, 27, 25, 33)
n1 <- length(r1)
x1<-mean(r1)
n2<-length(r2)
x2<-mean(r1)
sigma1<-sd(r1)
sigma2<-sd(r2)
alpha<-0.02
SE<-sqrt(sigma1^2/n1+sigma2^2/n2)
d1<-mean(r1-r2)
print(d1)
## [1] 0.1
DF<-length(r1-r2)-1
t1<- qt(1 - alpha / 2, DF)
lower1<-d1-(t1*SE)
upper1<-d1+(t1*SE)
cat("the 98% confidence interval for the true mean difference for route I between", lower1 ,"to", upper1)
## the 98% confidence interval for the true mean difference for route I between -4.637066 to 4.837066
df1<-((sigma1^2 / n1) + (sigma2^2 / n2))^2
df2<-(((sigma1^2 / n1)^2)/(n1-1)) + ((sigma2^2 / n2)^2/(n2-1))
df<-df1/df2
t2<- qt(1 - alpha / 2, df)
lower2<-d1-(t2*SE)
upper2<-d1+(t2*SE)
cat("the 98% confidence interval for the true mean difference for route II between", lower2 ,"to", upper2)
## the 98% confidence interval for the true mean difference for route II between -4.213039 to 4.413039

Question 9 (See Open Stats Textbook - Chapter 5 Section 5.2-5.33: Confidence intervals/Hypothesis testing for a proportion)

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?

n1 <- 391  
n2 <- 510  
x1 <- 195  
x2 <- 193  
alpha=0.05
p1 <- x1/n1
p2 <- x2/n2
SE <- sqrt((p1*(1-p1)/n1) + (p2*(1-p2)/n2))
z<- (p1-p2)/SE
p9<- pnorm(z, lower.tail = FALSE)
print(p9)
## [1] 0.0001439855
myp(p9,alpha)
## [1] "REJECT Ho"

Since p value less than alpha, it reject the null hypothesis.