Welcome to the Institute for Digital Research and Education

What statistical analysis should I use?
Statistical analyses using Stata

Version info: Code for this page was tested in Stata 12.

Introduction

This page shows how to perform a number of statistical tests using Stata.
Each section gives a brief description of the aim of the statistical test,
when it is used, an example showing the Stata commands and Stata output with
a brief interpretation of the output. You can see the page
Choosing the Correct Statistical Test for a table that shows an overview
of when each test is appropriate to use. In deciding which test is
appropriate to use, it is important to consider the type of variables that
you have (i.e., whether your variables are categorical, ordinal or interval
and whether they are normally distributed), see
What is the difference between categorical, ordinal and interval variables?
for more information on this.

About the hsb data file

Most of the examples in this page will use a data file called hsb2,
high school and beyond. This data file contains 200 observations from a
sample of high school students with demographic information about the
students, such as their gender (female), socio-economic status (ses)
and ethnic background (race). It also contains a number of scores on
standardized tests, including tests of reading (read), writing (write),
mathematics (math) and social studies (socst). You can get
the hsb2 data file from within Stata by typing:

use http://www.ats.ucla.edu/stat/stata/notes/hsb2

One sample t-test

A one sample t-test allows us to test whether a sample mean (of a
normally distributed interval variable) significantly differs from a
hypothesized value. For example, using the
hsb2
data file, say we wish to test whether the average writing score (write)
differs significantly from 50. We can do this as shown below.

The mean of the variable write for this particular sample of
students is 52.775, which is statistically significantly different from the
test value of 50. We would conclude that this group of students has a
significantly higher mean on the writing test than 50.

See also

One sample median test

A one sample median test allows us to test whether a sample median
differs significantly from a hypothesized value. We will use the same
variable, write, as we did in the
one sample t-test example above, but we do not need to assume that it is
interval and normally distributed (we only need to assume that
write
is an ordinal variable and that its distribution is symmetric). We will test whether the median writing score (write)
differs significantly from 50.

See also

Binomial test

A one sample binomial test allows us to test whether the proportion of
successes on a two-level categorical dependent variable significantly
differs from a hypothesized value. For example, using the
hsb2
data file, say we wish to test whether the proportion of females (female)
differs significantly from 50%, i.e., from .5. We can do this as shown
below.

See also

Chi-square goodness of fit

A chi-square goodness of fit test allows us to test whether the observed
proportions for a categorical variable differ from hypothesized
proportions. For example, let's suppose that we believe that the general
population consists of 10% Hispanic, 10% Asian, 10% African American and 70%
White folks. We want to test whether the observed proportions from our
sample differ significantly from these hypothesized proportions. To conduct
the chi-square goodness of fit test, you need to first download the
csgof
program that performs this test. You can download
csgof from
within Stata by typing findit csgof (see
How can I used the findit command to search for programs and get additional
help? for more information about using
findit).

See also

Two independent samples t-test

An independent samples t-test is used when you want to compare the means
of a normally distributed interval dependent variable for two independent
groups. For example, using the
hsb2
data file, say we wish to test whether the mean for
write is the
same for males and females.

The results indicate that there is a statistically significant difference
between the mean writing score for males and females (t = -3.7341, p =
.0002). In other words, females have a statistically significantly higher
mean score on writing (54.99) than males (50.12).

See also

Wilcoxon-Mann-Whitney test

The Wilcoxon-Mann-Whitney test is a non-parametric analog to the
independent samples t-test and can be used when you do not assume that the
dependent variable is a normally distributed interval variable (you only
assume that the variable is at least ordinal). You will notice that the
Stata syntax for the Wilcoxon-Mann-Whitney test is almost identical to that
of the independent samples t-test. We will use the same data file (the
hsb2
data file) and the same variables in this example as we did in the
independent t-test example above and will not assume that
write,
our dependent variable, is normally distributed.

