Get startedGet started for free

Hypothesis test for a proportion

1. Hypothesis test for a proportion

In this chapter you will extend your toolbox to include hypothesis tests on proportions. The hypothesis test is very closely related to the confidence interval that was covered in the last chapter.

2. Confidence interval

A confidence interval is formed using the standard error, which is the standard deviation of the sampling distribution. This distribution is the heart of the confidence interval.

3. Confidence interval

It captures how much uncertainty you have in your estimate. The more data that your estimate is based upon, the narrower this distribution and the confidence interval become.

4. Hypothesis test

A hypothesis test on a proportion asks the question: what sort of p-hats would you observe if the true parameter p held a particular value?

5. Hypothesis test

We assert this hypothesis, this model of the true state of the word, using hypothesize.

6. Hypothesis test

The insertion of this command will cause generate to now generate datasets according to the world as set out in hypothesize.

7. Hypothesis test

Thus the sampling distribution, now called the null distribution, captures the variability in p-hat that you might observe in a world where H0 is true.

8. Do half of Americans favor capital punishment?

Let’s use a hypothesis test to study another question on the gss. Respondents were asked if they favor or oppose capital punishment, also called the death penalty, for the crime of murder. You can look at the distribution of answers by forming a bar chart. You see that of the 150 respondents, more than half answered favor. You can calculate that proportion, p-hat, CL more precisely using the summarize command, and you learn that the proportion is about point-5-7

9. Do half of Americans favor capital punishment?

Is this data consistent with a world in which only half of Americans favor the death penalty? It’s hard to say. We only have 150 subjects in our sample, so it’s possible that we happened to select slightly more people in favor of it just by chance. The hypothesis test helps sort this out. Begin by specifying the column that you’re interested in as well as the level of that variable that will indicate a success. We’ll use FAVOR, but you could just as easily use OPPOSE. Next, you hypothesize that the true proportion that favor is point-5. This is an example of a "point" null hypothesis - you are fixing a particular point value for a parameter. Next, we generate through "simulation", 500 datasets that might occur under this hypothesis, and calculate the proportion that favor for each. We’ll save this data frame under the name null. If you print out the top of the null data frame, you’ll see in the stat column you have 500 examples of the type of p-hats that might occur if in fact the true parameter is point-5. Recall that in the confidence interval setting, these are clustered around the p-hat that was found in the original gss dataset, whereas here they are clustered around the null value point-5.

10. Do half of Americans favor capital punishment?

The distribution of those statistics can be visualized using a density plot. We’ll add in geom_vline to overlay a vertical red line at the location of our observed p-hat, point-5-7. The curve represents the sort of p-hats you would see if p was point-5 and the red line is the p-hat that you actually observed. If your p-hat is in the main part of the null distribution, then it is consistent with the null hypothesis. If it’s far into the tails, though, it’s deemed very unlikely if the null hypothesis were true. This is formalized by computing the p-value, the proportion of generated p-hats that were more extreme than the observed p-hat. In other words, we want to summarize the column of statistics by finding the proportion that are greater than point-5-7. We multiply that number by two to account for the left tail of the null distribution, which also contained p-hats that were farther from the center of the distribution than point-5-7.

11. Hypothesis test

To recap, the hypothesis test is formulated around a null hypothesis that states a theory about the way the world works, in this case the particular point value taken by a proportion. The null distribution provides a sense of the statistics, the p-hats, that would be calculated if you were to generate many datasets according to the null hypothesis. The p-value tells you where your actual observed p-hat falls into that distribution. If it's in the body of the distribution, the p-value will be high, indicating consistency between your data and the null hypothesis. If it's in the tails of the distribution, the p-value will be low, indicating that it's inconsistent with the null hypothesis.

12. Let's practice!

OK, now it's your turn to practice.