1. In the Flight Delays Case Study in Section 1.1,

    1. The data contain flight delays for two airlines, American Airlines and United Airlines. Conduct a two-sided permutation test to see if the mean delay times between the two carriers are statistically significant.

    2. The flight delays occured in May and June of 2009. Conduct a two-sided permutation test to see if the difference in mean delay times between the 2 months is statistically significant.

# a. 
FD <- read.csv("http://www1.appstate.edu/~arnholta/Data/FlightDelays.csv")
library(tidyverse)


FDA <- FD %>%
  group_by(Carrier) %>%
  summarize(Mean=mean(Delay),n=n()) %>%
  summarize(obs_diff = diff(Mean))    
FDA
# A tibble: 1 x 1
  obs_diff
     <dbl>
1 5.885696
sims <- 10^4 - 1
md <- numeric(sims)
for(i in 1:sims) {
  index <- sample(4029, 1123, replace=FALSE)
  md[i] <- mean(FD$Delay[index]) - mean(FD$Delay[-index])
}

pval <- 2 * ((sum(md >= FDA$obs_diff) + 1) / (sims + 1)) 
pval
[1] 2e-04

Given that our p-value is below 0.05, we find the difference in mean delay times of each carrier is statistically significant. This suggests on average United Airline delays are longer than American Airline delays.

# b. 
FD %>%
  group_by(Month) %>%
  summarize(Mean = mean(Delay), n = n()) %>%
  summarize(obs_diff = Mean[1] - Mean[2])
# A tibble: 1 x 1
  obs_diff
     <dbl>
1 5.663341
obs_diff <- 5.663341

sims <- 10^4 - 1
md <- numeric(sims)
for (i in 1:sims) {
  index <- sample(4029, 1999, replace = FALSE)  
  md[i] <- mean(FD$Delay[index]) - mean(FD$Delay[-index])
}

pval <- 2 * ((sum(md >= obs_diff) + 1) / (sims+1))
pval
[1] 2e-04

Given our p-value is below 0.05 we find the difference in mean delay times is statistically significant. This suggest that on average the delay times in the month of June are longer than the delay times in the month of May.

  1. In the Flight Delays Case Study in Section 1.1, the data contain flight delays for two airlines, American Airlines and United Airlines.

    1. Compute the proportion of times that each carrier’s flights was delayed more than 20 minutes. Conduct a two-sided test to see if the difference in these proportions is statistically significant.

    2. Compute the variance in the flight delay lengths for each carrier. Conduct a test to see if the variance for United Airlines is greater than that of American Airlines.

# a. 
out <- FD %>% 
  filter(Delay > 20) %>%
  group_by(Carrier) %>%
  summarize(Mean = mean(Delay)) %>%
  summarize(diff = Mean[2] / Mean[1])

md <- numeric(sims)
for(i in 1:sims) {
  index <- sample(731, 239, replace = FALSE)
  md[i] <- mean(FD$Delay[index] > 20) / mean(FD$Delay[-index] > 20)
}

pval <- 2 * ((sum(md >= (out * -1)) + 1) / (sims + 1))
pval
[1] 4e-04

Given the p-value is below 0.05, the difference in delay time proportions in statistically significant. This would suggest that United Airlines has a higher proportion of delays over 20 min.

# b. 
FD %>%
  group_by(Carrier) %>%
  summarize(Var = var(Delay), n = n())  %>%
  summarize(var_diff = Var[2] - Var[1])
# A tibble: 1 x 1
  var_diff
     <dbl>
1 431.0677
sims <- 10^4 - 1
var <- numeric(sims)
for (i in 1:sims) {
  index <- sample(4023, 1999, replace = FALSE) # sample(n, nUA) 
  var[i] <- var(FD$Delay[index]) - # values correspond to nUA
    var(FD$Delay[-index]) # values correspond to nAA
}
pval <- 2*((sum(var >= FD$var_diff) + 1) / (sims + 1))
pval
[1] 2e-04
  1. In the Flight Delays Case Study in Section 1.1, repeat Exercise 3 part (a) using three test statistics: (i) the mean of the United Airlines delay times, (ii) the sum of the United Airlines delay times, and (iii) the difference in the means, and compare the P-values. Make sure all three test statistics are computed within the same for loop.
