IDH: Código del PIB comunal.

Deducida el álgebra, se implementa la lógica en código R para el cálculo del PIB comunal.

Equipo Data Science DataIntelligence

06-12-2021

1 PIB por comuna

1.1 Leemos el PIB regional

Leemos el PIB regional del 2020 (en miles de millones de pesos):

# pib_region <- readxl::read_xls("PIBR_Act.xls", sheet = 1)
# pib_region <- cbind(seq(1,99),pib_region)
# pib_region <- pib_region[76:91,c(3,16)]
# colnames(pib_region) <- c("region","pib_r") 
# pib_region$pib_r <- as.numeric(pib_region$pib_r)
# pib_region$region[pib_region$region == "AyP"] <- "Arica y Parinacota"
# pib_region$region[pib_region$region == "TPCA"] <- "Tarapacá"
# pib_region$region[pib_region$region == "ANTOF"] <- "Antofagasta"
# pib_region$region[pib_region$region == "ATCMA"] <- "Atacama"
# pib_region$region[pib_region$region == "COQ"] <- "Coquimbo"
# pib_region$region[pib_region$region == "VALPO"] <- "Valparaíso"
# pib_region$region[pib_region$region == "RM"] <- "Metropolitana"
# pib_region$region[pib_region$region == "LGBO"] <- "O'Higgins"
# pib_region$region[pib_region$region == "MAULE"] <- "Maule"
# pib_region$region[pib_region$region == "BBIO"] <- "Biobío"
# pib_region$region[pib_region$region == "ARAUC"] <- "La Araucanía"
# pib_region$region[pib_region$region == "RIOS"] <- "Los Ríos"
# pib_region$region[pib_region$region == "LAGOS"] <- "Los Lagos"
# pib_region$region[pib_region$region == "AYSEN"] <- "Aysén"
# pib_region$region[pib_region$region == "MAG"] <- "Magallanes" 
# 
# pib_region
pib_region <- readxl::read_xls("pib por region.xls", sheet = 1)
## New names:
## * `` -> ...2
## * `` -> ...3
## * `` -> ...4
## * `` -> ...5
## * `` -> ...6
## * ...
pib_region <- pib_region[6:20,c(1,10)]
colnames(pib_region) <- c("region","pib_r")
pib_region$region[pib_region$region == "Región de Arica y Parinacota."] <- "Arica y Parinacota"
pib_region$region[pib_region$region == "Región de Tarapacá."] <- "Tarapacá"
pib_region$region[pib_region$region == "Región de Antofagasta."] <- "Antofagasta"
pib_region$region[pib_region$region == "Región de Atacama."] <- "Atacama"
pib_region$region[pib_region$region == "Región de Coquimbo."] <- "Coquimbo"
pib_region$region[pib_region$region == "Región de Valparaíso."] <- "Valparaíso"
pib_region$region[pib_region$region == "Región Metropolitana de Santiago."] <- "Metropolitana"
pib_region$region[pib_region$region == "Región del Libertador General Bernardo O’Higgins."] <- "O'Higgins"
pib_region$region[pib_region$region == "Región del Maule."] <- "Maule"
pib_region$region[pib_region$region == "Región del Biobío."] <- "Biobío"
pib_region$region[pib_region$region == "Región de La Araucanía."] <- "La Araucanía"
pib_region$region[pib_region$region == "Región de Los Ríos."] <- "Los Ríos"
pib_region$region[pib_region$region == "Región de Los Lagos."] <- "Los Lagos"
pib_region$region[pib_region$region == "Región de Aysén del General Carlos Ibáñez del Campo."] <- "Aysén"
pib_region$region[pib_region$region == "Región de Magallanes y la Antártica Chilena"] <- "Magallanes" 
pib_region
## # A tibble: 15 x 2
##    region              pib_r
##    <chr>               <dbl>
##  1 Arica y Parinacota  1052.
##  2 Tarapacá            3633.
##  3 Antofagasta        14604.
##  4 Atacama             3175.
##  5 Coquimbo            4175.
##  6 Valparaíso         11244.
##  7 Metropolitana      60508.
##  8 O'Higgins           6487.
##  9 Maule               4798.
## 10 Biobío             10339.
## 11 La Araucanía        3764.
## 12 Los Ríos            1883.
## 13 Los Lagos           4718.
## 14 Aysén                854.
## 15 Magallanes          1597.

1.2 Leemos la encuesta de trabajo

##        region  b18_codigo  fact_cal    pib_r
## 1 Antofagasta Antofagasta 413.33335 14603.98
## 2 Antofagasta Antofagasta  58.47501 14603.98
## 3 Antofagasta      Calama 139.32694 14603.98
## 4 Antofagasta Antofagasta 361.62372 14603.98
## 5 Antofagasta      Calama 218.39952 14603.98
## 6 Antofagasta      Calama 249.33903 14603.98

1.3 Aplicamos la lógica

receptaculo <- as_tibble()
for (i in unique(comunas$region)[-c(13,18)]) {
  comunas2 <- filter(comunas, region == i)
  per_com <- xtabs(fact_cal~b18_codigo, data = comunas2)#----- Fuerza de trabajo por comuna
  per_com <- as_tibble(per_com)
  per_com$pib_r <- unique(comunas2$pib_r)
  per_com$PMT <- per_com$pib_r / sum(per_com$n)*1000000000#----- Calculo de PMT
  per_com$PIB <- per_com$PMT*(per_com$n)#----- PIB comunal
  per_com$PIB_r_calculado <- sum(per_com$PIB)
  per_com$region <- i
  receptaculo <- rbind(receptaculo,per_com)
}
colnames(receptaculo) <- c("comuna_trabajo","fuerza_laboral","pib_reg_publicado","PMT","PIB_comuna","PIB_reg_calculado","region")


datatable(receptaculo, extensions = 'Buttons', escape = FALSE, rownames = TRUE,
          options = list(dom = 'Bfrtip',
          buttons = list('colvis', list(extend = 'collection',
          buttons = list(
          list(extend='copy'),
          list(extend='excel',
            filename = 'pib_comunal'),
          list(extend='pdf',
            filename= 'tabla')),
          text = 'Download')), scrollX = TRUE))
saveRDS(receptaculo,"PIB_comunal_regional.rds")
write_xlsx(receptaculo,"PIB_comunal_regional.xlsx")