Learn R Programming

mariposa (version 0.7.3)

w_median: Find the Population-Representative Middle Value

Description

w_median() finds the median (middle value) of your data using survey weights. The weighted median is the value where half the population falls below and half falls above. Unlike the mean, the median is not pulled by extreme values, making it a robust measure of the "typical" value in your population.

Usage

w_median(data, ..., weights = NULL, na.rm = TRUE)

Value

Population-weighted median(s) with sample size information, including the weighted median, effective sample size (effective N), and the number of valid observations used.

Arguments

data

Your survey data (a data frame or tibble)

...

The numeric variables you want to analyze. You can list multiple variables or use helpers like starts_with("income")

weights

Survey weights to make results representative of your population. Without weights, you get the simple sample median.

na.rm

Remove missing values before calculating? (Default: TRUE)

Details

Understanding the Results

  • Weighted Median: The value that splits the weighted population in half. 50% of the population (by weight) falls below this value, 50% above.

  • Effective N: How many independent observations your weighted data represents.

  • N: The actual number of observations used.

Comparing the weighted median to the weighted mean is informative:

  • If they are similar, the distribution is roughly symmetric.

  • If the mean is much larger than the median, the distribution is right-skewed (a few very high values pull the mean up, e.g., income).

When to Use This

Use w_median() when:

  • Your data has outliers or is skewed (e.g., income, housing prices)

  • You want a robust "typical value" not influenced by extremes

  • You need the weighted 50th percentile

  • You need SPSS-compatible weighted median values

Formula

The weighted median is the weighted 50th percentile, computed with the SPSS HAVERAGE (quantile Type 6) position h = 0.5 * (W + 1) on the cumulative weights, with linear interpolation between bracketing observations. It is identical to w_quantile(x, probs = 0.5) by construction. Unweighted values match SPSS FREQUENCIES; weighted values with non-integer weights are an R-internal extension of the HAVERAGE rule (Tier 4 of the Validation Charter) pending SPSS reference runs.

References

IBM Corp. (2023). IBM SPSS Statistics 29 Algorithms. IBM Corporation.

See Also

median for the base R median function.

w_mean for weighted means.

w_quantile for arbitrary weighted percentiles.

describe for comprehensive descriptive statistics including the median.

Other weighted_statistics: w_iqr(), w_kurtosis(), w_mean(), w_modus(), w_quantile(), w_range(), w_sd(), w_se(), w_skew(), w_var()

Examples

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

# Basic weighted median
survey_data %>% w_median(age, weights = sampling_weight)

# Multiple variables
survey_data %>% w_median(age, income, weights = sampling_weight)

# Grouped data
survey_data %>% group_by(region) %>% w_median(age, weights = sampling_weight)

# In summarise context
survey_data %>% summarise(med_age = w_median(age, weights = sampling_weight))

# Unweighted (for comparison)
survey_data %>% w_median(age)

Run the code above in your browser using DataLab