Chapter 6.1.1.1 Exercises

rankbased
In [3]:
library(dplyr)
library(ggplot2)
library(infer)
library(nycflights13)
library(ggplot2movies)
library(broom)
In [1]:
require(graphics)

t.test(1:10, y = c(7:20)) # P = .00001855
t.test(1:10, y = c(7:20, 200)) # P = .1245 -- NOT significant anymore
	Welch Two Sample t-test

data:  1:10 and c(7:20)
t = -5.4349, df = 21.982, p-value = 1.855e-05
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -11.052802  -4.947198
sample estimates:
mean of x mean of y 
      5.5      13.5 
	Welch Two Sample t-test

data:  1:10 and c(7:20, 200)
t = -1.6329, df = 14.165, p-value = 0.1245
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -47.242900   6.376233
sample estimates:
mean of x mean of y 
  5.50000  25.93333 
In [3]:
# Traditional interface

with(sleep, t.test(extra[group == 1], extra[group == 2]))
	Welch Two Sample t-test

data:  extra[group == 1] and extra[group == 2]
t = -1.8608, df = 17.776, p-value = 0.07939
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -3.3654832  0.2054832
sample estimates:
mean of x mean of y 
     0.75      2.33 
In [3]:
# Traditional interface

with(sleep, t.test(extra[group == 1], extra[group == 2]))
	Welch Two Sample t-test

data:  extra[group == 1] and extra[group == 2]
t = -1.8608, df = 17.776, p-value = 0.07939
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -3.3654832  0.2054832
sample estimates:
mean of x mean of y 
     0.75      2.33 
In [6]:
wilcox.test(y - x, alternative = "less") # The same.
	Wilcoxon signed rank test

data:  y - x
V = 5, p-value = 0.01953
alternative hypothesis: true location is less than 0
In [7]:
wilcox.test(y - x, alternative = "less",
exact = FALSE, correct = FALSE) # H&W large sample approximation
	Wilcoxon signed rank test

data:  y - x
V = 5, p-value = 0.01908
alternative hypothesis: true location is less than 0
In [ ]:
# Two-sample test.
# Hollander & Wolfe (1973), 69f.
# Permeability constants of the human chorioamnion (a placental
# membrane) at term (x) and between 12 to 26 weeks gestational
# age (y). The alternative of interest is greater permeability
# of the human chorioamnion for the term pregnancy.

x <- c(0.80, 0.83, 1.89, 1.04, 1.45, 1.38, 1.91, 1.64, 0.73, 1.46)
y <- c(1.15, 0.88, 0.90, 0.74, 1.21)
wilcox.test(x, y, alternative = "g") # greater
	Wilcoxon rank sum test

data:  x and y
W = 35, p-value = 0.1272
alternative hypothesis: true location shift is greater than 0
In [10]:
wilcox.test(rnorm(10), rnorm(10, 2), conf.int = TRUE)
	Wilcoxon rank sum test

data:  rnorm(10) and rnorm(10, 2)
W = 11, p-value = 0.002089
alternative hypothesis: true location shift is not equal to 0
95 percent confidence interval:
 -2.2477085 -0.5804047
sample estimates:
difference in location 
             -1.315183 
In [4]:
# Formula interface.