# code here
FD%>% 
  group_by(Carrier) %>%
  summarize(n = n())
# A tibble: 2 x 2
  Carrier     n
   <fctr> <int>
1      AA  2906
2      UA  1123
out <- FD%>% 
  group_by(Carrier) %>%
  summarize(Mean = mean(Delay)) %>%
  summarize(obs = mean(Mean[2] + Mean[1]))

out_sum <- FD%>% 
  group_by(Carrier) %>%
  summarize(Sum = sum(Delay)) %>%
  summarize(obs_sum = Sum[2] + Sum[1])

out_diff <- FD%>% 
  group_by(Carrier) %>%
  summarize(Mean = mean(Delay)) %>%
  summarize(obs_diff = Mean[2] - Mean[1])



sim <- 10^4-1
md1 <- numeric(sim)
md2 <- numeric(sim)
md3 <- numeric(sim)
for(i in 1:sim)
{
  index <-sample(4029,1123, replace = FALSE)
  md1[i] <- mean(mean(FD$Delay[index]) + mean(FD$Delay[-index]))
  md2[i] <- sum((FD$Delay[index]) + (FD$Delay[-index]))
  md3[i] <- mean(FD$Delay[index]) - mean(FD$Delay[-index])
}

pval <- 2 * ((sum(md1 >= (out * -1)) + 1) / (sim + 1))
pval
[1] 4e-04
pval <- 2 * ((sum(md2 >= (out_sum * -1)) + 1) / (sim + 1)) #two-side test
pval
[1] 4e-04
pval_diff <- 2 * ((sum(md3 >= (out_diff * -1)) + 1) / (sim + 1)) #two-side test
pval_diff
[1] 4e-04

The p-value is below 0.05 for the mean delay times, thus is statistically significant. The other p-values for the differences also suggest the difference in sum and means is also statistically significant.

  1. In the Flight Delays Case Study in Section 1.1,

    1. Find the 25% trimmed mean of the delay times for United Airlines and American Airlines.

    2. Conduct a two-sided test to see if the difference in trimmed means is statistically significant.

# code here
out <- FD %>% 
  group_by(Carrier) %>%
  summarize(Mean = mean(Delay, trim = 0.25)) %>%
  summarize(obs = Mean[2] - Mean[1])

#Two sided test
md <- numeric(sims)
for(i in 1:sims) {
  index <- sample(4029,1123, replace = FALSE)
  md[i] <- mean(FD$Delay[index] > 20 , trim = 0.25) - mean(FD$Delay[-index] > 20 , trim = 0.25)
}

pvalue <- 2 * ((sum(md >= (out * -1) + 1) / (sims + 1))) 
pvalue
[1] 2e-04

The p-value is below 0.05 for the means, so the difference in trimmed means is statistically significant. After trimming 25%, the mean delay times of United Airlines are shorter than the mean delay times of American Airlines.

  1. In the Flight Delays Case Study in Section 1.1,

    1. Compute the proportion of times the flights in May and in June were delayed more than 20 min, and conduct a two-sided test of whether the difference between months is statistically significant.

    2. Compute the variance of the flight delay times in May and June and then conduct a two-sided test of whether the ratio of variances is statistically significantly different from 1.

# a. 
out <- FD %>% 
  filter(Delay > 20) %>%
  filter(Month == "June" || Month == "May") %>%
  group_by(Month) %>%
  summarize(Mean = mean(Delay)) %>%
  summarize(diff = Mean[2] - Mean[1])

md <- numeric(sims)
may <- subset(FD, select = Delay, subset = Month == "May", drop = TRUE)
june <- subset(FD, select = Delay, subset = Month == "June", drop = TRUE)
mo <- c(may, june)

for(i in 1:sims) {
  index <-sample(731,333, replace = FALSE)
  md[i] <- mean(mo[index] > 20) - mean(mo[-index] > 20)
}

pvalue <- 2 * ((sum(md >= (out * -1)) + 1) / (sims + 1)) 
pvalue
[1] 2e-04

The p-value is below 0.05 for the difference in flights declared more than 20 minutes in June, so it is statistically significant.

# b. 
FD %>% 
  filter(Delay > 20) %>% filter(Month == "June" || Month == "May") %>%
  group_by(Month) %>%
  summarize(n = n())
