Using Stata to conduct t-tests
PS601
The t-test is a very commonly encountered statistical test that is applicable in a number of designs. To perform it using Stata is relatively simple.
We will use the Presidential Approval data set for this analysis. Presapp.dta
The steps we will follow will be:
First, run simple descriptive statistics on the approval variable
summ approval
This can also be done with greater statistical detail
summ approval, d
Run a simple t-test using Stata as a calculator
Test to see if the approval rating for Jimmy Carter is worse than other Presidents. This requires that we calculate the average for the Carter years and then for all years and do a simple t-test to see if Carter is different (lower ?) that the entire period.
We have the average for all presidents (54.7 as indicated above, with a standard deviation of 13.7).
Now what is the mean for the Carter period?First identify the range for Carter (the observations that comprise the Carter years: 1977Q1 through 1980Q4.
Thus the Carter presidency is in the range 113-128.
Obtain the mean and Standard deviation for the Carter years.
summ approval in 113/128
Combined with the mean for the full period, which we will treat as the population mean, we can perform a t-test. This may be done with a calculator, or with a simple command line in Stata.
ttesti 16 46.7 12.1 54.7
Thus we can conlcude that the Carter presidency did have an avergae approval rating that was lower than the overall period from1949-1985.
.
Running a two-sample ttest is even easier. There are two type of two-sample t-tests:
- The paired comparison t-test
- In this test, the mean of two separate variables are compared
- This command looks like:
- ttest varname1 = varname2
- The standard two sample test
- In this test one variable is broken into two grouops for comparison, using a second variable to determione the groups.
- This command looks like:
- ttest varname, by(groupvar)
Thus wwe can compare Democratic presidents with Republican ones with the following command.
ttest approval, by (ptydum)
The unequal variance t-test resuls can be obtained by adding unequal to the command line.
ttest approval, by (ptydum) unequal
To know which to do, use the variance test command
sdtest approval, by (ptydum)
![]()