Compares the weighted means of two groups using a design-based t-test.
Follows the mathematical model of survey::svyttest() but uses
surveycore's own variance machinery (survey_glm()). Supports all four
survey design classes and optional subgroup analysis via group.
Usage
get_t_test(
design,
x,
by,
group = NULL,
conf_level = 0.95,
variance = "ci",
na.rm = TRUE,
min_cell_n = 30L,
decimals = NULL,
label_values = TRUE,
label_vars = TRUE,
name_style = "surveycore",
...,
.id = NULL,
.if_missing_var = NULL
)Arguments
- design
A survey design object:
survey_taylor,survey_replicate,survey_twophase, orsurvey_nonprob.- x
<
tidy-select> A single unquoted numeric variable name for the outcome variable. Must resolve to exactly one numeric column.- by
<
tidy-select> A single unquoted variable name for the grouping variable. Must produce a model matrix with exactly 2 columns after fitting (intercept + one binary indicator). Character, integer, and logical columns are coerced to factor with a warning. Ordered factors are accepted as-is.- group
<
tidy-select> Optional subgroup variable(s). When supplied, the t-test is run separately within each unique combination of group values. Combined with any grouping set bygroup_by(). DefaultNULL.- conf_level
Numeric(1). Confidence level strictly in (0, 1). Default
0.95.- variance
Character. Which uncertainty columns to include. Valid values:
"se","ci". Default"ci". Both may be requested:c("se", "ci").- na.rm
Logical(1). Accepted for API uniformity with other
get_*()functions. NA rows inxorbyare always excluded (the GLM requires complete cases). DefaultTRUE.- min_cell_n
Integer(1). Warn when either group has fewer than this many unweighted observations. Default
30L. Use0Lto suppress.- decimals
Integer(1) or
NULL. Round all double output columns to this many decimal places.NULL= no rounding. DefaultNULL.- label_values
Logical(1). When
TRUE(default), convertbyandgroupfactor codes to their value labels in the output.- label_vars
Logical(1). Accepted for API uniformity; has no visible effect because column names are fixed. Default
TRUE.- name_style
Character(1). Output column naming style.
"surveycore"(default) or"broom"(renamessetostd.error,ci_lowtoconf.low,ci_hightoconf.high,p_valuetop.value,dftoparameter).t_statis not renamed.- ...
Unused. Reserved so that
.idand.if_missing_varremain named-only when asurvey_collectionis passed asdesign.- .id
Character(1) or
NULL. Column name used to identify each survey whendesignis asurvey_collection. For collection inputs,NULL(the default) resolves to the collection's stored@idproperty. Pass a non-NULLvalue to override. Ignored whendesignis a single survey.- .if_missing_var
"error","skip", orNULL. 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_varproperty. Pass a non-NULLvalue to override. Ignored whendesignis a single survey.
Value
A survey_t_test tibble (also inheriting survey_result).
Columns: group columns (when active), level_a, level_b,
estimate, mean_a, mean_b, n_a, n_b, se (optional),
ci_low (optional), ci_high (optional), t_stat, df,
p_value, stars. Use meta() to access design type, conf_level,
and variable metadata.
See also
Other analysis:
clean(),
get_anova(),
get_corr(),
get_covariance(),
get_diffs(),
get_freqs(),
get_means(),
get_pairwise(),
get_quantiles(),
get_ratios(),
get_totals(),
get_variance(),
meta()
Examples
gss_sub <- gss_2024[gss_2024$sex %in% c(1L, 2L) & !is.na(gss_2024$age), ]
gss_sub$sex <- factor(gss_sub$sex, levels = c(1, 2), labels = c("Male", "Female"))
gss_design <- as_survey(gss_sub,
ids = vpsu, weights = wtssps, strata = vstrat, nest = TRUE)
get_t_test(gss_design, age, by = sex)
#> # A tibble: 1 × 13
#> level_a level_b estimate mean_a mean_b n_a n_b ci_low ci_high t_stat df
#> <chr> <chr> <dbl> <dbl> <dbl> <int> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 Male Female 0.260 47.8 48.1 1424 1773 -1.65 2.17 0.271 66
#> # ℹ 2 more variables: p_value <dbl>, stars <chr>