# A tibble: 2 x 2
   Month     n
  <fctr> <int>
1   June   398
2    May   333
out <- FD %>% 
  filter(Delay > 20) %>% filter(Month == "June" || Month == "May") %>%
  group_by(Month) %>%
  summarize(Mean = mean(Delay)) %>%
  summarize(diff = Mean[2] - Mean[1])

md <- numeric(sims)
may <- subset(FD, select = Delay, subset = Month == "May", drop = TRUE)
june <- subset(FD, select = Delay, subset = Month == "June", drop = TRUE)
mo <- c(may, june)

for(i in 1:sims) {
  index <- sample(731, 333, replace = FALSE)
  md[i] <- mean(mo[index] > 20) - mean(mo[-index] > 20)
}

pvalue <- ((sum(md >= (out * - 1)) + 1) / (sims + 1)) * 2
pvalue
[1] 2e-04

The p-value is below 0.05 for the difference in variance, so this suggests that the ratio of variance is not significantly different from 1.

  1. Research at the University of Nebraska conducted a study to investigate sex differences in dieting trends among a group of Midwestern college students (Davy et al. (2006)). Students were recruited from an introductory nutrition course during one term. Below are data from one question asked to 286 participants.

    1. Write down the appropriate hypothesis to test to see if there is a relationship between gender and diet and then carry out the test.

Null Hypothesis \({H_0}\): A low fat diet is independent of gender in Midwestern college students.

Alternative Hypothesis \({H_A}\): A low fat diet is dependent of gender in Midwestern college students.

  1. Can the results be generalized to a population? Explain.
       LowFatDiet
Gender  Yes  No
  Women  35 146
  Men     8  97
# code here
ODT <- as.table(DT)
ODTDF <- as.data.frame(ODT)
DDF <- as.tbl(vcdExtra::expand.dft(ODTDF))
Test <- xtabs(~Gender + LowFatDiet, data = DDF)
chisq.test(Test, correct = FALSE)

    Pearson's Chi-squared test

data:  Test
X-squared = 7.1427, df = 1, p-value = 0.007527

The p-value is less than 0.05 so we reject the null hypothesis. We find evidence to support the alternative hypothsis that diet is dependent on gender in Midwestern College students.

  1. A national polling company conducted a survey in 2001 asking a randomly selected group of Americans of 18 years of age or older whether they supported limited use of marijuana for medicinal purposes. Here is a summary of the data:

    Write down the appropriate hypothesis to test whether there is a relationship between age and support for medicinal marijuana and carry out the test.

                   Support
Age                 Against For
  18-29 years old        52 172
  30-49 years old       103 313
  50 years or older     119 258

Null Hypothesis \({H_0}\): There is not an association between age and support of medicinal marijuana.

Alternative Hypothesis \({H_A}\): There is an association between age and support of medicinal marijuana.

# code here
chisq.test(T1, correct = FALSE)

    Pearson's Chi-squared test

data:  T1
X-squared = 6.6814, df = 2, p-value = 0.03541

Since the p-value of 0.03541 is less than .05, we reject \({H_0}\) in favor of \({H_A}\).

  1. Two students went to a local supermarket and collected data on cereals; they classified by their target consumer (children versus adults) and the placement of the cereal on the shelf (bottom, middle, and top). The data are given in Cereals.

    1. Create a table to summarize the relationship between age of target consumer and shelf location.

    2. Conduct a chi-square test using R’s chisq.test command.

    3. R returns a warning message. Compute the expected counts for each cell to see why.

    4. Conduct a permutation test for independence.

Cereals <- read.csv("http://www1.appstate.edu/~arnholta/Data/Cereals.csv")
# a
T2 <- xtabs(~Age + Shelf, data = Cereals)
T2
          Shelf
Age        bottom middle top
  adult         2      1  14
  children      7     18   1
# b 
chisq.test(T2, correct = FALSE)

    Pearson's Chi-squared test

data:  T2
X-squared = 28.625, df = 2, p-value = 6.083e-07
# c
Cereals %>% 
  group_by(Age) %>%
  summarise(n = n())
# A tibble: 2 x 2
       Age     n
    <fctr> <int>
