rm(list = ls())

library(tidyverse)
library(ggpubr)

setwd("~/Dropbox/C\ O\ N\ S\ U\ T\ O\ R\ I\ A\ S/UNFPA-INE_Paraguay/bases")

base0 <- readxl::read_xlsx("base_INE_proyectados_y_censales.xlsx") %>% 
  select(-X) %>% 
  type_convert()

names(table(base0$edad))
names(table(base0$departamento))
names(table(base0$dpto))
names(table(base0$sexo))
names(table(base0$year))
names(table(base0$tipo))
names(table(base0$edadcod))

# ANALISIS DE CENSOS ----
## ANALISIS DE NO ESPECIFICADOS ----

base02 <- base0 %>% 
  filter(tipo == "Dato censal", year == 2002) %>% 
  select(-dpto, -edadcod, -tipo)

base12 <- base0 %>% 
  filter(tipo == "Dato censal", year == 2012) %>% 
  select(-dpto, -edadcod, -tipo)

base22 <- base0 %>% 
  filter(tipo == "Dato censal", year == 2022) %>% 
  select(-dpto, -edadcod, -tipo)

base02 %>% 
  summarise(sum(valor))

base02 %>% 
  group_by(sexo) %>% 
  summarise(total = sum(valor))

base12 %>% 
  group_by(sexo) %>% 
  summarise(total = sum(valor))

base22 %>% 
  group_by(sexo) %>% 
  summarise(total = sum(valor)) %>% 
  ungroup() %>% 
  mutate(prop = 100*total/sum(total))

base22 %>% 
  group_by(edad) %>% 
  summarise(total = sum(valor)) %>% 
  ungroup() %>% 
  mutate(prop = 100*total/sum(total)) %>% 
  filter(edad == "No reportado")

base22 %>% 
  group_by(edad, sexo) %>% 
  summarise(total = sum(valor)) %>% 
  ungroup() %>% 
  mutate(prop = 100*total/sum(total)) %>% 
  filter(edad == "No reportado", sexo == "No reportado")

base22 %>% 
  group_by(departamento, edad) %>% 
  summarise(total = sum(valor)) %>% 
  ungroup() %>% 
  mutate(prop = 100*total/sum(total)) %>% 
  filter(edad == "No reportado")

base22 %>% 
  group_by(departamento, sexo) %>% 
  summarise(total = sum(valor)) %>% 
  ungroup() %>% 
  mutate(prop = 100*total/sum(total)) %>% 
  filter(sexo == "No reportado")
