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).
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
#> Warning: Unknown or uninitialised column: `Variable`.
#>
#> Group: region = West
#> Warning: Unknown or uninitialised column: `Variable`.
#>
# 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
#>
