STATA Comparison of Means Module: the t-tests


This module will provide an overview of how to run t-tests (comparison of mean tests) in STATA.  In doing so, this module steps you through the process of running a t-test, including testing for the assumptions underlying the various t-tests.  This module will use data on randomly sampled loblolly pines in Thomas County, Georgia (Moore, McCabe and Craig, 2009). This STATA software module will NOT be covered on the Diagnostic Exam (although you should understand comparison of means tests).


Goals of the Module


1.  Conduct a one-sample t-test using STATA software package.

2.  Conduct a two independent sample t-test using STATA software with equal variance.

3.  Interpret results of the t-tests, including the p-value.

4.  Interpret confidence intervals associated with t-tests.

5.  Understand the differences between one-sided and two-sided tests.

6.  Understand and test the assumptions underlying the t-test.


Exercise 1: One Sample Mean Comparison Test, Two-Sided Test


Question of Interest:  It was reported that the average DBH of loblolly pines in southeast Georgia is 35 centimeters.  Is there evidence that the average DBH of loblolly pines in the Wade tract A is different from this reported average for southeast Georgia?

First, before any analysis, we set up our null and alternative hypotheses.


Ho:  μ = 35 centimeters

Ha:  μ ≠ 35 centimeters


We established the hypotheses this way because we are interested in if there is a DIFFERENCE between the DBH of the trees and 35 centimeters.  Therefore, we conduct a two-sided, one-sample test.


Assumptions of the One-Sample Comparison of Means Test


The one-sample t-tests are based on a set of assumptions.  In order for our results to be valid, the assumptions of the t-tests should be met.  We make assumptions about the POPULATION, but test the assumptions with our sample data.


1.  We assume that the population of DBH measurements of trees in Wade tract A in Georgia is normally distributed.  We need to test this assumption with our sample data.  To do so, you may develop a histogram, boxplot and/or a quantile-normal plot.


2.  We also assume that the DBH measurements are independent.  This assumption may be met through the process of simple random sampling.  The presence of spatial autocorrelation (i.e., a relationship between where the tree is located in space and its DBH measurement) would suggest that independence assumption does not hold.  We do not know where these trees are located in space and therefore, based on simple random sampling, we will assume independence among measurements.


Step-by-Step STATA Guidance

One-Sample Test


The following section will walk you through the steps of conducting a one-sample mean comparison test in STATA.

1.  Download the loblolly.dta file from Sakai and place in your working directory.  Don’t forget to change your working directory in Stata (command:  cd).

2.  Be sure to start a log file (command:  log using labweek3.log).

3.  Open loblolly.dta (File→ Open).   Look at the data with Data-> Data Editor -> Data Editor (Browse).




4.  You may want to put the commands from today’s labs in a .do file so you have them for your future assignments.


5.  Develop a histogram and box plot of the DBH measurements of the loblolly pines.  You can do this through the pull-down menu or the command: histogram dbhtractA, bin(12) frequency, where dbhtractA is the name of the variable.  Along with these graphs you will also want to describe the samples with summary statistics.




As you can see in the histogram above, the DBH measurements of the loblolly pines in our sample are NOT normally distributed.  This provides little evidence in support of the assumption that the DBH measurements of the POPULATION of the DBH measurement of the loblollys in Wade Tract A is normally distributed.  It is important to remember that we make assumptions about the POPULATION!  Not meeting this assumption becomes problematic with small sample sizes (as we know from the Central Limit Theorem).  Because our sample size is 40 (not particularly large), we should be mindful of this when we interpret our results.  We could run tests of normality (i.e., Shapiro-Wilk or Kolmogorov-Smirnov) on our sample.   The t-test is not particularly robust to outliers and so we could look for outliers in our sample with the use of a box plot.  If outliers exist, it may be useful to run the test with and without the outliers in the sample to see how the outlier is affecting the results.  To learn more about test’s of normality, check out the statistics site at Indiana University.


6.   The formula for the one-sample t-statistic is:


When you run the one-sample t-test, STATA uses this formula to calculate the t-statistic.  The STATA command is ttest dbhtractA == 35.  The following results should appear in your results window.



