## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4)

## -----------------------------------------------------------------------------
library(SurveyNCD)
library(survey)
data(nhanes, package = "survey")

nh <- transform(
  nhanes,
  sex  = factor(RIAGENDR, 1:2, c("Male", "Female")),
  race = factor(race, 1:4, c("Hispanic", "Non-Hispanic white",
                              "Non-Hispanic black", "Other")),
  age_group = factor(agecat, levels(agecat), c("0-19", "20-39", "40-59", "60+"))
)
des <- svydesign(ids = ~SDMVPSU, strata = ~SDMVSTRA, weights = ~WTMEC2YR,
                 nest = TRUE, data = nh)

## -----------------------------------------------------------------------------
survey_prevalence(des, "HI_CHOL")
ncd_table(des, "HI_CHOL", by = "race",
          labels = c(HI_CHOL = "Total cholesterol >= 240 mg/dL"))

## ----fig.alt = "Prevalence of high cholesterol by sex and age group with confidence intervals."----
ggplot2::autoplot(survey_prevalence(des, "HI_CHOL", by = c("age_group", "sex")))

## -----------------------------------------------------------------------------
std <- who_standard_population(breaks = c(0, 20, 40, 60, 105),
                               labels = c("0-19", "20-39", "40-59", "60+"))
std
mm_standardize(des, "HI_CHOL", age_group = "age_group", standard = std, by = "race")

