## ----------------------------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", message = FALSE, warning = FALSE, fig.align = "center" ) ## ----------------------------------------------------------------------------- # # Instalar desde 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("encuesta.csv") # # # SPSS # data <- read_survey_data("encuesta.sav") # # # Stata # data <- read_survey_data("encuesta.dta") # # # Excel # data <- read_survey_data("encuesta.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_media <- estimate_survey( design = design, variable = "ingreso_pc", estimator = "mean" ) r_media ## ----------------------------------------------------------------------------- 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_razon <- estimate_survey( design = design, estimator = "ratio", numerator = "ingreso_pc", denominator = "gasto_pc" ) r_razon ## ----------------------------------------------------------------------------- r_razon_cat <- estimate_survey( design = design, estimator = "ratio", numerator = "empleo", denominator = "empleo", ratio_num_level = "Formal", ratio_den_level = "Informal" ) r_razon_cat ## ----------------------------------------------------------------------------- r_razon_mix <- estimate_survey( design = design, estimator = "ratio", numerator = "ingreso_pc", denominator = "empleo", ratio_den_level = "Formal" ) r_razon_mix ## ----------------------------------------------------------------------------- r_cuant <- estimate_survey( design = design, variable = "ingreso_pc", estimator = "quantile", probs = c(0.10, 0.25, 0.50, 0.75, 0.90) ) r_cuant ## ----------------------------------------------------------------------------- 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. Generar / cargar datos data <- generate_example_data(n_upm = 100, seed = 2024) # 2. Derivar nuevas variables data <- mutate_survey_data( data, definitions = list( log_ingreso = ~ log(ingreso_pc + 1) ) ) # 3. Construir diseño muestral (estratificado multietápico) design <- as_survey_design_tbl( data = data, weight = "weight", strata = "strata", cluster = "upm", nest = TRUE ) # 4. Diagnosticar el diseño describe_survey_design(design) # 5. Estimar media por dominio y formatear res <- estimate_survey( design = design, variable = "ingreso_pc", estimator = "mean", by = c("region", "area") ) format_results_table(res, digits = 2)