## ----------------------------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", message = FALSE, warning = FALSE, fig.align = "center" ) ## ----------------------------------------------------------------------------- # # Install from GitHub # # install.packages("remotes") # remotes::install_github("stalynGuerrero/complexr") ## ----------------------------------------------------------------------------- library(complexr) ## ----------------------------------------------------------------------------- data <- generate_example_data(n_upm = 100, seed = 123) dplyr::glimpse(data) ## ----------------------------------------------------------------------------- # # CSV # data <- read_survey_data("survey.csv") # # # SPSS # data <- read_survey_data("survey.sav") # # # Stata # data <- read_survey_data("survey.dta") # # # Excel # data <- read_survey_data("survey.xlsx") ## ----------------------------------------------------------------------------- data <- mutate_survey_data( data, definitions = list( log_ingreso = ~ log(ingreso_pc + 1), ratio_gasto = ~ gasto_pc / ingreso_pc ) ) dplyr::select(data, ingreso_pc, log_ingreso, ratio_gasto) |> head(4) ## ----------------------------------------------------------------------------- design <- as_survey_design_tbl( data = data, weight = "weight", strata = "strata", cluster = "upm", nest = TRUE ) class(design) ## ----------------------------------------------------------------------------- describe_survey_design(design) ## ----------------------------------------------------------------------------- r_mean <- estimate_survey( design = design, variable = "ingreso_pc", estimator = "mean" ) r_mean ## ----------------------------------------------------------------------------- r_total <- estimate_survey( design = design, variable = "ingreso_pc", estimator = "total" ) r_total ## ----------------------------------------------------------------------------- r_pobre <- estimate_survey( design = design, variable = "pobre", estimator = "prop" ) r_pobre ## ----------------------------------------------------------------------------- r_empleo <- estimate_survey( design = design, variable = "empleo", estimator = "prop" ) r_empleo ## ----------------------------------------------------------------------------- r_ratio <- estimate_survey( design = design, estimator = "ratio", numerator = "ingreso_pc", denominator = "gasto_pc" ) r_ratio ## ----------------------------------------------------------------------------- r_ratio_cat <- estimate_survey( design = design, estimator = "ratio", numerator = "empleo", denominator = "empleo", ratio_num_level = "Formal", ratio_den_level = "Informal" ) r_ratio_cat ## ----------------------------------------------------------------------------- r_ratio_mix <- estimate_survey( design = design, estimator = "ratio", numerator = "ingreso_pc", denominator = "empleo", ratio_den_level = "Formal" ) r_ratio_mix ## ----------------------------------------------------------------------------- r_quant <- estimate_survey( design = design, variable = "ingreso_pc", estimator = "quantile", probs = c(0.10, 0.25, 0.50, 0.75, 0.90) ) r_quant ## ----------------------------------------------------------------------------- r_region <- estimate_survey( design = design, variable = "ingreso_pc", estimator = "mean", by = "region" ) r_region ## ----------------------------------------------------------------------------- r_region_area <- estimate_survey( design = design, variable = "ingreso_pc", estimator = "mean", by = c("region", "area") ) r_region_area ## ----------------------------------------------------------------------------- r_pobre_region <- estimate_survey( design = design, variable = "pobre", estimator = "prop", by = "region" ) r_pobre_region ## ----------------------------------------------------------------------------- format_results_table(r_region, digits = 3) ## ----------------------------------------------------------------------------- plot_results_bar(r_region) ## ----------------------------------------------------------------------------- plot_results_bar(r_region_area) ## ----------------------------------------------------------------------------- plot_results_bar(r_pobre_region) ## ----------------------------------------------------------------------------- # ComplexSurvey_app() ## ----------------------------------------------------------------------------- library(complexr) # 1. Generate / load data data <- generate_example_data(n_upm = 100, seed = 2024) # 2. Derive new variables data <- mutate_survey_data( data, definitions = list( log_ingreso = ~ log(ingreso_pc + 1) ) ) # 3. Build survey design (stratified multistage) design <- as_survey_design_tbl( data = data, weight = "weight", strata = "strata", cluster = "upm", nest = TRUE ) # 4. Diagnose: N_hat, H, PSUs, CV(w) describe_survey_design(design) # 5. Estimate domain mean and format res <- estimate_survey( design = design, variable = "ingreso_pc", estimator = "mean", by = c("region", "area") ) format_results_table(res, digits = 2)