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 -

Hypothesis Test:

  1. H0 - The mean training time for the CAI method is the same as the traditional method.

  2. Ha - The mean training time for the CAI method differs from the traditional method.

#defining parameters of interest 
mu <- 109 #population mean
xbar <- 110 #sample mean
n <- 190 #sample size
sigma <- 6 #standard deviation
alpha <- .05 #level of significance
#calculating standard error
se <- sigma/sqrt(n)

#calculating z-score
z <- (xbar - mu)/se

#calculating p-value
p.value <- 2 *(1-pnorm(abs(z)))
print(p.value)
## [1] 0.0215993
#drawing conclusion
myp(p.value, alpha)
## [1] "REJECT Ho"

Interpretation: At a significance level of .05, we have strong evidence supporting the alternative hypothesis - the newly introduced CAI technique performs differently than the traditional training 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?

Hypothesis Test:

  1. H0 - The ozone level is sufficient.

  2. Ha - The ozone level is insufficient.

#defining parameters of interest
mu <- 5.3 #population mean
xbar <- 5 #sample mean
n <- 5 #sample size
sigma <- 1.1 #standard deviation
alpha <- .05 #level of significance 
#calculating t statistic
t.stat <- (xbar - mu)/(sigma/sqrt(n))

#determining degrees of freedom
df <- n - 1

#calculating p-value
p.value <- 2 * pt(-abs(t.stat), df = df) #two-tail test, multiply by two
print(p.value)
## [1] 0.5749137
#drawing conclusion
myp(p.value, alpha)
## [1] "FAIL 2 REJECT"

Interpretation: At a significance level of .05, we do not have sufficient evidence to reject the null hypothesis - the data suggests the current ozone level is not insufficient.

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.

Hypothesis Test:

  1. H0 - The current ozone level is “normal.”

  2. Ha - The current ozone level is “not normal.”

#defining parameters of interest
mu <- 7.3 #population mean
n <- 51 #sample size
xbar <- 7.1 #sample mean
sigma <- sqrt(.49) #standard deviation
alpha <- .01 #level of significance
#calculating t statistic
t.stat <- (xbar - mu)/(sigma/sqrt(n))

#determining degrees of freedom
df <- n - 1

#calculating p-value
p.value <- 2 * pt(-abs(t.stat), df = df) #two-tail test, multiply by two
print(p.value)
## [1] 0.04660827
#drawing conclusion
myp(p.value, alpha)
## [1] "FAIL 2 REJECT"

Interpretation:At a significance level of .01, we do not have sufficient evidence to reject the null hypothesis - the data suggests the current ozone level is not at an abnormal level.

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.

Hypothesis Test:

  1. H0 - The percentage of readers who own a laptop is at least equal to 36%

  2. Ha - The percentage of readers who own a laptop is less than 36%.

#defining parameters of interest
pi <- .36 #population parameter
n <- 100 #sample size
p.hat <- .29 #point estimate
alpha <- .02 #level of significance
#calculating standard error
se <- sqrt((pi * (1-pi))/n)

#calculating z-score
z <- (p.hat - pi)/se

#calculating p-value
p.value <- pnorm(z)
print(p.value)
## [1] 0.07237434
#drawing conclusion
myp(p.value, alpha)
## [1] "FAIL 2 REJECT"

Interpretation:At a significance level of .02, we do not have sufficient evidence to reject the null hypothesis - the data suggests that the percentage of readers who own a laptop is not less than the reported percentage

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

Hypothesis Test:

  1. H0 - The percentage of uninsured patients is at least equal to 31%.

  2. Ha - The percentage of uninsured patients is less than 31%.

#determining parameters of interest
pi <- .31 #population parameter
n <- 380 #sample size
p.hat <- 95/n #point estimate
alpha <- .05 #level of significance 
#determining if success/failure conditions are met 
condition.1 <- n*pi
condition.2 <- n*(1-pi)

if (n*pi >= 10 & n*(1-pi) >= 10) {
  print("Yes")
}
## [1] "Yes"
#calculating standard error
se <- sqrt((pi * (1-pi))/n)

#calculating z-score
z <- (p.hat - pi)/se

#calculating p-value
p.value <- pnorm(z)
print(p.value)
## [1] 0.005720462
#drawing conclusion
myp(p.value, alpha)
## [1] "REJECT Ho"

Interpretation: At a significance level of .05, we have strong evidence supporting the alternative hypothesis - the percentage of uninsured patients is not at least equal to the expected percentage.

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 = 32, \mu_2 = 84, \sigma_1 = 9, \sigma_2 = 10 , \alpha = .01\).

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.

Hypothesis Test:

  1. H0 - There is no difference in pulse rate between smokers and non-smokers.

  2. Ha - There is a difference in pulse rate between smokers and non-smokers.

#defining parameters of interest 
n.1 <- 32 #sample size of smokers group
mu.1 <- 87 #mean of smokers group
sigma.1 <- 9 #sd of smokers group

