R/survey_statistics.r
survey_ratio.Rd
Calculate ratios from complex survey data. A wrapper
around svyratio
. survey_ratio
should always be called from summarise
.
survey_ratio(
numerator,
denominator,
na.rm = FALSE,
vartype = c("se", "ci", "var", "cv"),
level = 0.95,
deff = FALSE,
df = NULL,
...
)
The numerator of the ratio
The denominator of the ratio
A logical value to indicate whether missing values should be dropped
Report variability as one or more of: standard error ("se", default), confidence interval ("ci"), variance ("var") or coefficient of variation ("cv").
A single number or vector of numbers indicating the confidence level
A logical value to indicate whether the design effect should be returned.
(For vartype = "ci" only) A numeric value indicating the degrees of freedom
for t-distribution. The default (NULL) uses degf
,
but Inf is the usual survey package's default (except in
svyciprop
.
Ignored
library(survey)
data(api)
dstrata <- apistrat %>%
as_survey_design(strata = stype, weights = pw)
dstrata %>%
summarise(enroll = survey_ratio(api00, api99, vartype = c("ci", "cv")))
#> # A tibble: 1 × 4
#> enroll enroll_low enroll_upp enroll_cv
#> <dbl> <dbl> <dbl> <dbl>
#> 1 1.05 1.04 1.06 0.00351
dstrata %>%
group_by(awards) %>%
summarise(api00 = survey_ratio(api00, api99))
#> # A tibble: 2 × 3
#> awards api00 api00_se
#> <fct> <dbl> <dbl>
#> 1 No 1.02 0.00343
#> 2 Yes 1.07 0.00478
# level takes a vector for multiple levels of confidence intervals
dstrata %>%
summarise(enroll = survey_ratio(api99, api00, vartype = "ci", level = c(0.95, 0.65)))
#> # A tibble: 1 × 5
#> enroll enroll_low95 enroll_upp95 enroll_low65 enroll_upp65
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.950 0.944 0.957 0.947 0.953
# Note that the default degrees of freedom in srvyr is different from
# survey, so your confidence intervals might not exactly match. To
# replicate survey's behavior, use df = Inf
dstrata %>%
summarise(srvyr_default = survey_total(api99, vartype = "ci"),
survey_defualt = survey_total(api99, vartype = "ci", df = Inf))
#> # A tibble: 1 × 6
#> srvyr_default srvyr_default_low srvyr_default_upp survey_defualt
#> <dbl> <dbl> <dbl> <dbl>
#> 1 3898472. 3775136. 4021807. 3898472.
#> # ℹ 2 more variables: survey_defualt_low <dbl>, survey_defualt_upp <dbl>
comparison <- survey::svytotal(~api99, dstrata)
confint(comparison) # survey's default
#> 2.5 % 97.5 %
#> api99 3775894 4021049
confint(comparison, df = survey::degf(dstrata)) # srvyr's default
#> 2.5 % 97.5 %
#> api99 3775136 4021807