Opens in a new tab

Kruskal-Wallis H Test in Stata
The Ties Line and Follow-Up Comparisons

A Kruskal-Wallis H test in Stata comes from kwallis, which prints the statistic with and without the ties adjustment. It has no built-in pairwise follow-up, so a community-contributed command such as dunntest, with a named adjustment, is the usual route.

What You'll Need
An ordinal or continuous outcome
A grouping variable, three or more
Internet access to install dunntest

IN SHORT

What kwallis Tests and What It Does Not Follow Up

A Kruskal-Wallis H test compares three or more independent groups through the ranks of an ordinal or continuous outcome, testing whether the groups differ in their distributions, which is usually read as one group tending to give higher values than another. To run a Kruskal-Wallis H test in Stata, type kwallis followed by the outcome, with by() naming the grouping variable. The output gives the rank sum for each group and the chi-squared statistic twice, without and with the ties adjustment. Pairwise follow-ups come from a community-contributed command, dunntest. OnlineSPSS, a specialist statistical consultancy for academic, clinical and professional research, carries out and reviews this analysis through an in-house PhD-level statistician.

In This Guide

1
Compare the Groups' Distributions
2
Check the Size of Each Group
3
Run kwallis With by()
4
Report the Ties-Adjusted Statistic
5
Install and Run dunntest
6
Read the Adjusted Comparisons

ANALYSIS SUPPORT

Analysis Support for This Method

A rank test across several groups yields a p-value but no difference expressed in the outcome's units. When the group means are what the question concerns, a one-way ANOVA estimates those differences, with intervals from adjusted comparisons.

Kruskal-Wallis Test

A rank-based comparison of three or more groups, from the question the test answers to the follow-up comparisons and the sentence that reports them.

Explore Kruskal-Wallis Test
One-Way ANOVA

Differences among three or more means estimated with adjusted comparisons, appropriate when the mean is the quantity that the research question concerns.

Explore One-Way ANOVA

BEFORE YOU RUN IT

What kwallis Assumes About the Groups

The test is rank-based, which changes what the null hypothesis says, so the question, the independence of the groups and the follow-up plan are settled first.

Independent Groups Each participant belongs to one group and appears once. Repeated measurements on the same people need the Friedman test or another model for dependent data.

A Question About Ranks A significant result says the groups differ in their distributions, typically one tending to higher values. It is a statement about medians only if the distributions share the same shape.

Group Sizes Known The chi-squared reference distribution is an approximation that can be poor when groups are very small. With a handful of cases per group, an exact approach is considered.

A Plan for Follow-Ups Decide whether all pairs, or each group against a reference group, will be compared, and which adjustment controls the error rate. kwallis itself supplies none of these comparisons.

IN STATA

How to Run a Kruskal-Wallis H Test in Stata

The kwallis command in Stata is supplied with Stata. The follow-up command dunntest does not, and it is installed once from the SSC archive or the author's site before its first use.

1

Compare the Groups' Distributions

Plot the outcome by group and compare shapes, spread and the number of tied values before any test is run.

Watch: Similar shapes make a difference in location straightforward to describe. Different shapes make the result a general tendency toward higher values, and the wording is chosen accordingly.

2

Check the Size of Each Group

Tabulate the grouping variable to see how many cases each group has after missing values are removed.

Check: kwallis uses only rows with both variables observed. A group that shrinks to a few cases after missing values changes how far the chi-squared approximation can be trusted.

3

Run kwallis With by()

Run kwallis with the outcome and by() naming the group variable, then read the rank sum for each group.

Tip: Larger groups have larger rank sums by default. Dividing each rank sum by its Obs gives the mean rank, which is the figure that shows which group tends toward higher values.

4

Report the Ties-Adjusted Statistic

Use the chi-squared with ties, which kwallis prints beneath the unadjusted value, and note its degrees of freedom.

Check: When no values are tied, the two statistics are equal. With many ties, as in Likert items, the adjusted line is the one that matches the data.

5

Install and Run dunntest

Install dunntest once to run Dunn’s test in Stata, then call it with the same outcome and by() variable and a named adjustment in ma().

Watch: dunntest is community-contributed, so the command and its version are cited in the methods. Its options and the layout of its table can differ between versions.

6

Read the Adjusted Comparisons

Read each pairwise z statistic with its adjusted p-value, and report which pairs differ under the named adjustment.

Check: Documented versions of dunntest print one-tailed p-values, meant to be compared with alpha divided by 2, unless the altp option is given, which prints two-sided values. Confirm this for the installed version before any p is quoted.

Stata Commands
graph box y, over(group)
tabulate group
kwallis y, by(group)
ssc install dunntest
dunntest y, by(group) ma(holm)

READING THE OUTPUT

Reading the kwallis Block and the dunntest Table

The kwallis block repeats its statistic with and without a ties adjustment, and the pairwise table appears only when the second command is run. Labels quoted are those of older releases; the wording varies by release.

