diff --git a/12-hypothesis-tests.Rmd b/12-hypothesis-tests.Rmd index 2961a72b..789c8ec4 100644 --- a/12-hypothesis-tests.Rmd +++ b/12-hypothesis-tests.Rmd @@ -37,6 +37,7 @@ library(knitr) library(kableExtra) library(patchwork) library(scales) +library(cowplot) ``` ## Decision making {#decision-making} @@ -203,7 +204,7 @@ Finally, there is one more piece of vocabulary that is important: * The type I error rate ($\alpha$) * Sometimes other sample statistics -## Conducting Hypothesis Tests +## Conducting Hypothesis Tests {#two-tailed} @@ -384,7 +385,7 @@ summary(movies_model) ``` We again see the p-value of 0.005, which leads us to conclude there is a true difference in means in the population. -### Ride share example revisited +### Ride share example revisited {#ride-two-tail} Let's return to our ride share price example from Section \@ref(ride-share). Recall that we wanted to know whether $\mu_B = \$19.50$. We could test this with the following hypotheses, again specifying $\alpha = 0.05$ in advance: @@ -416,6 +417,101 @@ summary(ride_model_2) This again gives the p-value of $0.13$ which leads us to *fail to reject $H_0$*. There is insufficient evidence to conclude that company B's prices differ from \$19.50 on average. +## One-tailed hypothesis tests + +All the examples in Section \@ref(two-tailed) were what are call two-tailed hypothesis tests. This means that the rejection region is split into both the upper and lower tail. A two-tailed test is used when the hypothesis claim includes equality. Then we are concerned with the chance of being either above or below the claim. + +But what if our hypothesis is that the test statistic is less than a claimed value or greater than a claimed value? In that case we are only concerned with one direction and it is more appropriate to use a one-tailed hypothesis test. + +### Ride share example revisited again + +Consider the ride share example from Section \@ref(ride-share) and \@ref(ride-two-tail). We want to test the claim if the average price of rides at company B is equal to \$19.50. To test this claim we obtain a random sample of 100 rides and compare the sample mean. +Our claim again is $\mu_B = 19.50$, meaning we are concerned if their price is different in either direction. Recall, we write our hypothesis test as: + +$$H_0: \mu_B = 19.50$$ +$$H_A: \mu_B \neq 19.50$$ +What if instead, we want to test the claim that the average price of rides at company B is **greater than** $\$19.50$? In mathematical notation this claim is $\mu_B > 19.50$. We formulate the hypothesis as follows: + +$$H_0: \mu_B \leq 19.50$$ +$$H_A: \mu_B > 19.50$$ +It's important to note that the null hypothesis always contains equality! +Now our rejection region is strictly in the upper tail of the distribution. Let's test this claim, again specifying $\alpha = 0.05$ in advance. + + +```{r, warnings = FALSE, message = FALSE} +rides_one.tail <- t.test(rides_B$price, mu = 19.5, alternative = "greater") +rides_one.tail$statistic +rides_one.tail$p.value +``` + +The test-statistic is `r rides_one.tail$statistic`, the same test statistic from our two-tailed test in Section \@ref(ride-two-tail). This means the test-statistic for a one-tailed test compared to a two-tailed test will always be the same. What is different is our p-value of `r rides_one.tail$p.value`. The p-value for a one-tailed test is half the p-value of a two-tailed test. We still compare our $p\_value$ to our pre-defined $\alpha$. Since our $p\_value = 0.065 > \alpha = 0.05$, we *do not reject* the null hypothesis. In this instance we came to the same conclusions, but that is not always the case. + +```{r, rideshare-p-value, fig.cap = "P-value for Ride Share Hypothesis in Two-tail and One-tail Test", message = FALSE, echo = FALSE, warning = FALSE} +shade_curve <- function(MyDF, tstart, tend, fill = "red", alpha = .5){ + geom_area(data = subset(MyDF, x >= 0 + tstart + & x < 0 + tend), + aes(y=y), fill = fill, color = NA, alpha = alpha) +} + +test_stat <- rides_one.tail$statistic %>% + as.numeric() +data <- data.frame(x = seq(from = -5, to = 5, by = 0.01)) %>% + mutate(y = dnorm(x)) +two_tail <- ggplot(data.frame(x = c(-4, 4)), aes(x = x)) + + stat_function(fun = dnorm, + aes(colour = "Z-distribution"), size = 1) + + geom_vline(xintercept = test_stat, linetype = 2) + + geom_vline(xintercept = -test_stat, linetype = 2) + + shade_curve(data, tstart = -6, tend = -test_stat) + + shade_curve(data, tstart = test_stat, tend = 6) + + xlab("Z") + + theme(legend.position = "none") + + labs(title = "two-tailed test") + +one_tail <- ggplot(data.frame(x = c(-4, 4)), aes(x = x)) + + stat_function(fun = dnorm, + aes(colour = "Z-distribution"), size = 1) + + geom_vline(xintercept = test_stat, linetype = 2) + + #geom_vline(xintercept = -test_stat, linetype = 2) + + #shade_curve(data, tstart = -6, tend = -test_stat) + + shade_curve(data, tstart = test_stat, tend = 6) + + xlab("Z") + + theme(legend.position = "none")+ + labs(title = "one-tailed test") + +plot_grid(two_tail, one_tail, ncol=2) +``` + + +### Formulating the Hypotheses Overview + +When formulating the hypothesis you first want to state the claim in mathematical notation. If the claim contains equality, it belongs in the null hypothesis and if it does not it belongs in the alternative hypothesis. Next decide if the claim is a two-tailed, right-tailed, or left-tailed test. Table \@ref(tab:tail-ch-12) provides an overview of the three possible scenarios. + +```{r tail-ch-12, echo=FALSE, message=FALSE} + +tails <- read_rds("rds/ch12_tails.rds") +#rownames(tails) <- c("$H_{0}$", "$H_{A}$", "p-value") + +tails %>% + kable( + caption = "Formulating the Null and Alternative Hypothesis", + booktabs = TRUE, + escape = FALSE, + align = c('c', 'c', 'c') + ) %>% + kable_styling(font_size = ifelse(knitr:::is_latex_output(), 10, 16), + latex_options = c("HOLD_position")) %>% + column_spec(1, width = "1in") %>% + column_spec(2, width = "2in") %>% + column_spec(3, width = "2in") %>% + column_spec(4, width = "2in") + +``` + + +Choosing a one-tailed test for the sole purpose of attaining significance is not appropriate. For example, if you formulate a two-tailed test at the $\alpha = 0.05$ significance level and obtain a p-value of 0.051 you fail to reject the null. You cannot then change the test to a one-tail test in order to obtain significance. This will lead to questionable and invalid results. + + ## More advanced points to consider As this is an introductory book, we have introduced some concepts but not developed them in full detail. This does not mean that there are not things to say about these – more that there is simply too much to say at this time. These topics include: diff --git a/images/tail_left.png b/images/tail_left.png new file mode 100644 index 00000000..0cb69f9a Binary files /dev/null and b/images/tail_left.png differ diff --git a/images/tail_right.png b/images/tail_right.png new file mode 100644 index 00000000..8b6bbee4 Binary files /dev/null and b/images/tail_right.png differ diff --git a/images/tail_two.png b/images/tail_two.png new file mode 100644 index 00000000..3073c507 Binary files /dev/null and b/images/tail_two.png differ diff --git a/moderndive_book.Rproj b/intro-stat-ds.Rproj similarity index 100% rename from moderndive_book.Rproj rename to intro-stat-ds.Rproj diff --git a/rds/ch12_tails.rds b/rds/ch12_tails.rds new file mode 100644 index 00000000..1c98a4f5 Binary files /dev/null and b/rds/ch12_tails.rds differ