## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>"
)

## ----install, eval = FALSE----------------------------------------------------
# remotes::install_github("GabrielSotomayorl/dosr")

## ----crear-disenos------------------------------------------------------------
library(dosr)
library(srvyr)

design_2022 <- as_survey_design(casen_2022,
  ids     = varunit,
  strata  = varstrat,
  weights = expr
)

design_2024 <- as_survey_design(casen_2024,
  ids     = varunit,
  strata  = varstrat,
  weights = expr
)

## ----obs-prop-----------------------------------------------------------------
resultado_prop <- obs_prop(
  design_2022,
  sufijo     = "2022",
  var        = "pobreza",
  des        = "region",
  porcentaje = TRUE,
  save_xlsx  = FALSE,
  verbose    = FALSE
)
head(resultado_prop[, c("region", "pobreza", "prop_2022", "fiabilidad_2022")])

## ----obs-media----------------------------------------------------------------
resultado_media <- obs_media(
  design_2022,
  sufijo    = "2022",
  var       = "ytotcorh",
  des       = "region",
  save_xlsx = FALSE,
  verbose   = FALSE
)
head(resultado_media[, c("region", "media_2022", "fiabilidad_2022")])

## ----obs-cuantil--------------------------------------------------------------
resultado_cuantil <- obs_cuantil(
  design_2022,
  sufijo    = "2022",
  var       = "ytotcorh",
  des       = "region",
  cuant     = 0.5,
  save_xlsx = FALSE,
  verbose   = FALSE
)
head(resultado_cuantil[, c("region", "cuantil_2022", "fiabilidad_2022")])

## ----obs-total----------------------------------------------------------------
library(dplyr)

design_2022_pob <- design_2022
design_2022_pob$variables <- design_2022_pob$variables %>%
  mutate(pobre = as.integer(as.numeric(pobreza) %in% c(1, 2)))

resultado_total <- obs_total(
  design_2022_pob,
  sufijo    = "2022",
  var       = "pobre",
  des       = "region",
  save_xlsx = FALSE,
  verbose   = FALSE
)
head(resultado_total[, c("region", "total_2022", "fiabilidad_2022")])

## ----obs-ratio----------------------------------------------------------------
design_2022_sex <- design_2022
design_2022_sex$variables <- design_2022_sex$variables %>%
  mutate(
    mujer  = as.integer(as.numeric(sexo) == 2),
    hombre = as.integer(as.numeric(sexo) == 1)
  )

resultado_ratio <- obs_ratio(
  design_2022_sex,
  sufijo    = "2022",
  num       = "mujer",
  den       = "hombre",
  des       = "region",
  save_xlsx = FALSE,
  verbose   = FALSE
)
head(resultado_ratio[, c("region", "ratio_2022", "fiabilidad_2022")])

## ----serie-tiempo-------------------------------------------------------------
resultado_serie <- obs_prop(
  designs    = list(design_2022, design_2024),
  sufijo     = c("2022", "2024"),
  var        = "pobreza",
  des        = "region",
  porcentaje = TRUE,
  save_xlsx  = FALSE,
  verbose    = FALSE
)
cols <- c("region", "pobreza", "prop_2022", "prop_2024",
          "fiabilidad_2022", "fiabilidad_2024")
head(resultado_serie[, cols])

## ----multi-bin----------------------------------------------------------------
resultado_bin <- multi_bin(
  design_2024,
  vars_binarias = paste0("r8", letters[1:8]),
  des           = "area",
  dir           = tempdir(),
  verbose       = FALSE
)
nac <- resultado_bin$desagregacion_tipo == "Nacional"
resultado_bin[nac, c("etiqueta", "estimacion", "fiabilidad")]

