w_sd() calculates standard deviations that accurately represent your
population by using survey weights. The standard deviation tells you how spread
out your data is around the average – a larger SD means more variation in
responses, while a smaller SD means responses cluster tightly around the mean.
Without weights, you describe spread in your sample only. With weights, you estimate how spread out values are in the entire 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("trust")- weights
Survey weights to make results representative of your population. Without weights, you get the simple sample standard deviation. Give a column name (unquoted or as a string), an expression such as
sampling_weight * 2, or a numeric vector with one weight per row.- na.rm
Remove missing values before calculating? (Default: TRUE). With
FALSE, the result for a variable that contains missing values isNA(as in base R).
Value
Population-weighted standard deviation(s) with sample size information, including the weighted SD, effective sample size (effective N), and the number of valid observations used.
Details
Understanding the Results
Weighted SD: The population-representative standard deviation. Roughly 68% of your population falls within one SD of the weighted mean.
Effective N: How many independent observations your weighted data represents. Always less than or equal to the actual sample size.
N / Missing: Valid and missing cases. With weights, both are sums of weights (displayed rounded), as SPSS reports them under
WEIGHT BY; Kish's effective N is shown bysummary().
A large difference between weighted and unweighted SD suggests that the variability in your sample does not accurately reflect the population.
When to Use This
Use w_sd() when:
You need to report how spread out a variable is in the population
You want to compare variability across groups with proper weighting
Your survey used complex sampling (oversampling, stratification)
You need SPSS-compatible weighted standard deviations
Formula
The weighted standard deviation uses the SPSS frequency weights formula:
\(s_w = \sqrt{\frac{\sum w_i (x_i - \bar{x}_w)^2}{V_1 - 1}}\)
where \(V_1 = \sum w_i\) is the sum of all weights and \(\bar{x}_w = \sum w_i x_i / V_1\) is the weighted mean.
The effective sample size is: \(n_{eff} = (\sum w_i)^2 / \sum w_i^2\)
See also
sd for the base R standard deviation function.
w_var for weighted variance (the square of weighted SD).
w_mean for weighted means.
describe for comprehensive descriptive statistics including SD.
Other weighted_statistics:
w_iqr(),
w_kurtosis(),
w_mean(),
w_median(),
w_modus(),
w_quantile(),
w_range(),
w_se(),
w_skew(),
w_var()
Examples
# Load required packages and data
library(dplyr)
data(survey_data)
# Basic weighted standard deviation
survey_data %>% w_sd(age, weights = sampling_weight)
#>
#> Weighted Standard Deviation Statistics
#> --------------------------------------
#> Weights: sampling_weight
#>
#> -------------------------------
#> Variable SD N Missing
#> -------------------------------
#> age 17.084 2516 0
#> -------------------------------
# Multiple variables
survey_data %>% w_sd(age, income, life_satisfaction, weights = sampling_weight)
#>
#> Weighted Standard Deviation Statistics
#> --------------------------------------
#> Weights: sampling_weight
#>
#> ------------------------------------------
#> Variable SD N Missing
#> ------------------------------------------
#> age 17.084 2516 0
#> income 1423.966 2201 315
#> life_satisfaction 1.152 2437 79
#> ------------------------------------------
# Grouped data
survey_data %>% group_by(region) %>% w_sd(age, weights = sampling_weight)
#>
#> Weighted Standard Deviation Statistics
#> --------------------------------------
#> Weights: sampling_weight
#>
#> Group: region = East
#> --------------------
#>
#> ------------------------------
#> Variable SD N Missing
#> ------------------------------
#> age 17.595 509 0
#> ------------------------------
#>
#> Group: region = West
#> --------------------
#>
#> -------------------------------
#> Variable SD N Missing
#> -------------------------------
#> age 16.927 2007 0
#> -------------------------------
# In summarise context
survey_data %>% summarise(sd_age = w_sd(age, weights = sampling_weight))
#> # A tibble: 1 × 1
#> sd_age
#> <dbl>
#> 1 17.1
# Unweighted (for comparison)
survey_data %>% w_sd(age)
#>
#> Standard Deviation Statistics
#> -----------------------------
#>
#> -------------------------------
#> Variable SD N Missing
#> -------------------------------
#> age 16.976 2500 0
#> -------------------------------
