# T-tests

There are different types of t-tests, all handled by the **ttest** command in Stata. All of the examples below use the bplong or bpwide datasets. (To load the dataset, type sysuse bplong -- or sysuse bpwide -- at the command line in Stata.)

## Single-sample t-test

A single-sample t-test compares the mean of your sample to a test number, specified by you.

Using the blood pressure dataset (bplong), I hypothesize that the mean of the entire dataset is equal to 155 mmHg.

sysuse bplong

ttest bp==155

Based on the results of this test, I fail to reject my null hypothesis that the mean of bp is not significantly different from 155 mmHg. (Run this code on your own and see if you reach the same conclusion.)

I am interested in seeing if when the blood pressure reading is affected by when the reading was taken; this is represented by the variable **when**, which takes values of 1 ("Before") and 2 ("After").

(Look at your data -- type **br** at the command line -- and look at **when**; it is labeled such that the underlying values are numeric but the human-read values are words. Note that "Before" and "After" are blue, and not red -- meaning they are labels [and not strings].)

Use the following two tests to ask whether the mean for the first group is equal to 155 mmHg, or if the mean of the second group is equal to 155 mmHg.

ttest bp==155 if when==1

ttest bp==155 if when==2

## Two sample t-test

Because of my investigations above, I want to examine whether the mean blood pressure reading is different between the "Before" and "After" groups.

sysuse bplong

ttest bp, by(when)

If your data were formatted differently (wide and not long), you could also use variables for your "before" and "after" groups. (*Please see the next section re: why you should NOT use an unpaired ttest on this particular dataset.)*

sysuse bpwide

ttest bp_before == bp_after, unpaired

## Paired t-test

These blood pressure readings were taken from the same (fictional) subjects. Since there may be some effect of individual on the blood pressure readings, the proper way to compare before and after is to account for the dependent nature of your data. To do this, use a paired t-test:

sysuse bpwide

ttest bp_before == bp_after

Note that the results from this paired t-test and the two-sample, unpaired t-test above are not the same.* Stata will let you make methodological mistakes because it does not know any better; your job is to avoid these mistakes by approaching your analysis carefully.*

More on T-tests