Performs a statistical test as to whether a variable has a specified mean, whether two variables are equal, or whether the mean of one variable is equal across values of another variable.

**Typical Use:**

`ttest varx = 2 `

- Instructions given to surveyors were supposed to ensure that the sample was half
male and half female, however, the final sample is actually 51% male.
We test the hypothesis that respondent selection was actually carried out correctly
such that male and female respondents had an equal chance of being interviewed (we
cannot reject this hypothesis).
. ttest male == 0.5 One-sample t test ------------------------------------------------------------------------------ Variable | Obs Mean Std. Err. Std. Dev. [95% Conf. Interval] ---------+-------------------------------------------------------------------- male | 1100 .5136364 .0150768 .5000414 .4840538 .543219 ------------------------------------------------------------------------------ Degrees of freedom: 1099 Ho: mean(male) = 0.5 Ha: mean < 0.5 Ha: mean != 0.5 Ha: mean > 0.5 t = 0.9045 t = 0.9045 t = 0.9045 P < t = 0.8170 P > |t| = 0.3660 P > t = 0.1830

- Next, let's test whether the same fraction of the population own a bicycle as
own a TV. Again, we cannot reject this hypothesis.
. ttest own_TV == own_bicycle, unpaired Two-sample t test with equal variances ------------------------------------------------------------------------------ Variable | Obs Mean Std. Err. Std. Dev. [95% Conf. Interval] ---------+-------------------------------------------------------------------- own_TV | 1098 .0801457 .0081978 .2716424 .0640606 .0962308 own_bi~e | 1098 .079235 .0081551 .2702282 .0632336 .0952363 ---------+-------------------------------------------------------------------- combined | 2196 .0796903 .0057803 .2708749 .0683549 .0910258 ---------+-------------------------------------------------------------------- diff | .0009107 .0115633 -.0217654 .0235869 ------------------------------------------------------------------------------ Degrees of freedom: 2194 Ho: mean(own_TV) - mean(own_bicycle) = diff = 0 Ha: diff < 0 Ha: diff != 0 Ha: diff > 0 t = 0.0788 t = 0.0788 t = 0.0788 P < t = 0.5314 P > |t| = 0.9372 P > t = 0.4686

- Finally, let's test whether the average age of male respondents is equal to
average age of female respdondents. We find that we can reject this hypothesis --
male respdondents are significantly older on average.
. ttest age, by(gender) Two-sample t test with equal variances ------------------------------------------------------------------------------ Variable | Obs Mean Std. Err. Std. Dev. [95% Conf. Interval] ---------+-------------------------------------------------------------------- male | 563 43.56838 .6611666 15.68791 42.26972 44.86704 female | 530 38.91698 .642816 14.79873 37.6542 40.17977 ---------+-------------------------------------------------------------------- combined | 1093 41.3129 .466795 15.4325 40.39698 42.22882 ---------+-------------------------------------------------------------------- diff | 4.651403 .9237718 2.838832 6.463973 ------------------------------------------------------------------------------ Degrees of freedom: 1091 Ho: mean(male) - mean(female) = diff = 0 Ha: diff < 0 Ha: diff != 0 Ha: diff > 0 t = 5.0352 t = 5.0352 t = 5.0352 P < t = 1.0000 P > |t| = 0.0000 P > t = 0.0000

- Without the option
`, unpaired`, the second syntax does a one-sample test that the mean of the difference of the two variables is 0. Thus`ttest`is equivalent to*varx*==*vary*gen

*diff*=*varx*-*vary*; ttest*diff*== 0; - When doing a two-sample test (second or third syntax) Stata tests the hypothesis that not only are the means of
the two samples equal, but that their variances are equal as well. Sometimes, we don't care about the variances, and just want to test if the means of the two samples are equal.
The option
`, unequal`allows us to test this (slightly weaker) hypothesis. Usually it makes little difference, but if you did have two samples with similar means but very different variances it would.

`ttest`does not know whether or not a variable is binary, and always uses the sample variance to compute the standard error. If the variable is binary, the command`bitest`uses the standard error implied by the hypothesis, but it is irritating in that it doesn't give you the value of the t-stat, only the p-value. Redoing the first example:. bitest male == 0.5 Variable | N Observed k Expected k Assumed p Observed p -------------+------------------------------------------------------------ male | 1100 565 550 0.50000 0.51364 Pr(k >= 565) = 0.190959 (one-sided test) Pr(k <= 565) = 0.825022 (one-sided test) Pr(k <= 535 or k >= 565) = 0.381918 (two-sided test)

- See the official Stata help for
`ttest`.

contact: djiboliz@gmail.com

last modified: 30 April 2007