Estimate the ratio of two survey-weighted totals (numerator / denominator)
for a survey design object. Uses the delta method (linearization) for
variance estimation for Taylor, SRS, calibrated, and two-phase designs, and
direct per-replicate computation for replicate-weight designs. Both
approaches are equivalent to survey::svyratio() for their respective
design types.
Supports optional grouping, domain estimation, and all five survey design
classes.
get_ratios(
design,
numerator,
denominator,
group = NULL,
variance = "ci",
conf_level = 0.95,
n_weighted = FALSE,
decimals = NULL,
min_cell_n = 30L,
na.rm = TRUE,
label_values = TRUE,
label_vars = TRUE,
name_style = "surveycore",
...,
.id = NULL,
.if_missing_var = NULL
)A survey_ratios tibble (also inheriting survey_result).
[group_cols...] — group variable columns (when active), first.
ratio — estimated ratio (weighted total of numerator / weighted
total of denominator).
Variance columns (se, var, cv, ci_low, ci_high, moe,
deff) — only those requested via variance.
n — unweighted count of rows where both numerator and denominator
are non-NA.
n_weighted — sum of weights (only when requested).
Numerator and denominator variable names are stored in meta(result), not
as output columns. Use meta(result)$numerator and
meta(result)$denominator to access them.
A survey design object: survey_taylor, survey_replicate,
survey_twophase, or survey_nonprob.
<tidy-select> A single unquoted
numeric variable name for the numerator. Must resolve to exactly one
numeric column.
<tidy-select> A single unquoted
numeric variable name for the denominator. Must resolve to exactly one
numeric column. All in-domain values must not sum to zero.
<tidy-select> Optional grouping
variable(s). Combined with any grouping set by group_by(). Rows where
the grouping variable is NA are excluded from all groups and do not
appear in the output. This matches dplyr::group_by() semantics.
Default NULL.
NULL or a character vector from "se", "ci", "var",
"cv", "moe", "deff". Controls which uncertainty columns appear in
the output. Default "ci".
Numeric scalar in (0, 1). Confidence level for
confidence intervals. Default 0.95.
Logical. If TRUE, add an n_weighted column with the
sum of weights for rows where both numerator and denominator are non-NA
in each group. Default FALSE.
Integer or NULL. If an integer, rounds all numeric output
columns (e.g., ratio, se, ci_low, ci_high) to this many decimal
places. Default NULL (no rounding).
Integer. Minimum unweighted cell count before
surveycore_warning_small_cell fires. Default 30L (AAPOR guidance).
Logical. If TRUE (default), NA values are excluded from
analysis: observations where the analysis variable is NA are dropped
from calculations, and observations where any group variable is NA are
excluded from the output. If FALSE, NA observations in the analysis
variable are included in calculations, and observations where a group
variable is NA are collected into their own group row in the output
(appearing after all non-NA group rows).
Logical. Accepted for API uniformity; has no visible
effect on get_ratios() output. Default TRUE.
Logical. Accepted for API uniformity; has no visible
effect on get_ratios() output. Default TRUE.
"surveycore" (default) or "broom". When "broom",
renames ratio → estimate, se → std.error, ci_low →
conf.low, ci_high → conf.high.
Unused. Reserved so that .id and .if_missing_var remain
named-only when a survey_collection is passed as design.
Character(1) or NULL. Column name used to identify each
survey when design is a survey_collection. For collection inputs,
NULL (the default) resolves to the collection's stored @id property.
Pass a non-NULL value to override. Ignored when design is a single
survey.
"error", "skip", or NULL. How to handle
surveys in a collection that lack one of the requested NSE variables.
For collection inputs, NULL (the default) resolves to the collection's
stored @if_missing_var property. Pass a non-NULL value to override.
Ignored when design is a single survey.
Other analysis:
clean(),
get_anova(),
get_corr(),
get_covariance(),
get_diffs(),
get_freqs(),
get_means(),
get_pairwise(),
get_quantiles(),
get_t_test(),
get_totals(),
get_variance(),
meta()
d <- as_survey(pew_npors_2025, weights = weight, strata = stratum)
# Ratio of prayer frequency to in-person attendance frequency
get_ratios(d, numerator = pray, denominator = attendper)
# With grouped estimates
get_ratios(d, pray, attendper, group = gender)
# AAPOR-compliant output
get_ratios(d, pray, attendper, variance = c("ci", "moe"), n_weighted = TRUE)
Run the code above in your browser using DataLab