# Stata for Students: Proportion Tests

Proportion tests allow you to test hypotheses about proportions in a population, such as the proportion of the population that is female or the proportion that answers a question in a given way. Conceptually they are very similar to t-tests. The command to run one is simply prtest, but the syntax will depend on the hypothesis you want to test. In this section we'll discuss the following types of tests:

### The Proportion in the Population is Equal to Some Specified Value

One type of hypothesis simply asks whether the population proportion of a variable is equal to some particular value of interest.

### The Population Proportions for Two Variables are the Same

Another type of hypothesis looks at whether two variables have the same proportions.

### The Population Proportions for Two Subsamples are the Same

The final type of hypothesis we'll consider is whether two groups have the same proportions for a single variable.

For all these tests we've described the null hypothesis. Usually the null hypothesis is the opposite of what you're really interested in. For example, if you're investigating differences between men and women in the proportion that have earned a bachelor's degree, your null hypothesis will usually be that the proportions are the same. Your alternative hypothesis could then be one of the following: that the proportion of women with a bachelor's degree is higher than the proportion of men with a bachelor's degree, that the proportion of women with a bachelor's degree is different than the proportion of men with a bachelor's degree, or the proportion of women with a bachelor's degree is lower than the proportion of men with a bachelor's degree.

Stata will report results for all three alternative hypotheses, but you should choose which one you're interested in ahead of time. Looking at the results and then picking the alternative hypothesis that matches what you'd like to see will increase the probability of drawing the wrong conclusion from the test.

We will discuss the interpretation of the proportion test in detail for the first type of hypothesis (that the proportion is equal to a specified value) but the discussion applies to all the hypotheses a t-test can test.

## Setting Up

If you plan to carry out the examples in this article, make sure you've downloaded the GSS sample to your U:\SFS folder as described in Managing Stata Files. Then create a do file called prtests.do in that folder that loads the GSS sample as described in Doing Your Work Using Do Files. If you plan on applying what you learn directly to your homework, create a similar do file but have it load the data set used for your assignment.

## Preparing Variables for Proportion Tests

The prtest command assumes that the variables it will act on are binary (0/1) variables and the proportion of interest is the proportion of 1's. The GSS codes most of its binary variables as 1/2, so we'll need to create some new variables and change others to match what prtest is expecting. We'll use the following four variables in the examples for this section:

sex is coded 1 for male, 2 for female. Create a variable called female coded 0/1 (1 meaning "yes, this person is female") with:

gen female=(sex==2)

Recall that if you set a variable equal to a condition, the variable gets 1 if the condition is true and 0 if it is false.

evolved (SCI KNOWLEDGE:HUMAN BEINGS DEVELOPED FROM ANIMALS) is coded 1/2 with 1 meaning "True" (the correct answer) and 2 meaning "False". Recode it to 0/1 with 1 meaning "Yes, the respondent said humans evolved from animals" by changing the 2's to 0's with:

recode evolved (2=0)

electron (SCI KNOWLEDGE:ELECTRONS ARE SMALLER THAN ATOMS) is coded 1/2 with 1 meaning "True" (the correct answer) and 2 meaning "False". Recode it to 0/1 with 1 meaning "Yes, the respondent said electrons are smaller than atoms" by changing the 2's to 0's with:

recode electron (2=0)

relpersn (R[espondent] CONSIDER SELF A RELIGIOUS PERSON) is coded 1-4, but create a binary variable with:

gen religious=(relpersn<3) if relprsn<.

