Data @ Reed

Chi-square tests

Chi-square tests are non-parametric analyses that evaluate frequencies in a sample and compare those to the expected frequencies in a population. Chi-square goodness-of-fit tests look at one variable, while a chi-square difference of means test looks at two variables. 

Chi-square test, goodness of fit

A chi-square goodness of fit compares your observed values to expected values. For this example, we will look at the 1988 NLSW (National Bureau of Labor + Statistics, Young Women dataset) data and use the csgof package for our analysis.

First, install the csgof package. In Stata, type

findit csgof

and click on "csgof from https://stats.oarc.ucla.edu/stat/stata/ado/analysis". This will take you to a second screen in Stata; click on "click here to install", install the package, and then return to the command line.

Load your data and look at your data (using the browse command).

sysuse nlsw88

br

Based on other information, you hypothesize that the people captured by this dataset are mostly "race = white", with smaller frequencies of people coded as "race = black" and an even smaller group of "race = other".

csgof race, expperc(75 20 5)

These results show that the racial composition in your sample does not match your expectations.

Beyond the basic Chi-squared test 

If you need more than csgof offers — for example, testing subgroups with if, running an exact test on a small sample instead of relying on the large-sample chi-square approximation, or adjusting for a complex survey design — the mgof package is a good next step. It runs the same basic test but is more actively maintained and more flexible.

Install it (it depends on another package, moremata):

ssc install moremata
ssc install mgof

Using the same hypothesis as above, create a variable holding your expected percentages, then run mgof:

recode race (1=75) (2=20) (3=5), gen(hypothesized)
mgof race = hypothesized, freq

The freq option displays a table of observed vs. expected counts alongside the test statistics, similar to csgof's output. A few things mgof can do that csgof can't:

  • Restrict the test to a subgroup: mgof race = hypothesized if south==1
  • Run an exact test for small samples instead of the standard chi-square approximation: add the mc (Monte Carlo) or ee (exhaustive enumeration) option
  • Adjust for survey design effects with the svy option, if your data is svyset

See help mgof for the full set of options.

Chi-square test, independence

Also known as the chi-square test for a difference of means, this test examines the relationship between two categorical variables. In this example, I will look at the stock Stata dataset of automobile repair data from 1978 and see if there is a relationship between a car's repair rating and whether or not it was produced in the US.

sysuse auto

tab rep78 foreign, chi2

If you wanted to see row percentages instead of frequencies, specify that in the options:

tab rep78 foreign, row nofreq chi2