library(survey)
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")))
dstrata %>%
group_by(awards) %>%
summarise(api00 = survey_total(enroll))
# Leave x empty to calculate the total in each group
dstrata %>%
group_by(awards) %>%
summarise(pct = survey_total())
# level takes a vector for multiple levels of confidence intervals
dstrata %>%
summarise(enroll = survey_total(enroll, vartype = "ci", level = c(0.95, 0.65)))
# 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))
comparison <- survey::svytotal(~api99, dstrata)
confint(comparison) # survey's default
confint(comparison, df = survey::degf(dstrata)) # srvyr's default
Run the code above in your browser using DataLab