Examples for 'srvyr::survey_total'


Calculate the total and its variation using survey methods

Aliases: survey_total

Keywords:

### ** Examples

library(survey)
Loading required package: grid
Loading required package: Matrix
Loading required package: survival
Attaching package: 'survey'
The following object is masked from 'package:graphics':

    dotchart
data(api)

dstrata <- apistrat %>%
  as_survey_design(strata = stype, weights = pw)

dstrata %>%
  summarise(enroll_tot = survey_total(enroll),
            tot_meals = survey_total(enroll * meals / 100, vartype = c("ci", "cv")))
# A tibble: 1 × 6
  enroll_tot enroll_tot_se tot_meals tot_meals_low tot_meals_upp tot_meals_cv
       <dbl>         <dbl>     <dbl>         <dbl>         <dbl>        <dbl>
1   3687178.       117319.  1753775.      1528167.      1979384.       0.0652
dstrata %>%
  group_by(awards) %>%
  summarise(api00 = survey_total(enroll))
# A tibble: 2 × 3
  awards    api00 api00_se
  <fct>     <dbl>    <dbl>
1 No     1627217.  147847.
2 Yes    2059960.  143734.
# Leave x empty to calculate the total in each group
dstrata %>%
  group_by(awards) %>%
  summarise(pct = survey_total())
# A tibble: 2 × 3
  awards   pct pct_se
  <fct>  <dbl>  <dbl>
1 No     2236.   216.
2 Yes    3958.   216.
# level takes a vector for multiple levels of confidence intervals
dstrata %>%
  summarise(enroll = survey_total(enroll, 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 3687178.     3455815.     3918540.     3577271.     3797084.
# 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.
# … with 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

[Package srvyr version 1.1.1 Index]