|
42 | 42 | #' @import stats |
43 | 43 | #' |
44 | 44 | #' @export |
| 45 | +#' |
| 46 | +#' @examples |
| 47 | +#' # Ensure required packages are loaded |
| 48 | +#' if (requireNamespace("survey", quietly = TRUE) && |
| 49 | +#' requireNamespace("NHANES", quietly = TRUE) && |
| 50 | +#' requireNamespace("dplyr", quietly = TRUE)) { |
| 51 | +#' |
| 52 | +#' # 1. Prepare Data using NHANES |
| 53 | +#' data(NHANESraw, package = "NHANES") |
| 54 | +#' nhanes_adults_with_na <- NHANESraw %>% |
| 55 | +#' dplyr::filter(Age >= 20) %>% |
| 56 | +#' dplyr::mutate( |
| 57 | +#' ObeseStatus = factor(ifelse(BMI >= 30, "Obese", "Not Obese"), |
| 58 | +#' levels = c("Not Obese", "Obese")) |
| 59 | +#' ) |
| 60 | +#' |
| 61 | +#' adult_design_with_na <- survey::svydesign( |
| 62 | +#' id = ~SDMVPSU, |
| 63 | +#' strata = ~SDMVSTRA, |
| 64 | +#' weights = ~WTMEC2YR, |
| 65 | +#' nest = TRUE, |
| 66 | +#' data = nhanes_adults_with_na |
| 67 | +#' ) |
| 68 | +#' |
| 69 | +#' # 2. Basic Example: Create a simple Table 1 |
| 70 | +#' vars_to_summarize <- c("Age", "Gender", "Race1", "Education") |
| 71 | +#' table1 <- svytable1( |
| 72 | +#' design = adult_design_with_na, |
| 73 | +#' strata_var = "ObeseStatus", |
| 74 | +#' table_vars = vars_to_summarize |
| 75 | +#' ) |
| 76 | +#' print(table1) |
| 77 | +#' |
| 78 | +#' # 3. Advanced Example: Use reliability checks and get detailed metrics |
| 79 | +#' results <- svytable1( |
| 80 | +#' design = adult_design_with_na, |
| 81 | +#' strata_var = "ObeseStatus", |
| 82 | +#' table_vars = vars_to_summarize, |
| 83 | +#' reliability_checks = TRUE, |
| 84 | +#' return_metrics = TRUE |
| 85 | +#' ) |
| 86 | +#' |
| 87 | +#' # View the table with unreliable estimates suppressed (*) |
| 88 | +#' print(results$formatted_table) |
| 89 | +#' |
| 90 | +#' # View the detailed report card of reliability checks |
| 91 | +#' print(results$reliability_metrics) |
| 92 | +#' } |
45 | 93 | svytable1 <- function(design, strata_var, table_vars, |
46 | 94 | mode = "mixed", commas = TRUE, |
47 | 95 | reliability_checks = FALSE, return_metrics = FALSE) { |
|
0 commit comments