R/survey_statistics.r
survey_quantile.Rd
Calculate quantiles from complex survey data. A wrapper
around svyquantile
. survey_quantile
and
survey_median
should always be called from summarise
.
survey_quantile( x, quantiles, na.rm = FALSE, vartype = c("se", "ci", "var", "cv"), level = 0.95, interval_type = c("mean", "beta", "xlogit", "asin", "score", "quantile"), qrule = c("math", "school", "shahvaish", "hf1", "hf2", "hf3", "hf4", "hf5", "hf6", "hf7", "hf8", "hf9"), df = NULL, ... ) survey_median( x, na.rm = FALSE, vartype = c("se", "ci", "var", "cv"), level = 0.95, interval_type = c("mean", "beta", "xlogit", "asin", "score", "quantile"), qrule = c("math", "school", "shahvaish", "hf1", "hf2", "hf3", "hf4", "hf5", "hf6", "hf7", "hf8", "hf9"), df = NULL, ... )
x | A variable or expression |
---|---|
quantiles | A vector of quantiles to calculate |
na.rm | A logical value to indicate whether missing values should be dropped |
vartype | NULL to report no variability. Otherwise one or more of: standard error ("se", the default), confidence interval ("ci"), variance ("var") or coefficient of variation ("cv"). |
level | A single number indicating the confidence level (only one level allowed). Note that this may effect estimated standard errors (see |
interval_type | See |
qrule | See |
df | A number indicating the degrees of freedom for t-distribution. The default, NULL, uses the design degrees of freedom (matches the survey package). |
... | Ignored |
Note that the behavior of these functions has changed in srvyr version 1.1,
but the old functions are still (currently) supported as
survey_old_quantile
and survey_old_median
if you need
to replicate the old results. For more details about what has changed, see
Thomas Lumley's blog post on the changes, available here:
<https://notstatschat.rbind.io/2021/07/20/what-s-new-in-the-survey-package/>
library(survey) data(api) dstrata <- apistrat %>% as_survey_design(strata = stype, weights = pw) dstrata %>% summarise(api99 = survey_quantile(api99, c(0.25, 0.5, 0.75)), api00 = survey_median(api00, vartype = c("ci")))#> # A tibble: 1 × 9 #> api99_q25 api99_q50 api99_q75 api99_q2…¹ api99…² api99…³ api00 api00…⁴ api00…⁵ #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 526 631 728 13.7 17.5 18.3 668 641 695 #> # … with abbreviated variable names ¹api99_q25_se, ²api99_q50_se, #> # ³api99_q75_se, ⁴api00_low, ⁵api00_upp#> # A tibble: 2 × 3 #> awards api00 api00_se #> <fct> <dbl> <dbl> #> 1 No 646 21.4 #> 2 Yes 676 17.7