The results suggest that there is a statistically significant difference
between the underlying distributions of the write scores of males and
the write scores of females (z = -3.329, p = 0.0009). You can
determine which group has the higher rank by looking at the how the actual
rank sums compare to the expected rank sums under the null hypothesis.
The sum of the female ranks was higher while the sum of the male ranks was
lower. Thus the female group had higher rank.

See also

Chi-square test

A chi-square test is used when you want to see if there is a relationship
between two categorical variables. In Stata, the
chi2 option is used
with the tabulate command to obtain the test statistic and its
associated p-value. Using the
hsb2
data file, let's see if there is a relationship between the type of
school attended (schtyp) and students' gender (female).
Remember that the chi-square test assumes the expected value of each cell is
five or higher. This assumption is easily met in the examples below.
However, if this assumption is not met in your data, please see the section
on Fisher's exact test below.

These results indicate that there is no statistically significant
relationship between the type of school attended and gender (chi-square with
one degree of freedom = 0.0470, p = 0.828).

Let's look at another example, this time looking at the relationship
between gender (female) and socio-economic status (ses). The
point of this example is that one (or both) variables may have more than two
levels, and that the variables do not have to have the same number of
levels. In this example,
female has two levels (male and female) and
ses has three levels (low, medium and high).

Fisher's exact test

The Fisher's exact test is used when you want to conduct a chi-square
test, but one or more of your cells has an expected frequency of five or
less. Remember that the chi-square test assumes that each cell has an
expected frequency of five or more, but the Fisher's exact test has no such
assumption and can be used regardless of how small the expected frequency
is. In the example below, we have cells with observed frequencies of two and
one, which may indicate expected frequencies that could be below five, so we
will use Fisher's exact test with the
exact option on the
tabulate
command.

These results suggest that there is not a statistically significant
relationship between race and type of school (p = 0.597). Note that the
Fisher's exact test does not have a "test statistic", but computes the
p-value directly.

See also

One-way ANOVA

A one-way analysis of variance (ANOVA) is used when you have a
categorical independent variable (with two or more categories) and a
normally distributed interval dependent variable and you wish to test for
differences in the means of the dependent variable broken down by the levels
of the independent variable. For example, using the
hsb2
data file, say we wish to test whether the mean of
write differs
between the three program types (prog). The command for this test
would be:

The mean of the dependent variable differs significantly among the levels
of program type. However, we do not know if the difference is between only
two of the levels or all three of the levels. (The F test for the
Model
is the same as the F test for
prog because
prog was the only
variable entered into the model. If other variables had also been entered,
the F test for the Model would have been different from
prog.)
To see the mean of write for each level of program type, you can use
the tabulate command with the
summarize option, as illustrated
below.

See also

Kruskal Wallis test

The Kruskal Wallis test is used when you have one independent variable
with two or more levels and an ordinal dependent variable. In other words,
it is the non-parametric version of ANOVA and a generalized form of the
Mann-Whitney test method since it permits 2 or more groups. We will use the
same data file as the
one way ANOVA example above (the
hsb2
data file) and the same variables as in the example above, but we will
not assume that write is a normally distributed interval variable.

If some of the scores receive tied ranks, then a correction factor is
used, yielding a slightly different value of chi-squared. With or without
ties, the results indicate that there is a statistically significant
difference among the three type of programs.

Paired t-test

A paired (samples) t-test is used when you have two related observations
(i.e. two observations per subject) and you want to see if the means on
these two normally distributed interval variables differ from one another.
For example, using the
hsb2
data file we will test whether the mean of
read is equal to the
mean of write.

See also

Wilcoxon signed rank sum test

The Wilcoxon signed rank sum test is the non-parametric version of a
paired samples t-test. You use the Wilcoxon signed rank sum test when you
do not wish to assume that the difference between the two variables is
interval and normally distributed (but you do assume the difference is
ordinal). We will use the same example as above, but we will not assume that
the difference between read and
write is interval and normally
distributed.

The results suggest that there is not a statistically significant
difference between read and
write.