This creates a variable religious which is 1 if the respondent described themselves as moderately (relpersn=2) or very (relpersn=1) religious and 0 otherwise. The if condition ensures that if relpersn is missing (the respondent didn't answer the question) then religious is also missing.

For more examples of code like this, see Creating Variables. It also discusses creating labels, which is left as an exercise for the reader.

## Hypothesis: The Proportion is Equal to Some Specified Value

The Census Bureau reports that proportion of females in the US population is 0.508 (50.8%), but the proportion female in our sample is .567 (56.7%). Is this difference significant? To test, the syntax is:

prtest female=0.508

This gives the following output:

```One-sample test of proportion                 female: Number of obs =      254
------------------------------------------------------------------------------
Variable |       Mean   Std. Err.                     [95% Conf. Interval]
-------------+----------------------------------------------------------------
female |   .5669291   .0310905                       .505993    .6278653
------------------------------------------------------------------------------
p = proportion(female)                                        z =   1.8786
Ho: p = 0.508

Ha: p < 0.508               Ha: p != 0.508                 Ha: p > 0.508
Pr(Z < z) = 0.9698         Pr(|Z| > |z|) = 0.0603          Pr(Z > z) = 0.0302```

Formal evaluation compares the null hypothesis (Ho), that the proportion (p) is 0.508, with one of three alternative hypotheses (Ha): that the proportion is less than 0.508, that the proportion is not equal to 0.508 but could be bigger or smaller, and that the proportion is greater than 0.508. You must pick the alternative hypothesis you're interested in testing before running the test.

First consider Ha: p < 0.508. If the proportion female in the population is 0.508, the probability of drawing a sample of 254 observations with a proportion female less than 0.566921, which is what we observe in the sample, is 0.9698 (i.e. it's very likely). So if your alternative hypothesis is that the GSS population has a lower proportion female than the US population, then you should accept the null hypothesis, that they are the same, instead.

Next consider Ha: p != 0.508. If the proportion female in the population is 0.508, the probability of drawing a sample of 254 observations where the proportion female is at least 0.5669291-0.508=0.0589291 away from that in either direction is 0.0603. This is still above the conventional threshold for significance of 0.05, so if your alternative hypothesis is that the proportion is not 0.508 you should reject it in favor of the null hypothesis again.

Finally consider Ha: p > 0.508. If the proportion female in the population is 0.508, the probability of drawing a sample of 254 observations with a proportion female of 0.5669291 or greater is 0.0302, i.e. it's not very likely. Since this is below the usual threshold of 0.05, if your alternative hypothesis is that the proportion female in the population is greater than 0.508, then you should reject the null hypothesis and accept the alternative instead.

Does this mean the GSS sample is not drawn from the US population? No, but it does suggest females may be more likely to respond to the GSS than males, which could bias any analysis done with this GSS sample. This is a very important issue in survey research and statistics in general, but techniques for correcting that bias, like weighting the data, are beyond the scope of Stata for Students.

## Hypothesis: Two Variables have the Same Proportion

Now consider the variables evolved and electron. One possible research hypothesis is that the relative sizes of electrons and atoms is a more obscure issue than evolution, so the proportion getting the evolved question right should be larger than the proportion getting the electron question right. Another possible research hypothesis is that some respondents are aware of the scientific consensus on evolution but choose not to respond "true" to the evolution question for religious reasons, and thus the proportion giving the correct answer for evolved will be lower than the proportion giving the correct answer for electron (because electron does not raise similar issues). You can test both with:

prtest evolved=electron

```Two-sample test of proportions               evolved: Number of obs =      105
electron: Number of obs =       83
------------------------------------------------------------------------------
Variable |       Mean   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
evolved |    .552381   .0485265                      .4572708    .6474911
electron |   .7108434   .0497639                      .6133079    .8083788
-------------+----------------------------------------------------------------
diff |  -.1584624   .0695073                     -.2946943   -.0222306
|  under Ho:   .0712048    -2.23   0.026
------------------------------------------------------------------------------
diff = prop(evolved) - prop(electron)                     z =  -2.2254
Ho: diff = 0

Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
Pr(Z < z) = 0.0130         Pr(|Z| > |z|) = 0.0261          Pr(Z > z) = 0.9870```

Keep in mind that Stata is comparing the proportion of 1's for the two variables. It is up to you to ensure that this makes sense: in this case both variables are coded so that a 1 means the respondent gave the correct answer.

Stata calculated the difference (diff) between the two proportions as prop(evolved) - prop(electron), so the alternative hypothesis Ha: diff < 0 is also the hypothesis that the proportion giving the right answer for evolved is smaller than the proportion giving the right answer for electron, while Ha: diff > 0 is the hypothesis that the proportion giving the right answer for electron is greater than evolved.

If your research hypothesis is that more people know about evolution than the size of electrons, the relevant alternative hypothesis is Ha: diff > 0, and the very large p-value suggests you should reject the alternative and accept the null hypothesis (no difference). This finding would not support your research hypothesis.

If your research hypothesis is that some people choose not to answer "true" to the evolved question for religious reasons, then the relevant alternative hypothesis is Ha: diff < 0 and the low p-value suggests you should reject the null hypothesis and accept the alternative. This finding is consistent with your research hypothesis.

## Hypothesis: Two Subsamples have the Same Proportion

But if we think people are choosing not to answer "true" to the evolve question for religious reasons, we should see a difference in the proportion responding "true" between religious respondents and non-religious respondents. We can test that with the following syntax:

prtest evolve, by(religious)

```Two-sample test of proportions                     0: Number of obs =       48
1: Number of obs =       57
------------------------------------------------------------------------------
Variable |       Mean   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
0 |   .7291667   .0641422                      .6034503    .8548831
1 |   .4035088   .0649817                       .276147    .5308706
-------------+----------------------------------------------------------------
diff |   .3256579   .0913063                      .1467008     .504615
|  under Ho:   .0974115     3.34   0.001
------------------------------------------------------------------------------
diff = prop(0) - prop(1)                                  z =   3.3431
Ho: diff = 0

Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
Pr(Z < z) = 0.9996         Pr(|Z| > |z|) = 0.0008          Pr(Z > z) = 0.0004```

Stata calculates the difference (diff) as prop(0) - prop(1), or proportion of non-religious people who answered true minus proportion of religious people who answered true. Thus the hypothesis that religious people are less likely to answer true is Ha: diff > 0 and the very low p-value associated with it suggests we should reject the null and accept that alternative hypothesis.

On the other hand, it's possible that that religious people simply know less about science. If that were the case, we would expect the proportion of religious people who respond correctly to electron to be lower than the proportion of non-religious people:

prtest electron, by(religious)

```Two-sample test of proportions                     0: Number of obs =       36
1: Number of obs =       47
------------------------------------------------------------------------------
Variable |       Mean   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
0 |   .6944444   .0767737                      .5439707    .8449182
1 |   .7234043   .0652476                      .5955214    .8512871
-------------+----------------------------------------------------------------
diff |  -.0289598   .1007544                     -.2264348    .1685152
|  under Ho:   .1004136    -0.29   0.773
------------------------------------------------------------------------------
diff = prop(0) - prop(1)                                  z =  -0.2884
Ho: diff = 0

Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
Pr(Z < z) = 0.3865         Pr(|Z| > |z|) = 0.7730          Pr(Z > z) = 0.6135
```

The high p-value associated with Ha: diff > 0 suggests you should accept the null hypothesis (no difference) instead. In fact the proportion of religious people who answered electron correctly is slightly higher than the proportion of non-religious people, but the difference is not significant (no matter which specific alternative hypothesis you choose to test).

While it is certainly not conclusive, this combination of results supports the research hypothesis that some religious people choose not to answer "true" to the evolved question for religious reasons despite likely being aware of the scientific consensus on the issue. (On the other hand, note that 40% of religious people did answer "true" while 27% of non-religious people answered "false.")

## Saving the Data?

In order to carry out this analysis you had to create two variables (female and religious) and recode two others (evolve and electron). Should you save the modified data set?

If this is the end of your analysis you don't need to. The commands to create and recode those variables are stored in the do file, so it will load the original GSS sample, create the variables it needs, and then carry out the analysis any time you run it.

If you want to carry out more analysis using these variables, you could save the data set with a command like:

save gss2, replace

You could then write a new do file that starts by loading gss2, and it will be able to use the new and modified variables immediately.

What you should not do is save the modified data set with the same name as the original data set (gss_sample) so that it overwrites the original data. If you do that, your do file will not work because the variables female and religious will already exist and Stata won't allow you to create them again. Worse, if it turns out that you made a mistake, you'll need to download the data set again to fix it rather than being able to just fix your do file and run it again.

For research work, we recommend putting data preparation (e.g. creating and recoding variables) and analysis in separate do files. That way you can be sure that all your analysis is based on the same data.

## Complete Do File

The following is a complete do file for this section.

capture log close
log using prtests.log, replace

clear all
set more off

use gss_sample
gen female=(sex==2)
recode evolved (2=0)
recode electron (2=0)
gen religious=(relpersn<3) if relpersn<.

prtest female=0.508

prtest evolved=electron

prtest evolve, by(religious)
prtest electron, by(religious)

save gss2,replace
log close

Last Revised: 12/30/2016