This article is part of the Stata for Students series. If you are new to Stata we strongly recommend reading all the articles in the Stata Basics section.
t-tests are frequently used to test hypotheses about the population mean of a variable. The command to run one is simply ttest, but the syntax will depend on the hypothesis you want to test. In this section we'll discuss the following types of tests:
The Population Mean is Equal to Some Specified Value
One type of hypothesis simply asks whether the population mean of a variable is equal to some particular value of interest. This is called a single-sample t-test, because you look at the entire sample at once.
The Population Means for Two Variables are the Same
Another type of hypothesis looks at whether two variables have the same population mean. This is called a paired-sample t-test, because the test assumes that the values of the two variables for the same observation go together (i.e. the value of X for observation 1 has a relationship to the value of Y for observation 1 that does not exist between the value of X for observation 1 and the value of Y for observation 2).
The Population Means for Two Subsamples are the Same
The final type of hypothesis we'll consider is whether two groups have the same population mean for a single variable. This is called a two-sample t-test, and is the most common.
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 mean education level, your null hypothesis will usually be that they are the same. Your alternative hypothesis could then be one of the following: that the mean education level of women is higher than the mean education level of men, that the mean education level of men is higher than the mean education level of women, or that the mean levels of education are different regardless of which is higher.
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 t-test in detail for the first type of hypothesis (that the mean 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 ttests.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.
Hypothesis: The Population Mean is Equal to Some Specified Value
Suppose you want to test the hypothesis that the population mean of educ is 14 years. The syntax is simply:
ttest educ=14
This gives the output:
One-sample t test ------------------------------------------------------------------------------ Variable | Obs Mean Std. Err. Std. Dev. [95% Conf. Interval] ---------+-------------------------------------------------------------------- educ | 254 13.38583 .2093408 3.336343 12.97355 13.7981 ------------------------------------------------------------------------------ mean = mean(educ) t = -2.9338 Ho: mean = 14 degrees of freedom = 253 Ha: mean < 14 Ha: mean != 14 Ha: mean > 14 Pr(T < t) = 0.0018 Pr(|T| > |t|) = 0.0037 Pr(T > t) = 0.9982
The mean of educ in the sample, which is also the best estimate of the population mean, is 13.38. But in order to evaluate the hypothesis that mean is really 14, you have to consider the uncertainty about that estimate. The 95% confidence interval ranges from 12.97 to 13.80, which does not include 14, so it's not looking good for our null hypothesis.
Formal evaluation compares the null hypothesis (Ho), that the mean is 14, with one of three alternative hypotheses (Ha): that the mean is less than 14, that the mean is not equal to 14 but could be bigger or smaller, and that the mean is greater than 14. You must pick the alternative hypothesis you're interested in testing before running the test.
First consider Ha: mean < 14. If the population mean is 14, then the probability of drawing a sample with a mean of 13.38 or less, given the number of observations we have and the standard deviation we observe, is 0.0018 (i.e. it's extremly unlikely). This is less than .05, so we reject the null hypothesis that the mean is 14 in favor of the alternative that the mean is less than 14.
Next consider Ha: mean != 14. If the population mean is 14, then the probability of drawing a sample that is at least 14 - 13.38 = 0.62 away from that mean in either direction is 0.0037 (again, given the number of observations we have and the standard deviation we observe). This is exactly twice the probability of the previous hypothesis, though this is obscured by rounding. The previous hypothesis was a one-tail test (i.e. looking at the probability that the outcome is out in one of the "tails" of the probability distribution) while this is a two-tail test (i.e. looking at the probability that the outcome is in either tail of the distribution). Again the probability is less than 0.05, so we reject the null hypothesis that the mean is 14 in favor of the alternative hypothesis that the mean is something other than 14.
Finally consider Ha: mean > 14. If the population mean is 14, then the probability of drawing a sample with a mean that is 13.38 or greater is 0.9982 (i.e. it's almost certain). This probability is nowhere near less than 0.05, so in this case we accept the null hypothesis that the mean is 14 rather than the alternative that the mean is greater than 14.
Changing the Confidence Level
If you want to consider a different confidence level, use the level() option with the desired confidence level in the parentheses:
ttest educ=14, level(90)
This produces:
One-sample t test ------------------------------------------------------------------------------ Variable | Obs Mean Std. Err. Std. Dev. [90% Conf. Interval] ---------+-------------------------------------------------------------------- educ | 254 13.38583 .2093408 3.336343 13.04023 13.73143 ------------------------------------------------------------------------------ mean = mean(educ) t = -2.9338 Ho: mean = 14 degrees of freedom = 253 Ha: mean < 14 Ha: mean != 14 Ha: mean > 14 Pr(T < t) = 0.0018 Pr(|T| > |t|) = 0.0037 Pr(T > t) = 0.9982
The only change is that you are given a 90% confidence interval rather than a 95% confidence interval. The true mean will fall into this interval 90% of the time rather than 95% of the time like in the prior results, so this interval is slightly smaller.
Hypothesis: The Population Means for Two Variables are the Same
Suppose you wanted to test the hypotheses that the population mean for the respondent's father's education (paeduc) is the same as the population mean for the respondent's mother's education (maeduc). This is a paired sample test because the mother and father of the same respondent are related. To do this, run:
ttest paeduc=maeduc
This produces:
Paired t test ------------------------------------------------------------------------------ Variable | Obs Mean Std. Err. Std. Dev. [95% Conf. Interval] ---------+-------------------------------------------------------------------- maeduc | 172 11.94767 .2895882 3.797913 11.37605 12.5193 paeduc | 172 11.98256 .3293803 4.319782 11.33238 12.63273 ---------+-------------------------------------------------------------------- diff | 172 -.0348837 .2189349 2.871305 -.4670468 .3972794 ------------------------------------------------------------------------------ mean(diff) = mean(maeduc - paeduc) t = -0.1593 Ho: mean(diff) = 0 degrees of freedom = 171 Ha: mean(diff) < 0 Ha: mean(diff) != 0 Ha: mean(diff) > 0 Pr(T < t) = 0.4368 Pr(|T| > |t|) = 0.8736 Pr(T > t) = 0.5632
Stata calculated the difference (diff) between the two means as maeduc - paeduc, so the alternative hypothesis mean(diff) < 0 is also the hypothesis that paeduc is greater than maeduc. In this case the probabilities associated with all three alternative hypotheses are well above 0.05, so no matter which alternative hypothesis you chose to test you would accept the null hypothesis that the means are the same. More precisely, we do not have sufficient evidence to reject the hypothesis that they are same. It's possible we could reject that hypothesis if we had more observations, for example.
Hypothesis: The Population Means for Two Subsamples are the Same
Suppose you wanted to test the hypothesis that the population mean of educ is the same for men and women. To do this, run:
ttest educ, by(sex)
Two-sample t test with equal variances ------------------------------------------------------------------------------ Group | Obs Mean Std. Err. Std. Dev. [95% Conf. Interval] ---------+-------------------------------------------------------------------- male | 110 13.33636 .3275703 3.435586 12.68713 13.9856 female | 144 13.42361 .2725067 3.27008 12.88495 13.96227 ---------+-------------------------------------------------------------------- combined | 254 13.38583 .2093408 3.336343 12.97355 13.7981 ---------+-------------------------------------------------------------------- diff | -.0872475 .4232854 -.9208752 .7463803 ------------------------------------------------------------------------------ diff = mean(male) - mean(female) t = -0.2061 Ho: diff = 0 degrees of freedom = 252 Ha: diff < 0 Ha: diff != 0 Ha: diff > 0 Pr(T < t) = 0.4184 Pr(|T| > |t|) = 0.8369 Pr(T > t) = 0.5816
diff is defined as mean(male) - mean(female), so the alternative hypothesis diff < 0 is also the hypothesis that the mean of educ for females is greater than the mean of educ for males. All the probabilities are well above 0.05, so once again no matter which alternative hypothesis you chose to test you will not reject the null hypothesis that the mean level of education for males and females is the same.
Note that this test assumed that the population variance of educ was the same for males and females. We can see from the output that the standard deviation (which is the square root of the variance) is slightly higher for males in the sample. If we think that difference is real, we can tell the ttest command to take it into account by adding the unequal option:
ttest educ, by(sex) unequal
In this case it makes very little difference.
Complete Do File
The following is a complete do file for this section.
capture log close
log using ttests.log, replace
clear all
set more off
use gss_sample
ttest educ=14
ttest educ=14, level(90)
ttest paeduc=maeduc
ttest educ, by(sex)
ttest educ, by(sex) unequal
log close
Last Revised: 9/2/2016