1    adult    17
2 children    26
# d
obs_stat_diff <- chisq.test(T2)$statistic
sims <- 10^4-1
md <- numeric(sims)
for (i in 1:sims) {
    T3 <- xtabs(~sample(Age) + Shelf, data = Cereals)
    md[i] <- chisq.test(T3)$statistic
}
pval <- (sum(md >= obs_stat_diff) + 1)/(sims + 1)
pval
[1] 1e-04

The p-value is less than 0.05, we reject the null hypothesis and can say there is a relationship between the placement of cereals in stores and the age group of targeted customers.

  1. From GSS 2002 Case Study in Section 1.6,

    1. Create a table to summarize the relationship between gender and the person’s choice for president in the 2000 election.

    2. Test to see if a person’s choice for president in the 2000 election is independent of gender (use chisq.test in R).

    3. Repeat the test but use the permutation test for independence. Does your conclusion change? (Be sure to remove observations with missing values)

GSS2002 <- read.csv("http://www1.appstate.edu/~arnholta/Data/GSS2002.csv")

#a
T4 <- xtabs(~Gender + Pres00, data = GSS2002)
T4
        Pres00
Gender   Bush Didnt vote Gore Nader Other
  Female  459          5  492    26     3
  Male    426          5  289    31    13
#b
chisq.test(T4, correct = FALSE)

    Pearson's Chi-squared test

data:  T4
X-squared = 33.29, df = 4, p-value = 1.042e-06

Since the p-value is smaller than 0.05, we can safely reject the null hypothesis showing dependence. There is a relationship between gender of the person and the person’s choice for the president of the 2000 election.

#c
obs_stat_diff <- chisq.test(T4)$statistic

md <- numeric(sims)
for (i in 1:sims) {
    T5 <- xtabs(~sample(Pres00) + Gender, data = GSS2002)
    md[i] <- chisq.test(T5)$statistic
}
pval <- (sum(md >= obs_stat_diff) + 1)/(sims + 1)
pval
[1] 1e-04

Since the p-value is still less than 0.05, my conclusion does not change from part c. We still reject the null hypothesis and conclude there is a relationship between gender of the person and the person’s choice for president.

  1. From GSS 2002 Case Study in Section 1.6,

    1. Create a table to summarize the relationship bewteen gender and the person’s general level of happiness (Happy).

    2. Conduct a permutation test to see if gender and level of happiness are independent (Be sure to remove the observations with missing values).

#a
T4 <- xtabs(~Gender + Happy, data = GSS2002)
T4
        Happy
Gender   Not too happy Pretty happy Very happy
  Female           109          406        205
  Male              61          378        210
#b
obs_stat_diff <- chisq.test(T4)$statistic

md <- numeric(sims)
for (i in 1:sims) {
    T6 <- xtabs(~sample(Happy) + Gender, data = GSS2002)
    md[i] <- chisq.test(T6)$statistic
}
pval <- (sum(md >= obs_stat_diff) + 1)/(sims + 1)
pval
[1] 0.0048

With the p-value being less than 0.05, we reject the null hypothesis and conclude that there is a relationship between gender of the person and the person’s level of happiness.

  1. From GSS 2002 Case Study in Section 1.6,

    1. Create a table to summarize the relationship between support for gun laws (GunLaw) and views on government spending on the military (SpendMilitary).

    2. Conduct a permutation test to see if support for gun laws and views on government spending on the military are independent (Be sure to remove observations with missing values).

#a
T5 <- xtabs(~GunLaw + SpendMilitary, data = GSS2002)
T5
        SpendMilitary
GunLaw   About right Too little Too much
  Favor          168        101       72
  Oppose          34         33       19
#b
obs_stat_diff3 <- chisq.test(T4)$statistic

result3 <- numeric(sims)
for (i in 1:sims) {
    T7 <- xtabs(~sample(GunLaw) + SpendMilitary, data = GSS2002)
    result3[i] <- chisq.test(T7)$statistic
}
pvalue <- (sum(result3 >= obs_stat_diff3) + 1)/(sims + 1)
pvalue
[1] 0.0031

Since the p-value is greater than 0.05 we do not reject the null hypothesis and conclude that support for gun laws and views on government spending on the military are independent.