Output element What it tells you What to check
Header: Kruskal-Wallis equality-of-populations rank test That the rank test ran on the groups named by by() That the grouping variable is the intended one
Group, Obs and Rank Sum The size of each group and its sum of ranks in the pooled sample That Obs matches the intended n and that mean ranks, Rank Sum over Obs, are compared
First chi-squared with its d.f. and probability The statistic and p-value without the adjustment for ties That it is used only when no values are tied
chi-squared with ties and probability The statistic and p-value adjusted for tied ranks That it is the line reported when ties exist
d.f. The number of groups minus one That it equals the groups actually analyzed
Rank Sum divided by Obs (computed by hand) The mean rank of each group, which shows the direction of the difference That the group with the higher mean rank is named in words
dunntest table (layout varies by version) A z statistic and an adjusted p-value for each pair of groups That the adjustment named in the text is the one passed to ma()

REPORTING

Reporting the H Statistic and the Pairs That Differ

The omnibus line and the pairwise table come from two commands, so the sentence names both and the adjustment that links them.

Report H from the line with ties as H(df) = ___, p = ___, with the sample size of each group and the rule used for missing values. Describe the result as a difference in the distributions, or in the tendency toward higher values, and mention medians only when the group shapes are alike. Give the median and interquartile range of each group as descriptives, and an effect size such as epsilon-squared = ___ computed from H with its formula named, because kwallis does not print one. For the follow-ups, name dunntest, its version and the adjustment, and give the adjusted p-value for each pair that is reported, stating whether it is one- or two-sided.

H With df and pTies Line UsedGroup Medians and IQRAdjustment NamedCommand Version Cited

COMMON MISTAKES

Claims the kwallis Output Cannot Carry

Five of the six concern wording or follow-up rather than the omnibus statistic itself, which kwallis computes correctly once the groups are right.

Medians Claimed Without Equal Shapes Describing the result as a difference in medians holds only when the group distributions share a shape. Otherwise the finding concerns the ranks, and the sentence says so.

Unadjusted Pairwise Tests Running a rank-sum test on each pair without an adjustment inflates the chance of a false finding across the family of comparisons. One named adjustment is applied to all pairs.

Pairs Searched After the Omnibus Test A pair singled out after scanning the table, outside the comparisons named in the plan, is reported as exploratory.

Unadjusted Statistic Quoted The first chi-squared line leaves out the ties adjustment. With many tied values it understates the statistic, so the line with ties is the one in the report.

Community Command Left Uncited dunntest is not part of official Stata. A result that depends on it, without the command, version and adjustment named, cannot be reproduced by a reader.

Effect Size Missing kwallis prints no effect size, so a report with only H and p leaves the size of the difference unstated. An effect size computed from H, with its formula named, completes it.

Omnibus test reported but the pairs unaddressed? Quantitative Data Analysis runs the follow-up comparisons with an adjustment matched to your groups.

Request an Assessment

What's Next

Only two independent groups are compared with the Mann-Whitney U Test.
Ranks of the same participants across several conditions are tested with the Friedman Test.
Adjusting an ordered outcome for covariates leads to Ordinal Regression.

FREQUENTLY ASKED QUESTIONS

Questions About kwallis and Dunn's Test

kwallis stops at the omnibus test and prints that statistic twice, and Dunn's test, which comes from elsewhere, ranks the data differently from pair-by-pair rank-sum tests.

No: kwallis reports only the omnibus test, so pairwise comparisons come from a community-contributed command such as dunntest. That command is installed separately, runs Dunn’s test with an adjustment that you name, and relies on the ranks of the whole sample. Cite it and its version in the methods.

Report the line with ties, because it adjusts for tied ranks, and the two lines are equal when no values are tied. With ordinal scales and rounded measurements, ties are common, and the adjusted statistic is the one that matches the data. State the degrees of freedom as printed.

Dunn’s test uses the ranks from the whole sample, whereas ranksum on a pair ranks only the cases in that pair, so the statistics differ. Both need an adjustment for the number of comparisons. Using the whole-sample ranks keeps the follow-up consistent with the omnibus test that preceded it.

Yes. An in-house PhD-level statistician at OnlineSPSS can take the whole sequence: checking the groups, running kwallis and the follow-up with an adjustment suited to your design, and preparing the do-file. For the write-up itself there is Results Reporting & Interpretation, a service of its own, and interpreting the group differences for your field is your part.

Your data and project details are handled confidentially, and project information is not published. Researcher names and institutions are not used publicly without permission. An NDA is available where required and can be arranged before sensitive materials are shared. Read more about confidentiality and data security.

You share your research question, design and relevant materials, such as the dataset or the output you have, and a statistically trained Research Coordinator reviews the request and responds the same day. The analysis, deliverables, timeline and one fixed project fee are then confirmed before any work begins, so the timeline follows from the agreed scope.

Still have a question? Find more answers in our full FAQ.

View All FAQs →

NEXT STEP

Which Pairs Differ, and
Under Which Adjustment?

Pairwise claims from dunntest should rest on the same rows as the kwallis result, with the adjustment and the sidedness of each p-value written beside them in one do-file.

No Commitment Required

Strictly Confidential

Fixed Project Fee

In-House PhD-Level Statisticians

Same-Day Response

Serving Researchers Since 2014