If you believe the differences between
read and
write were
not ordinal but could merely be classified as positive and negative, then
you may want to consider a sign test in lieu of sign rank test. Again, we
will use the same variables in this example and assume that this difference
is not ordinal.

This output gives both of the one-sided tests as well as the two-sided
test. Assuming that we were looking for any difference, we would use the
two-sided test and conclude that no statistically significant difference was
found (p=.5565).

McNemar test

You would perform McNemar's test
if you were interested in the marginal frequencies of two binary outcomes.
These binary outcomes may be the same outcome variable on matched pairs
(like a case-control study) or two outcome
variables from a single group. For example, let us consider two
questions, Q1 and Q2, from a test taken by 200 students. Suppose 172
students answered both questions correctly, 15 students answered both
questions incorrectly, 7 answered Q1 correctly and Q2 incorrectly, and 6
answered Q2 correctly and Q1 incorrectly. These counts can be considered in a
two-way contingency table. The null hypothesis is that the two
questions are answered correctly or incorrectly at the same rate (or that
the contingency table is symmetric). We can enter these counts into Stata
using mcci, a command from Stata's epidemiology tables. The outcome is labeled
according to case-control study conventions.

McNemar's chi-square statistic suggests that there is not a
statistically significant difference in the proportions of correct/incorrect
answers to these two questions.

One-way repeated measures ANOVA

You would perform a one-way repeated measures analysis of variance if you
had one categorical independent variable and a normally distributed interval
dependent variable that was repeated at least twice for each subject. This
is the equivalent of the paired samples t-test, but allows for two or more
levels of the categorical variable. This tests whether the mean of the
dependent variable differs by the categorical variable. We have an example
data set called
rb4,
which is used in Kirk's book Experimental Design. In this data set,
y
is the dependent variable,
a is the repeated measure and
s is
the variable that indicates the subject number.

