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.
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)
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.
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.
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
# Load required packages and data
library(dplyr)
data(survey_data)
# Basic weighted median
survey_data %>% w_median(age, weights = sampling_weight)
#>
#> Weighted Median Statistics
#> --------------------------
#>
#> --- age ---
#> Variable weighted_median Effective_N
#> age 50 2468.8
#>
# Multiple variables
survey_data %>% w_median(age, income, weights = sampling_weight)
#>
#> Weighted Median Statistics
#> --------------------------
#>
#> --- age ---
#> Variable weighted_median Effective_N
#> age 50 2468.8
#>
#> --- income ---
#> Variable weighted_median Effective_N
#> income 3500 2158.9
#>
# Grouped data
survey_data %>% group_by(region) %>% w_median(age, weights = sampling_weight)
#>
#> Weighted Median Statistics
#> --------------------------
#>
#> Group: region = East
#>
#> --- age ---
#> Variable weighted_median Effective_N
#> age 53 477
#>
#> Group: region = West
#>
#> --- age ---
#> Variable weighted_median Effective_N
#> age 49 1993.1
#>
# In summarise context
survey_data %>% summarise(med_age = w_median(age, weights = sampling_weight))
#> # A tibble: 1 × 1
#> med_age
#> <dbl>
#> 1 50
# Unweighted (for comparison)
survey_data %>% w_median(age)
#>
#> Median Statistics
#> -----------------
#>
#> --- age ---
#> Variable median N
#> age 50 2500
#>
