Learn R Programming

mariposa (version 0.7.3)

mann_whitney: Compare Two Groups Without Assuming Normal Data

Description

mann_whitney() compares two groups when your data isn't normally distributed or when you have ordinal data (like ratings or rankings). It's the go-to alternative when t-tests aren't appropriate.

Think of it as:

  • A robust way to compare groups that works with any data shape

  • Perfect for Likert scales, ratings, or skewed distributions

  • A test that compares the typical values between groups

The test tells you:

  • Whether one group tends to have higher values than the other

  • How strong the difference is (effect size)

  • Which group has higher average ranks

Usage

mann_whitney(
  data,
  ...,
  group,
  weights = NULL,
  mu = 0,
  alternative = c("two.sided", "less", "greater"),
  conf.level = 0.95
)

Value

Test results showing whether groups differ, including:

  • U and W statistics (test statistics)

  • Z-score and p-value (are groups different?)

  • Effect size r (r_effect, how big is the difference?)

  • Rank means for each group (which group is higher?) Use summary() for the full SPSS-style output with toggleable sections.

Arguments

data

Your survey data (a data frame or tibble)

...

The variables you want to compare between groups. You can list multiple variables or use helpers like starts_with("satisfaction")

group

The categorical variable that defines your two groups (e.g., gender, treatment/control). Must have exactly two groups.

weights

Optional survey weights for population-representative results

mu

The hypothesized difference (Default: 0, meaning no difference)

alternative

Direction of the test:

  • "two.sided" (default): Test if groups are different

  • "greater": Test if group 1 > group 2

  • "less": Test if group 1 < group 2

conf.level

Confidence level for intervals (Default: 0.95 = 95%)

Details

Understanding the Results

P-value: If p < 0.05, the groups are significantly different

  • p < 0.001: Very strong evidence of difference

  • p < 0.01: Strong evidence of difference

  • p < 0.05: Moderate evidence of difference

  • p >= 0.05: No significant difference found

Effect Size r (How big is the difference?):

  • |r| < 0.1: Negligible difference

  • |r| ~ 0.1: Small difference

  • |r| ~ 0.3: Medium difference

  • |r| ~ 0.5: Large difference

  • |r| > 0.5: Very large difference

Rank Mean Difference:

  • Positive: Group 1 tends to have higher values

  • Negative: Group 2 tends to have higher values

  • Zero: Groups have similar distributions

When to Use This

Use Mann-Whitney test when:

  • Your data is not normally distributed (skewed, outliers)

  • You have ordinal data (rankings, Likert scales)

  • Sample sizes are small (< 30 per group)

  • You want a robust alternative to the t-test

  • You're comparing satisfaction ratings, income, or other skewed variables

Advantages Over t-test

  • Works with any data distribution

  • Not affected by outliers

  • Valid for ordinal data

  • No assumptions about variance

  • More robust for real-world data

Tips for Success

  • Each group should have at least 5 observations

  • The test compares distributions, not just means

  • Look at both p-values and effect sizes

  • Consider plotting the data to see the pattern

Weighted variants

The weighted test is the design-based Lumley-Scott (2013) rank test (Horvitz-Thompson midranks with a sandwich variance), validated against survey::svyranktest() rather than SPSS: SPSS NPAR TESTS ignores WEIGHT BY, so no SPSS reference exists for weighted results. The weighted descriptive U and W are design-based rank quantities and may differ from SPSS's expanded-data U; the Z statistic and p-value are the validated quantities.

  • Use this for Likert scales and rating data

References

Mann, H. B., & Whitney, D. R. (1947). On a test of whether one of two random variables is stochastically larger than the other. The Annals of Mathematical Statistics, 18(1), 50-60.

Wilcoxon, F. (1945). Individual comparisons by ranking methods. Biometrics Bulletin, 1(6), 80-83.

Lumley, T., & Scott, A. (2013). Two-sample rank tests under complex sampling. Biometrika, 100(4), 831-842.

See Also

wilcox.test for the base R Wilcoxon test function.

svyranktest for survey-weighted rank tests.

t_test for parametric t-tests.

summary.mann_whitney for detailed output with toggleable sections.

Other hypothesis_tests: ancova(), binomial_test(), chi_square(), chisq_gof(), factorial_anova(), fisher_test(), friedman_test(), kruskal_wallis(), mcnemar_test(), oneway_anova(), t_test(), wilcoxon_test()

Examples

Run this code
# Load required packages and data
library(dplyr)
data(survey_data)

# Basic Mann-Whitney test (non-parametric comparison)
survey_data %>%
  mann_whitney(age, group = gender)

# Multiple variables
survey_data %>%
  mann_whitney(age, income, life_satisfaction, group = region)

# Using tidyselect helpers
survey_data %>%
  mann_whitney(starts_with("trust_"), group = gender)

# Weighted analysis
survey_data %>%
  mann_whitney(income, group = region, weights = sampling_weight)

# Grouped analysis (separate tests for each education level)
survey_data %>%
  group_by(education) %>%
  mann_whitney(life_satisfaction, group = gender)

# One-sided test
survey_data %>%
  mann_whitney(life_satisfaction, group = region, alternative = "greater")

# --- Three-layer output ---
result <- survey_data %>%
  mann_whitney(income, group = gender, weights = sampling_weight)
result              # compact one-line overview
summary(result)     # full detailed output with all sections
summary(result, effect_sizes = FALSE)  # hide effect sizes

Run the code above in your browser using DataLab