n.2 <- 31 #sample size of non-smokers group
mu.2 <- 84 #mean of non-smokers group
sigma.2 <- 10 #sd of non-smokers group

alpha <- .1 #level of significance 
#calculating variance
var.1 <- sigma.1^2
var.2 <- sigma.2^2

#calculating standard error
se <- sqrt((var.1/n.1)+(var.2/n.2))

#calculating test statistic
t <- (mu.1 - mu.2)/se

#determining degrees of freeedom
df.1 <- n.1 - 1
df.2 <- n.2 - 1
df <- min(df.1, df.2) #taking the smaller of the two df

#calculating p-value
p.value <- 2*pt(t, df = df, lower.tail = FALSE)
print(p.value)
## [1] 0.220848
#drawing conclusion
myp(p.value, alpha)
## [1] "FAIL 2 REJECT"

Interpretation:At a significance level of .1, we do not have sufficient evidence to reject the null hypothesis - the data suggests there is not a difference in pulse rate between smoker and non-smokers.

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.

#defining parameters of interest
n.1 <- 11 #sample size 1
xbar.1 <- 127 #sample mean 1
sigma.1 <- 33 #sd 1

n.2 <- 18 #sample size 2
xbar.2 <- 157 #sample mean 2
sigma.2 <- 27 #sd 2
#calculating difference in sample means 
s.diff <- xbar.1 - xbar.2

#calculating variance
var.1 <- sigma.1^2
var.2 <- sigma.2^2

#calculating degrees of freedom
df.1 <- n.1 - 1 
df.2 <- n.2 -1
df <- min(df.1, df.2) #taking the smaller of the two df

#calculating standard error
se <- sqrt(var.1/n.1+var.2/n.2)

#constructing interval
upper <- s.diff + qt(.975, df = df) * se
lower <- s.diff - qt(.975, df = df) * se 

ci <- c(lower, upper)
print(ci)
## [1] -56.316574  -3.683426

Interpretation: We are 95% confident that the true difference between the population means falls between -56.31 and -3.68

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.

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.

route.1 <- c(32, 27, 34, 24, 31, 25, 30, 23, 27, 35)
route.2 <- c(28, 28, 33, 25, 26, 29, 33, 27, 25, 33)

route.1 <- as.numeric(route.1)
route.2 <- as.numeric(route.2)

#defining parameters of interest
n.1 <- length(route.1) #sample size of route 1
xbar.1 <- mean(route.1) #sample mean of route 1
sigma.1 <- sd(route.1) #sd of route 1

n.2 <- length(route.2) #sample size of route 2
xbar.2 <- mean(route.2) #sample mean of route 2
sigma.2 <- sd(route.2) #sd of route 2

alpha <- 1 - .98 #level of significance 
#calculating difference in means
d1 <- xbar.1 - xbar.2

#calculating variance
var.1 <- sigma.1^2
var.2 <- sigma.2^2

#calculating degrees of freedom
df.1 <- n.1 - 1 
df.2 <- n.2 -1
df <- min(df.1, df.2)

#calculating standard error
se <- sqrt(var.1/n.1+var.2/n.2)

#constructing interval
upper <- d1 + qt(p = alpha/2, df = df, lower.tail = FALSE) * se
lower <- d1 - qt(p = alpha/2, df = df, lower.tail = FALSE) * se

ci <- c(lower, upper)
print(ci)
## [1] -4.637066  4.837066

Interpretation: We are 98% confident that the true mean difference between the average travel time for route 1 and route 2 falls between -4.64 and 4.84.

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?

Hypothesis Test:

  1. H0 - The percentage of employed workers registered to vote is equal to or less than the percentage of unemployed workers registered to vote.

  2. Ha - The percentage of employed workers registered to vote exceeds the percentage of unemployed workers registered to vote.

#defining parameters of interest
n.1 <- 391 #sample size; employed
x.1 <- 195 #registered voters; employed
p.1 <- x.1 / n.1 #sample proportion; employed 
  
n.2 <- 510 #sample size; unemployed 
x.2 <- 193 #registered voters; unemployed
p.2 <- x.2 / n.2  #sample proportion; unemployed 
  
alpha <- .05 #level of significance 
#calculating difference in proportions
diff <- p.1 - p.2

#calculating standard error
se <- sqrt(p.1 * (1 - p.1) / n.1 + p.2 * (1 - p.2) / n.2)

#calculating test statistic
z <- diff / se

#calculating p-value
p.value <- pnorm(z, lower.tail = FALSE)
print(p.value)
## [1] 0.0001439855
#drawing conclusion 
myp(p.value, alpha)
## [1] "REJECT Ho"

Interpretation: At a significance level of .05, we have strong evidence supporting the alternative hypothesis - the percentage of employed workers registered to vote is not less than or equal to the percentage of unemployed workers registered to vote.