You will notice that this output gives four different p-values. The
"regular" (0.0001) is the p-value that you would get if you assumed compound
symmetry in the variance-covariance matrix. Because that assumption is
often not valid, the three other p-values offer various corrections (the
Huynh-Feldt, H-F, Greenhouse-Geisser, G-G and Box's conservative, Box). No
matter which p-value you use, our results indicate that we have a
statistically significant effect of
a at the .05 level.

Repeated measures logistic regression

If you have a binary outcome
measured repeatedly for each subject and you wish to run a logistic
regression that accounts for the effect of these multiple measures from each
subjects, you can perform a repeated measures logistic regression. In Stata, this can be done using the xtgee command and indicating binomial
as the probability distribution and logit as the link function to be used in
the model. The
exercise data file contains
3 pulse measurements of 30 people assigned to 2 different diet regiments and
3 different exercise regiments. If we define a "high" pulse as being over
100, we can then predict the probability of a high pulse using diet
regiment.

First, we use
xtset to define
which variable defines the repetitions. In this dataset, there are
three measurements taken for each
id, so we will use
id as our
panel variable. Then we can use
i:
before diet so that we can create indicator variables as needed.

Factorial ANOVA

A factorial ANOVA has two or more categorical independent variables
(either with or without the interactions) and a single normally distributed
interval dependent variable. For example, using the
hsb2
data file we will look at writing scores (write) as the dependent
variable and gender (female) and socio-economic status (ses)
as independent variables, and we will include an interaction of
female
by ses. Note that in Stata, you do not need to have the interaction
term(s) in your data set. Rather, you can have Stata create it/them
temporarily by placing an asterisk between the variables that will make up
the interaction term(s).

See also

Friedman test

You perform a Friedman test when you have one within-subjects independent
variable with two or more levels and a dependent variable that is not
interval and normally distributed (but at least ordinal). We will use this
test to determine if there is a difference in the reading, writing and math
scores. The null hypothesis in this test is that the distribution of the
ranks of each type of score (i.e., reading, writing and math) are the same.
To conduct the Friedman test in Stata, you need to first download the
friedman
program that performs this test. You can download
friedman
from within Stata by typing
findit friedman (see
How can I used the findit command to search for programs and get additional
help? for more information about using
findit). Also, your data
will need to be transposed such that subjects are the columns and the
variables are the rows. We will use the
xpose command to arrange our
data this way.

Friedman's chi-square has a value of 0.6175 and a p-value of 0.7344 and
is not statistically significant. Hence, there is no evidence that the
distributions of the three types of scores are different.

Ordered logistic regression

Ordered logistic regression is used when the dependent variable is
ordered, but not continuous. For example, using the hsb2 data file we will
create an ordered variable called write3. This variable will have
the values 1, 2 and 3, indicating a low, medium or high writing score. We
do not generally recommend categorizing a continuous variable in this way;
we are simply creating a variable to use for this example. We will use
gender (female), reading score (read) and social studies score
(socst) as predictor variables in this model.

The results indicate that the overall model is statistically significant
(p < .0000), as are each of the predictor variables (p < .000). There are
two cutpoints for this model because there are three levels of the outcome
variable.

One of the assumptions underlying ordinal logistic (and ordinal probit)
regression is that the relationship between each pair of outcome groups is
the same. In other words, ordinal logistic regression assumes that the
coefficients that describe the relationship between, say, the lowest versus
all higher categories of the response variable are the same as those that
describe the relationship between the next lowest category and all higher
categories, etc. This is called the proportional odds assumption or the
parallel regression assumption. Because the relationship between all pairs
of groups is the same, there is only one set of coefficients (only one
model). If this was not the case, we would need different models (such as a
generalized ordered logit model) to describe the relationship between each
pair of outcome groups. To test this assumption, we can use either the
omodel command (findit omodel, see
How can I used the findit command to search for programs and get additional
help? for more information about using
findit) or the brant command.
We will show both below.

See also

Factorial logistic regression

A factorial logistic regression is used when you have two or more
categorical independent variables but a dichotomous dependent variable. For
example, using the
hsb2
data file we will use
female as our dependent variable, because
it is the only dichotomous (0/1) variable in our data set; certainly not
because it common practice to use gender as an outcome variable. We will
use type of program (prog) and school type (schtyp) as our
predictor variables. Because
prog is a categorical variable (it has
three levels), we need to create dummy codes for it. The use of i.prog does this. You can use the
logit command if
you want to see the regression coefficients or the
logistic command
if you want to see the odds ratios.

The results indicate that the overall model is not statistically
significant (LR chi2 = 3.15, p = 0.6774). Furthermore, none of the
coefficients are statistically significant either. We can use the test
command to get the test of the overall effect of
prog as shown
below. This shows that the overall effect of
prog is not
statistically significant.

Likewise, we can use the
testparm command to get the test of the
overall effect of the prog by
schtyp interaction, as shown
below. This shows that the overall effect of this interaction is not
statistically significant.

Correlation

A correlation is useful when you want to see the linear relationship
between two (or more) normally distributed interval variables. For example,
using the
hsb2
data file we can run a correlation between two continuous variables,
read and
write.

In the second example, we will run a correlation between a dichotomous
variable, female, and a continuous variable,
write. Although
it is assumed that the variables are interval and normally distributed, we
can include dummy variables when performing correlations.

In the first example above, we see that the correlation between
read
and write is 0.5968. By squaring the correlation and then
multiplying by 100, you can determine what percentage of the variability is
shared. Let's round 0.5968 to be 0.6, which when squared would be .36,
multiplied by 100 would be 36%. Hence
read shares about 36% of its
variability with write. In the output for the second example, we can
see the correlation between
write and
female is 0.2565.
Squaring this number yields .06579225, meaning that
female shares
approximately 6.5% of its variability with
write.

Simple linear regression

Simple linear regression allows us to look at the linear relationship
between one normally distributed interval predictor and one normally
distributed interval outcome variable. For example, using the
hsb2
data file, say we wish to look at the relationship between writing
scores (write) and reading scores (read); in other words,
predicting write from
read.

We see that the relationship between
write and
read is
positive (.5517051) and based on the t-value (10.47) and p-value (0.000), we
would conclude this relationship is statistically significant. Hence, we
would say there is a statistically significant positive linear relationship
between reading and writing.

Non-parametric correlation

A Spearman correlation is used when one or both of the variables are not
assumed to be normally distributed and interval (but are assumed to be
ordinal). The values of the variables are converted in ranks and then
correlated. In our example, we will look for a relationship between
read
and write. We will not assume that both of these variables are
normal and interval .

The results suggest that the relationship between
read and
write (rho = 0.6167, p = 0.000) is statistically significant.

Simple logistic regression

Logistic regression assumes that the outcome variable is binary (i.e.,
coded as 0 and 1). We have only one variable in the
hsb2
data file that is coded 0 and 1, and that is
female. We
understand that female is a silly outcome variable (it would make
more sense to use it as a predictor variable), but we can use
female
as the outcome variable to illustrate how the code for this command is
structured and how to interpret the output. The first variable listed after
the logistic (or
logit) command is the outcome (or dependent)
variable, and all of the rest of the variables are predictor (or
independent) variables. You can use the
logit command if you want to
see the regression coefficients or the
logistic command if you want
to see the odds ratios. In our example,
female will be the outcome
variable, and read will be the predictor variable. As with OLS
regression, the predictor variables must be either dichotomous or
continuous; they cannot be categorical.

Multiple regression

Multiple regression is very similar to simple regression, except that in
multiple regression you have more than one predictor variable in the
equation. For example, using the
hsb2
data file we will predict writing score from gender (female),
reading, math, science and social studies (socst) scores.

Analysis of covariance

Analysis of covariance is like ANOVA, except in addition to the
categorical predictors you also have continuous predictors as well. For
example, the
one way ANOVA example used
write as the dependent variable and
prog as the independent variable. Let's add
read as a continuous
variable to this model, as shown below.

See also

Multiple logistic regression is like simple logistic regression, except
that there are two or more predictors. The predictors can be interval
variables or dummy variables, but cannot be categorical variables. If you
have categorical predictors, they should be coded into one or more dummy
variables. We have only one variable in our data set that is coded 0 and 1,
and that is female. We understand that
female is a silly
outcome variable (it would make more sense to use it as a predictor
variable), but we can use
female as the outcome variable to
illustrate how the code for this command is structured and how to interpret
the output. The first variable listed after the
logistic (or
logit) command is the outcome (or dependent) variable, and all of the
rest of the variables are predictor (or independent) variables. You can use
the logit command if you want to see the regression coefficients or
the logistic command if you want to see the odds ratios. In our
example, female will be the outcome variable, and
read and
write will be the predictor variables.

Discriminant analysis

Discriminant analysis is used when you have one or more normally
distributed interval independent variables and a categorical dependent
variable. It is a multivariate technique that considers the latent
dimensions in the independent variables for predicting group membership in
the categorical dependent variable. For example, using the
hsb2
data file, say we wish to use
read,
write and
math
scores to predict the type of program a student belongs to (prog).
For this analysis, you need to first download the
daoneway
program
that performs this test. You can download
daoneway from within Stata
by typing findit daoneway (see
How can I used the findit command to search for programs and get additional
help? for more information about using
findit).

Clearly, the Stata output for this procedure is lengthy, and it is beyond
the scope of this page to explain all of it. However, the main point is
that two canonical variables are identified by the analysis, the first of
which seems to be more related to program type than the second. For more
information, see this page on
discriminant
function analysis.

See also

One-way MANOVA

MANOVA (multivariate analysis of variance) is like ANOVA, except that
there are two or more dependent variables. In a one-way MANOVA, there is one
categorical independent variable and two or more dependent variables. For
example, using the
hsb2
data file, say we wish to examine the differences in
read,
write and
math broken down by program type (prog). For
this analysis, you can use the
manova
command and then perform the
analysis like this.

This command produces three different test statistics that are used to
evaluate the statistical significance of the relationship between the
independent variable and the outcome variables. According to all three
criteria, the students in the different programs differ in their joint
distribution of read,
write and
math.

Multivariate multiple regression

Multivariate multiple regression is used when you have two or more
dependent variables that are to be predicted from two or more predictor variables. In
our example, we will predict
write and
read from
female,
math,
science and social studies (socst) scores.

Many researchers familiar with traditional multivariate analysis may not
recognize the tests above. They do not see Wilks' Lambda, Pillai's Trace or
the Hotelling-Lawley Trace statistics, the statistics with which they are
familiar. It is possible to obtain these statistics using the
mvtest
command written by David E. Moore of the University of Cincinnati.
UCLA updated this command to work with Stata 6 and above. You can download
mvtest from within Stata by typing findit mvtest (see
How can I used the findit command to search for programs and get additional
help? for more information about using
findit).

Canonical correlation

Canonical correlation is a multivariate technique used to examine the
relationship between two groups of variables. For each set of variables, it
creates latent variables and looks at the relationships among the latent
variables. It assumes that all variables in the model are interval and
normally distributed. Stata requires that each of the two groups of
variables be enclosed in parentheses. There need not be an equal number of
variables in the two groups.

The output above shows the linear combinations corresponding to the first
canonical correlation. At the bottom of the output are the two canonical
correlations. These results indicate that the first canonical correlation
is .7728. You will note that Stata is brief and may not provide you with
all of the information that you may want. Several programs have been
developed to provide more information regarding the analysis. You can
download this family of programs by typing
findit cancor (see
How can I used the findit command to search for programs and get additional
help? for more information about using
findit).

Because the output from the
cancor command is lengthy, we will use
the cantest command to obtain the eigenvalues, F-tests and associated
p-values that we want. Note that you do not have to specify a model with
either the cancor or the
cantest commands if they are issued
after the canon command.

The F-test in this output tests the hypothesis that the first canonical
correlation is equal to zero. Clearly, F = 56.4706 is statistically
significant. However, the second canonical correlation of .0235 is not
statistically significantly different from zero (F = 0.1087, p = 0.7420).

See also

Factor analysis

Factor analysis is a form of exploratory multivariate analysis that is
used to either reduce the number of variables in a model or to detect
relationships among variables. All variables involved in the factor
analysis need to be continuous and are assumed to be normally distributed.
The goal of the analysis is to try to identify factors which underlie the
variables. There may be fewer factors than variables, but there may not be
more factors than variables. For our example, let's suppose that we think
that there are some common factors underlying the various test scores. We
will first use the principal components method of extraction (by using the
pc option) and then the principal components factor method of
extraction (by using the pcf option). This parallels the output
produced by SAS and SPSS.

Note that by default, Stata will retain all factors with positive
eigenvalues; hence the use of the
mineigen option or the
factors(#) option. The
factors(#) option does not specify the
number of solutions to retain, but rather the largest number of solutions to
retain. From the table of factor loadings, we can see that all five of the
test scores load onto the first factor, while all five tend to load not so
heavily on the second factor. Uniqueness (which is the opposite of
commonality) is the proportion of variance of the variable (i.e.,
read)
that is not accounted for by all of the factors taken together, and a very
high uniqueness can indicate that a variable may not belong with any of the
factors. Factor loadings are often rotated in an attempt to make them more
interpretable. Stata performs both varimax and promax rotations.

The purpose of rotating the factors is to get the variables to load either
very high or very low on each factor. In this example, because all of the
variables loaded onto factor 1 and not on factor 2, the rotation did not aid
in the interpretation. Instead, it made the results even more difficult to
interpret.

To obtain a scree plot of the eigenvalues, you can use the greigen
command. We have included a reference line on the y-axis at one to aid in
determining how many factors should be retained.