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, ... )
numerator | The numerator of the ratio |
---|---|
denominator | The denominator of the ratio |
na.rm | A logical value to indicate whether missing values should be dropped |
vartype | Report variability as one or more of: standard error ("se", default), confidence interval ("ci"), variance ("var") or coefficient of variation ("cv"). |
level | A single number or vector of numbers indicating the confidence level |
deff | A logical value to indicate whether the design effect should be returned. |
df | (For vartype = "ci" only) A numeric value indicating the degrees of freedom
for t-distribution. The default (NULL) uses |
... | 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#> # 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_def…¹ surve…² surve…³ #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 3898472. 3775136. 4021807. 3898472. 3.78e6 4.02e6 #> # … with abbreviated variable names ¹survey_defualt, ²survey_defualt_low, #> # ³survey_defualt_upp#> 2.5 % 97.5 % #> api99 3775894 4021049#> 2.5 % 97.5 % #> api99 3775136 4021807