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,
  ...
)

Arguments

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 svyquantile details on alpha, which equals 1-level).

interval_type

See svyquantile. Note that interval_type = "quantile" is only available for replicate designs, and interval_type = "score" is unavailable for replicate designs.

qrule

See svyquantile

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

Details

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/>

Examples

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_q25_se api99_q50_se api99_q75_se api00
#>       <dbl>     <dbl>     <dbl>        <dbl>        <dbl>        <dbl> <dbl>
#> 1       526       631       728         13.7         17.5         18.3   668
#> # ℹ 2 more variables: api00_low <dbl>, api00_upp <dbl>

dstrata %>%
  group_by(awards) %>%
  summarise(api00 = survey_median(api00))
#> # A tibble: 2 × 3
#>   awards api00 api00_se
#>   <fct>  <dbl>    <dbl>
#> 1 No       646     21.4
#> 2 Yes      676     17.7