boxplot(Ozone ~ Month, data = airquality)
wilcox.test(Ozone ~ Month, data = airquality,
subset = Month %in% c(5, 8))
Warning message in wilcox.test.default(x = c(41L, 36L, 12L, 18L, 28L, 23L, 19L, :
“cannot compute exact p-value with ties”
	Wilcoxon rank sum test with continuity correction

data:  Ozone by Month
W = 127.5, p-value = 0.0001208
alternative hypothesis: true location shift is not equal to 0
In [1]:
wilcox.test(c(15,7,3,10,13), mu = 6)
	Wilcoxon signed rank test

data:  c(15, 7, 3, 10, 13)
V = 13, p-value = 0.1875
alternative hypothesis: true location is not equal to 6
In [3]:
# Let’s look at some examples. Consider the react data set. Recall that a t test looks like this:

react <- ISwR::react
t.test(react)
	One Sample t-test

data:  react
t = -7.7512, df = 333, p-value = 1.115e-13
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 -0.9985214 -0.5942930
sample estimates:
 mean of x 
-0.7964072 
In [4]:
# and we got a p-value of 10**−13. If we instead were to do a wilcoxon test, we get

wilcox.test(react)
	Wilcoxon signed rank test with continuity correction

data:  react
V = 9283.5, p-value = 2.075e-13
alternative hypothesis: true location is not equal to 0
In [5]:
(p <- sum(replicate(10000,t.test(rnorm(50),mu=.5)$p.value)< .05))

# Here, we see that t.test correctly rejects H0 93.75% of the time. Doing the same thing for Wilcoxon:

(p <- sum(replicate(10000,wilcox.test(rnorm(50),mu=.5)$p.value)< .05))

# We see that Wilcoxon correctly rejects H0 91.86% of the time. So, if you have reason to think that your data is normal or symmetric with not too heavy tails, then you will get better results using t.test. If it is symmetric but with heavy tails or especially with outliers, then Wilcoxon will perform better.
9356
9279
In [14]:
# Ordinal data: In order to do a two-sample Wilcoxon test, we need to load the data:

movies <- read.csv("http://stat.slu.edu/~speegle/_book_data/movieLensData", as.is = TRUE)

# Now, we need to pull out the ratings that correspond to Toy Story and Toy Story II. We begin by finding the movieID of the Toy Story movies:

library(dplyr)
filter(movies, stringr::str_detect(Title, "Toy Story")) %>% select(MovieID, Title) %>% distinct()
MovieIDTitle
1 Toy Story (1995)
3114 Toy Story 2 (1999)
In [20]:
# Next, we create a data frame that only has the ratings of the Toy Story movies in it.

ts <- filter(movies, MovieID %in% c(1, 3114))
ts
# Now, we can perform the Wilcoxon test:

wilcox.test(Rating~Title, data = ts)

# And we can see that there is not a statistically significant difference in the ratings of Toy Story and Toy Story II, so we do not reject the null hypothesis.
MovieIDTitleGenresUserIDRatingTimestamp
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2303 4.0 858170454
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2765 5.0 982166561
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2272 5.0 835022700
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2736 3.0 1190149990
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2662 2.0 1111581364
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2161 4.0 960563419
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2494 4.0 849785351
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2385 4.5 1076874494
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2236 4.0 835022152
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2747 5.0 982162047
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2180 4.0 952373774
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2465 5.0 868698565
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2847 4.0 868797356
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2714 4.0 974497009
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2610 3.0 941844070
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2361 3.5 1134668517
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2871 5.0 913486681
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2825 5.0 835046702
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2716 5.0 1163124126
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2324 3.0 1149787063
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2653 4.0 1111581403
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2467 4.0 946003530
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2861 5.0 1111590765
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2471 4.0 1216106070
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2434 4.0 1134620877
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2879 1.5 1085454880
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2679 4.0 1111584944
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2812 3.0 1085450584
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2445 4.0 835029844
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy2442 3.0 833019221
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2714 3.0 1034785913
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2168 3.0 1189137894
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2156 4.0 948686777
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2237 5.0 992607429
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2427 3.0 1001527487
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2770 3.5 1111586731
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2672 1.0 1111583581
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2842 3.0 1190043282
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2251 4.0 1111571436
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2447 3.5 1111574978
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2359 2.0 1023413799
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2484 4.0 1091239436
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2634 3.0 1163118244
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2204 4.0 1188852340
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2487 3.5 1085419607
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2220 5.0 960492688
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2467 4.0 946003214
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2709 4.0 982128702
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2706 4.5 1163123386
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2471 3.0 1216106684
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2227 5.0 948728182
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2612 4.0 1023539980
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2675 4.0 995691791
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2548 5.0 990644512
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2522 3.0 1051119208
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2859 4.0 1085445258
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2867 5.0 1189466425
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2592 4.0 1023544519
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2182 4.5 1134559365
3114 Toy Story 2 (1999) Adventure|Animation|Children|Comedy|Fantasy2796 1.5 1111592023
	Wilcoxon rank sum test with continuity correction

data:  Rating by Title
W = 17188, p-value = 0.6162
alternative hypothesis: true location shift is not equal to 0
In [25]:
# This is an example of a paired, one-sided Mann-Whitney-Wilcoxon test. Let’s pull out the data that we want to examine:

cbt <- filter(MASS::anorexia, Treat == "CBT") 

#Used MASS:: so I didn't have to load entire library(MASS). You could also load the library if you prefer. But, MASS has a select function that will hide dplyr's select, if you load it afterwards.

# We want to make a boxplot:

library(tidyr)
cbtBoxData <- gather(cbt, key = WeightTime, value = Weight, -Treat)
ggplot(cbtBoxData, aes(x = WeightTime, y = Weight)) + geom_boxplot()
In [27]:
# So, there is some evidence that the treatment was effective based on this test, but the evidence isn’t overwhelming. Note that if we hadn’t paired the data, we would have seen less significance in the results, and if we hadn’t done a one-sided test, then we would have seen even less significance.

wilcox.test(cbt$Prewt, cbt$Postwt, paired = FALSE, alternative = "less")
Warning message in wilcox.test.default(cbt$Prewt, cbt$Postwt, paired = FALSE, alternative = "less"):
“cannot compute exact p-value with ties”
	Wilcoxon rank sum test with continuity correction

data:  cbt$Prewt and cbt$Postwt
W = 320.5, p-value = 0.06088
alternative hypothesis: true location shift is less than 0
In [32]:
wilcox.test(cbt$Prewt, cbt$Postwt, paired = FALSE, alternative = "two")
Warning message in wilcox.test.default(cbt$Prewt, cbt$Postwt, paired = FALSE, alternative = "two"):
“cannot compute exact p-value with ties”
	Wilcoxon rank sum test with continuity correction

data:  cbt$Prewt and cbt$Postwt
W = 320.5, p-value = 0.1218
alternative hypothesis: true location shift is not equal to 0
In [33]:
# Simulations: We begin by preparing the power of Wilcoxon rank-sum test with t-test when the underlying data is normal. We assume we are testing H0:μ=0 versus Ha:μ≠0 at the α=.05 level, when the underlying population is truly normal with mean 1 and standard deviation 1 with a sample size of 10. Let’s first estimate the percentage of time that a t-test correctly rejects the null-hypothesis.

set.seed(1)
sum(replicate(10000, t.test(rnorm(10,1,1))$p.value < .05))
7932
In [36]:
sum(replicate(10000, wilcox.test(rt(10,3) + 1)$p.value < .05))

# Here, we see that Wilcoxon outperforms t, but not by a tremendous amount.
5423
In [41]:
# The automatic ranks are the ones that are not in manualRanks. It can be a little tricky to pull these out. Here are a couple of ways:

which(!(1:32 %in% manualRanks))
(1:32)[-manualRanks]
setdiff(1:32, manualRanks)
autoRanks <- setdiff(1:32, manualRanks) ; autoRanks
  1. 2
  2. 3
  3. 5
  4. 6
  5. 8
  6. 9
  7. 10
  8. 11
  9. 12
  10. 13
  11. 17
  12. 18
  13. 19
  14. 20
  15. 21
  16. 23
  17. 25
  18. 29
  19. 32
  1. 2
  2. 3
  3. 5
  4. 6
  5. 8
  6. 9
  7. 10
  8. 11
  9. 12
  10. 13
  11. 17
  12. 18
  13. 19
  14. 20
  15. 21
  16. 23
  17. 25
  18. 29
  19. 32
  1. 2
  2. 3
  3. 5
  4. 6
  5. 8
  6. 9
  7. 10
  8. 11
  9. 12
  10. 13
  11. 17
  12. 18
  13. 19
  14. 20
  15. 21
  16. 23
  17. 25
  18. 29
  19. 32
  1. 2
  2. 3
  3. 5
  4. 6
  5. 8
  6. 9
  7. 10
  8. 11
  9. 12
  10. 13
  11. 17
  12. 18
  13. 19
  14. 20
  15. 21
  16. 23
  17. 25
  18. 29
  19. 32
In [46]:
# If it is bigger than or equal to the observed value of 214 or less than or equal to 33 (which is the value equally far from the expected value of 13*19/2 = 123.5), then the test statistic would be in the smallest rejection region that contains our observed test statistic. The p-value is the probability of landing in this rejection region, so we estimate it.

mean(replicate(20000, { manualRanks <- sample(32, 13);
                      autoRanks <- setdiff(1:32, manualRanks);
                      abs(wilcox.test(manualRanks, autoRanks)$statistic - 123.5) >= 90.5}))
0.00045
In [44]:
# Another option would be to take the 32 data points that we have, and randomly assign them to either auto or manual transmissions, and estimate the p-value this way. The code would look like this: I am suppressing warnings in wilcox.test so we don’t get warned about ties so much!

mean(replicate(20000, {manualSample <- sample(32, 13) 
                      manualDisp <- mtcars$disp[manualSample];
                      autoDisp <- mtcars$disp[setdiff(1:32, manualSample)];
                      abs(suppressWarnings(wilcox.test(manualDisp, autoDisp)$statistic) - 123.5) >= 90.5}))

# The results are similar to the previous method.
3e-04
In [1]:
# On the other hand, if we obtained 30 successes, the p-value would sum the probabilities that are less than observing exactly 30 successes, as indicated by this plot:

library(ggplot2)
ggplot(plotData, aes(x, y)) + geom_bar(stat = "identity") +
  geom_abline(slope = 0, intercept = dbinom(30, 100, 0.4), linetype = "dashed", color = "red")
Error in ggplot(plotData, aes(x, y)): object 'plotData' not found
Traceback:

1. ggplot(plotData, aes(x, y))
In [8]:
# R will make these computations for us, naturally, in the following way.

binom.test(x = 50, n = 100, p = .4)
	Exact binomial test

data:  50 and 100
number of successes = 50, number of trials = 100, p-value = 0.05188
alternative hypothesis: true probability of success is not equal to 0.4
95 percent confidence interval:
 0.3983211 0.6016789
sample estimates:
probability of success 
                   0.5 
In [16]:
pnorm(50, 40, 4.89879, lower.tail = FALSE) + pnorm(30, 40, 4.89879)
0.0412189898970574
In [14]:
# The built in R function for this is prop.test, as follows:

prop.test(x = 50, n = 100, p = 0.4, correct = FALSE)
binom.test(x = 50, n = 100, p = .4)
	1-sample proportions test without continuity correction

data:  50 out of 100, null probability 0.4
X-squared = 4.1667, df = 1, p-value = 0.04123
alternative hypothesis: true p is not equal to 0.4
95 percent confidence interval:
 0.4038315 0.5961685
sample estimates:
  p 
0.5 
	Exact binomial test

data:  50 and 100
number of successes = 50, number of trials = 100, p-value = 0.05188
alternative hypothesis: true probability of success is not equal to 0.4
95 percent confidence interval:
 0.3983211 0.6016789
sample estimates:
probability of success 
                   0.5