As you can see in the results, STATA calculated the mean, standard deviation and standard error of the sample and produced a 95% confidence interval about the mean.  In addition, it calculated the t-statistic and three associated p-values that correspond with (1) a two-sided test (the middle p-value); a test whether the population mean is less than 35 cm (left handed p-value) and to test whether the population mean of DBH measurements is greater than 35cm.


We are interested in the two-sided p-value.  The two-sided p-values is the probability that  t ≤ -2.754 or  t ≥ 2.7540 (df=39). Therefore the two-sided p-value is 2*P(t ≤ -2.754) which equals 2 * 0.0044 = 0.0089.  Based on this result, we would say that we have strong evidence against the null hypothesis and in support of the alternative hypothesis.  The p-value represents the probability of getting a test statistic as extreme or even more extreme than we observed, given the null hypothesis is true.  Remember, the smaller the p-value, the stronger the evidence AGAINST the null hypothesis.  The p-value is a conditional probability statement (GIVEN or ASSUMING the null hypothesis is true).


The 95% confidence interval is (21.62cm, 32.95cm).  This interval does not cover the value of 35cm and therefore suggests that the average DBH of Tract A is different from the mean DBH of loblolly pines in the southeast Georgia.  The two-sided confidence interval and the two-sided t-test are different ways at looking at the same thing (whether the population of DBH of loblolly pines in Wade Tract A is different than 35cm).


Exercise 2.  Two-Independent Sample Test, Equal Variance


Question of Interest:  Is there evidence that the average DBH of loblolly pines in the Wade Tract A is greater than the average DBH of loblolly pines in Wade Tract B?


First, before any analysis, we set up our null and alternative hypotheses.  A represents the population of DBH measurements of loblolly pines in Wade Tract A, while B represents the DBH measurements of loblolly pines in Wade Tract B.


Ho:  μA ≤ μB

Ha:  μA > μB

1. Before we run a two-sample t –test, with equal variance, we need to check whether the two samples have roughly equal variances. We make assumptions about the populations (but can check validity based on the sample).  Before running the statistics, we also, based on our previous knowledge, believe that the two variance should be approximately equal. We use the following commands to summarize the data:


summarize dbhtractA   [We find that the standard deviation of Tract A is 17.7 cm]

summarize dbhtractB   [We find that the standard deviation of Tract A is 16.1 cm]


     2.  The standard deviations are relatively close and therefore we can use the two-sample t-test with equal variances.  We do not want one standard deviation more than twice that of the other sample.  Again, we make assumptions about the POPULATION and not the samples–we test the assumptions with the samples.


     3.  We also want to check the assumption that our populations are normally distributed.  We do this by looking at the distributions of the two samples.   Above, we have the box plot and histogram of the DBH data from Tract A.  Below we have the box plot and histogram Tract B.  Based on these graphs, the distribution of DBH measurements in the Tract B sample does not look particularly normally distributed.  The sample size is 40, again not particularly large, and therefore we should mention the lack of normality when we present our results.



4.  Now we can run the two-independent sample t-test.  The command is:

ttest dbhtractA == dbhtractB, unpaired

And you should get the following output:



As you can see in the output, STATA calculates the mean, standard deviations and standard errors for each of the samples.  We are interested in the fourth line (diff).  The difference in means between the two samples is 3.63 cm.  The 95% confidence interval of the difference is (-3.91cm, 11.17cm).  The confidence interval crosses zero, suggesting that there is NOT a difference in loblolly pine DBH between the two tracts.


Our t-statistic for the difference in the two-sample t-test with equal variances is 0.9590 (df=78).  The one-sided p-value of 0.17 does NOT provide strong evidence against the null hypothesis and therefore we CANNOT conclude that the trees in Tract A are larger on average than those of Tract B.  We fail to reject the null hypothesis or we can say the data are consistent with the null hypothesis being true (We can never outright claim that the null hypothesis is in fact true).


This page was developed by Elizabeth A. Albright, PhD of the Nicholas School of the Environment, Duke University. If you found this page helpful, check out the statistics iPhone App developed by Professor Albright.



Comments are closed.