se_mean()
estimates the means of numeric variables along with variance
and confidence intervals for FSO's structural survey.
Arguments
- data
A data frame or tibble.
- variable
Unquoted or quoted name of the numeric variable whose mean is to be estimated. Programmatic usage (e.g., using
!!sym()
) is supported.- ...
Optional grouping variables. Can be passed unquoted (e.g.,
gender
,birth_country
) or programmatically using!!!syms(c("gender", "birth_country"))
.- strata
Unquoted or quoted name of the strata column. Defaults to
zone
if omitted.- weight
Unquoted or quoted name of the sampling weights column. For programmatic use with a string variable (e.g.,
wt <- "weights"
), use!!sym(wt)
in the function call.- alpha
Numeric significance level for confidence intervals. Default is 0.05 (95% CI).
Value
A tibble with columns:
- occ
Sample size (number of observations) per group.
- <variable>
Estimated mean of the specified numeric variable, named dynamically.
- vhat, stand_dev
Estimated variance of the mean (
vhat
) and its standard deviation (stand_dev
, square root of the variance).- ci, ci_l, ci_u
Confidence interval: half-width (
ci
), lower (ci_l
) and upper (ci_u
) bounds.
Examples
# Direct column references (unquoted)
se_mean(
data = nhanes,
variable = age,
strata = strata,
weight = weights,
gender, birth_country
)
#> # A tibble: 5 × 9
#> gender birth_country occ age vhat stand_dev ci ci_l ci_u
#> <fct> <fct> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 Female US 3909 38.0 0.230 0.480 0.940 37.1 39.0
#> 2 Female Other 1168 43.7 0.386 0.621 1.22 42.5 45.0
#> 3 Female Missing 2 30.2 0 0 0 30.2 30.2
#> 4 Male US 3824 36.2 0.236 0.486 0.953 35.2 37.1
#> 5 Male Other 1068 41.8 0.361 0.601 1.18 40.7 43.0
# Quoted column names
se_mean(
data = nhanes,
variable = "age",
strata = "strata",
weight = "weights",
gender, birth_country
)
#> # A tibble: 5 × 9
#> gender birth_country occ age vhat stand_dev ci ci_l ci_u
#> <fct> <fct> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 Female US 3909 38.0 0.230 0.480 0.940 37.1 39.0
#> 2 Female Other 1168 43.7 0.386 0.621 1.22 42.5 45.0
#> 3 Female Missing 2 30.2 0 0 0 30.2 30.2
#> 4 Male US 3824 36.2 0.236 0.486 0.953 35.2 37.1
#> 5 Male Other 1068 41.8 0.361 0.601 1.18 40.7 43.0
# Programmatic use with strings
v <- "age"
wt <- "weights"
vars <- c("gender", "birth_country")
se_mean(
data = nhanes,
variable = !!rlang::sym(v),
strata = strata,
weight = !!rlang::sym(wt),
!!!rlang::syms(vars)
)
#> # A tibble: 5 × 9
#> gender birth_country occ age vhat stand_dev ci ci_l ci_u
#> <fct> <fct> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 Female US 3909 38.0 0.230 0.480 0.940 37.1 39.0
#> 2 Female Other 1168 43.7 0.386 0.621 1.22 42.5 45.0
#> 3 Female Missing 2 30.2 0 0 0 30.2 30.2
#> 4 Male US 3824 36.2 0.236 0.486 0.953 35.2 37.1
#> 5 Male Other 1068 41.8 0.361 0.601 1.18 40.7 43.0