library(rio)
library(DescTools)
library(ggplot2)
library(moments)
library(Rmisc)
## Loading required package: lattice
## Loading required package: plyr
library(e1071)
## 
## Attaching package: 'e1071'
## The following objects are masked from 'package:moments':
## 
##     kurtosis, moment, skewness
library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
## The following objects are masked from 'package:DescTools':
## 
##     AUC, ICC, SD
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(gplots)
## Registered S3 method overwritten by 'gplots':
##   method         from     
##   reorder.factor DescTools
## 
## Attaching package: 'gplots'
## The following object is masked from 'package:DescTools':
## 
##     reorder.factor
## The following object is masked from 'package:stats':
## 
##     lowess
library(vcd)
## Loading required package: grid
library(PMCMRplus)
library(nortest)
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:psych':
## 
##     logit
## The following object is masked from 'package:DescTools':
## 
##     Recode
library(stargazer)
## 
## Please cite as:
##  Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
library(lm.beta)
library(gtools)
## 
## Attaching package: 'gtools'
## The following object is masked from 'package:car':
## 
##     logit
## The following object is masked from 'package:psych':
## 
##     logit
## The following object is masked from 'package:e1071':
## 
##     permutations
library(jtools)
## 
## Attaching package: 'jtools'
## The following object is masked from 'package:DescTools':
## 
##     %nin%
library(ggstance)
## 
## Attaching package: 'ggstance'
## The following objects are masked from 'package:ggplot2':
## 
##     geom_errorbarh, GeomErrorbarh
library(broom.mixed)
## Registered S3 methods overwritten by 'broom':
##   method            from  
##   tidy.glht         jtools
##   tidy.summary.glht jtools
library(fastDummies)
library(writexl)
library(rvest)
library(jsonlite)
library(sp)
library(rgdal)
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## 
## rgdal: version: 1.5-32, (SVN revision 1176)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.0.4, released 2020/01/28
## Path to GDAL shared files: /usr/share/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 6.3.1, February 10th, 2020, [PJ_VERSION: 631]
## Path to PROJ shared files: /usr/share/proj
## Linking to sp version:1.5-0
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
library(htmltab)
library(googledrive)
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
library(stringr)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(polycor)
## 
## Attaching package: 'polycor'
## The following object is masked from 'package:psych':
## 
##     polyserial
library(ggcorrplot)
library(matrixcalc)
library(GPArotation)
library(lavaan)
## This is lavaan 0.6-12
## lavaan is FREE software! Please report any bugs.
## 
## Attaching package: 'lavaan'
## The following object is masked from 'package:psych':
## 
##     cor2cov
library(BBmisc)
## 
## Attaching package: 'BBmisc'
## The following object is masked from 'package:jtools':
## 
##     %nin%
## The following object is masked from 'package:grid':
## 
##     explode
## The following objects are masked from 'package:dplyr':
## 
##     coalesce, collapse
## The following object is masked from 'package:DescTools':
## 
##     %nin%
## The following object is masked from 'package:base':
## 
##     isFALSE
library(cluster)
library(factoextra)
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(qpcR)
## Loading required package: MASS
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
## Loading required package: minpack.lm
## Loading required package: rgl
## Warning in rgl.init(initValue, onlyNULL): RGL: unable to open X11 display
## Warning: 'rgl.init' failed, running with 'rgl.useNULL = TRUE'.
## Loading required package: robustbase
## Loading required package: Matrix
## 
## Attaching package: 'qpcR'
## The following object is masked from 'package:DescTools':
## 
##     RMSE
LINK="https://github.com/CarlosChavarri23/PC-3-.git"

#Descargar indicadores de pobreza de la base del BM

brecha="https://github.com/CarlosChavarri23/PC-3-/blob/0b03d59de3c8d3c530a07bff78a482ec98dee73b/Brecha%20de%20pobreza%20$1,%2090%20por%20d%C3%ADa.csv?raw=true"

brecha_sucia=import(brecha)

pobtug="https://github.com/CarlosChavarri23/PC-3-/blob/0b03d59de3c8d3c530a07bff78a482ec98dee73b/Poblaci%C3%B3n%20que%20vive%20en%20barrio%20s%20de%20tugurio.csv?raw=true"

pobtug_sucia=import(pobtug)


povheadc="https://github.com/CarlosChavarri23/PC-3-/blob/9ae37cfac11f9a92c662c602fc38bdfe9963af53/Poverty%20headcount%20ratio%20at%20$3.20%20a%20day%20(2011%20PPP)%20(%25%20of%20population).csv?raw=true"

povheadc_sucia=import(povheadc)

#Analizar la data

str(brecha_sucia)
## 'data.frame':    267 obs. of  67 variables:
##  $ V1 : chr  "Country Name" "Aruba" "" "Afganistán" ...
##  $ V2 : chr  "Country Code" "ABW" "AFE" "AFG" ...
##  $ V3 : chr  "Indicator Name" "Brecha de pobreza a $1,90 por día (2011 PPA) (%)" "Brecha de pobreza a $1,90 por día (2011 PPA) (%)" "Brecha de pobreza a $1,90 por día (2011 PPA) (%)" ...
##  $ V4 : chr  "Indicator Code" "SI.POV.GAPS" "SI.POV.GAPS" "SI.POV.GAPS" ...
##  $ V5 : int  1960 NA NA NA NA NA NA NA NA NA ...
##  $ V6 : int  1961 NA NA NA NA NA NA NA NA NA ...
##  $ V7 : int  1962 NA NA NA NA NA NA NA NA NA ...
##  $ V8 : int  1963 NA NA NA NA NA NA NA NA NA ...
##  $ V9 : int  1964 NA NA NA NA NA NA NA NA NA ...
##  $ V10: int  1965 NA NA NA NA NA NA NA NA NA ...
##  $ V11: int  1966 NA NA NA NA NA NA NA NA NA ...
##  $ V12: num  1967 NA NA NA NA ...
##  $ V13: int  1968 NA NA NA NA NA NA NA NA NA ...
##  $ V14: num  1969 NA NA NA NA ...
##  $ V15: int  1970 NA NA NA NA NA NA NA NA NA ...
##  $ V16: num  1971 NA NA NA NA ...
##  $ V17: int  1972 NA NA NA NA NA NA NA NA NA ...
##  $ V18: int  1973 NA NA NA NA NA NA NA NA NA ...
##  $ V19: num  1974 NA NA NA NA ...
##  $ V20: num  1975 NA NA NA NA ...
##  $ V21: int  1976 NA NA NA NA NA NA NA NA NA ...
##  $ V22: num  1977 NA NA NA NA ...
##  $ V23: num  1978 NA NA NA NA ...
##  $ V24: num  1979 NA NA NA NA ...
##  $ V25: num  1980 NA NA NA NA NA NA NA NA NA ...
##  $ V26: num  1981 NA NA NA NA ...
##  $ V27: num  1982 NA NA NA NA ...
##  $ V28: num  1983 NA NA NA NA ...
##  $ V29: num  1984 NA NA NA NA ...
##  $ V30: num  1985 NA NA NA NA ...
##  $ V31: num  1986 NA NA NA NA ...
##  $ V32: num  1987 NA NA NA NA ...
##  $ V33: num  1988 NA NA NA NA ...
##  $ V34: num  1989 NA NA NA NA ...
##  $ V35: num  1990 NA NA NA NA NA NA NA NA NA ...
##  $ V36: num  1991 NA NA NA NA ...
##  $ V37: num  1992 NA NA NA NA ...
##  $ V38: num  1993 NA NA NA NA ...
##  $ V39: num  1994 NA NA NA NA ...
##  $ V40: num  1995 NA NA NA NA ...
##  $ V41: num  1996 NA NA NA NA ...
##  $ V42: num  1997 NA NA NA NA ...
##  $ V43: num  1998 NA NA NA NA ...
##  $ V44: num  1999 NA NA NA NA ...
##  $ V45: num  2000 NA NA NA NA 16.4 NA NA NA NA ...
##  $ V46: num  2001 NA NA NA NA ...
##  $ V47: num  2002 NA NA NA NA ...
##  $ V48: num  2003 NA NA NA NA ...
##  $ V49: num  2004 NA NA NA NA ...
##  $ V50: num  2005 NA NA NA NA ...
##  $ V51: num  2006 NA NA NA NA ...
##  $ V52: num  2007 NA NA NA NA ...
##  $ V53: num  2008 NA NA NA NA ...
##  $ V54: num  2009 NA NA NA NA ...
##  $ V55: num  2010 NA NA NA NA NA NA NA NA NA ...
##  $ V56: num  2011 NA NA NA NA ...
##  $ V57: num  2012 NA NA NA NA ...
##  $ V58: num  2013 NA NA NA NA ...
##  $ V59: num  2014 NA NA NA NA ...
##  $ V60: num  2015 NA NA NA NA ...
##  $ V61: num  2016 NA NA NA NA ...
##  $ V62: num  2017 NA NA NA NA ...
##  $ V63: num  2018 NA NA NA NA ...
##  $ V64: num  2019 NA NA NA NA ...
##  $ V65: num  2020 NA NA NA NA NA NA NA NA NA ...
##  $ V66: num  2021 NA NA NA NA ...
##  $ V67: logi  NA NA NA NA NA NA ...
str(pobtug_sucia)
## 'data.frame':    267 obs. of  67 variables:
##  $ V1 : chr  "Country Name" "Aruba" "" "Afganistán" ...
##  $ V2 : chr  "Country Code" "ABW" "AFE" "AFG" ...
##  $ V3 : chr  "Indicator Name" "Población que vive en barrios de tugurios (% de la población urbana)" "Población que vive en barrios de tugurios (% de la población urbana)" "Población que vive en barrios de tugurios (% de la población urbana)" ...
##  $ V4 : chr  "Indicator Code" "EN.POP.SLUM.UR.ZS" "EN.POP.SLUM.UR.ZS" "EN.POP.SLUM.UR.ZS" ...
##  $ V5 : int  1960 NA NA NA NA NA NA NA NA NA ...
##  $ V6 : int  1961 NA NA NA NA NA NA NA NA NA ...
##  $ V7 : int  1962 NA NA NA NA NA NA NA NA NA ...
##  $ V8 : int  1963 NA NA NA NA NA NA NA NA NA ...
##  $ V9 : int  1964 NA NA NA NA NA NA NA NA NA ...
##  $ V10: int  1965 NA NA NA NA NA NA NA NA NA ...
##  $ V11: int  1966 NA NA NA NA NA NA NA NA NA ...
##  $ V12: int  1967 NA NA NA NA NA NA NA NA NA ...
##  $ V13: int  1968 NA NA NA NA NA NA NA NA NA ...
##  $ V14: int  1969 NA NA NA NA NA NA NA NA NA ...
##  $ V15: int  1970 NA NA NA NA NA NA NA NA NA ...
##  $ V16: int  1971 NA NA NA NA NA NA NA NA NA ...
##  $ V17: int  1972 NA NA NA NA NA NA NA NA NA ...
##  $ V18: int  1973 NA NA NA NA NA NA NA NA NA ...
##  $ V19: int  1974 NA NA NA NA NA NA NA NA NA ...
##  $ V20: int  1975 NA NA NA NA NA NA NA NA NA ...
##  $ V21: int  1976 NA NA NA NA NA NA NA NA NA ...
##  $ V22: int  1977 NA NA NA NA NA NA NA NA NA ...
##  $ V23: int  1978 NA NA NA NA NA NA NA NA NA ...
##  $ V24: int  1979 NA NA NA NA NA NA NA NA NA ...
##  $ V25: int  1980 NA NA NA NA NA NA NA NA NA ...
##  $ V26: int  1981 NA NA NA NA NA NA NA NA NA ...
##  $ V27: int  1982 NA NA NA NA NA NA NA NA NA ...
##  $ V28: int  1983 NA NA NA NA NA NA NA NA NA ...
##  $ V29: int  1984 NA NA NA NA NA NA NA NA NA ...
##  $ V30: int  1985 NA NA NA NA NA NA NA NA NA ...
##  $ V31: int  1986 NA NA NA NA NA NA NA NA NA ...
##  $ V32: int  1987 NA NA NA NA NA NA NA NA NA ...
##  $ V33: int  1988 NA NA NA NA NA NA NA NA NA ...
##  $ V34: int  1989 NA NA NA NA NA NA NA NA NA ...
##  $ V35: num  1990 NA 60.3 NA 73.2 ...
##  $ V36: int  1991 NA NA NA NA NA NA NA NA NA ...
##  $ V37: int  1992 NA NA NA NA NA NA NA NA NA ...
##  $ V38: int  1993 NA NA NA NA NA NA NA NA NA ...
##  $ V39: int  1994 NA NA NA NA NA NA NA NA NA ...
##  $ V40: num  1995 NA 58.2 NA 69.1 ...
##  $ V41: int  1996 NA NA NA NA NA NA NA NA NA ...
##  $ V42: int  1997 NA NA NA NA NA NA NA NA NA ...
##  $ V43: int  1998 NA NA NA NA NA NA NA NA NA ...
##  $ V44: int  1999 NA NA NA NA NA NA NA NA NA ...
##  $ V45: num  2000 NA 58.6 NA 65 ...
##  $ V46: int  2001 NA NA NA NA NA NA NA NA NA ...
##  $ V47: int  2002 NA NA NA NA NA NA NA NA NA ...
##  $ V48: int  2003 NA NA NA NA NA NA NA NA NA ...
##  $ V49: int  2004 NA NA NA NA NA NA NA NA NA ...
##  $ V50: num  2005 NA 61.2 NA 61.6 ...
##  $ V51: int  2006 NA NA NA NA NA NA NA NA NA ...
##  $ V52: int  2007 NA NA NA NA NA NA NA NA NA ...
##  $ V53: int  2008 NA NA NA NA NA NA NA NA NA ...
##  $ V54: int  2009 NA NA NA NA NA NA NA NA NA ...
##  $ V55: num  2010 NA 55.7 NA 59.2 ...
##  $ V56: int  2011 NA NA NA NA NA NA NA NA NA ...
##  $ V57: int  2012 NA NA NA NA NA NA NA NA NA ...
##  $ V58: int  2013 NA NA NA NA NA NA NA NA NA ...
##  $ V59: num  2014 NA 58.8 62.7 51.4 ...
##  $ V60: int  2015 NA NA NA NA NA NA NA NA NA ...
##  $ V61: num  2016 NA 57.2 71.3 51.4 ...
##  $ V62: int  2017 NA NA NA NA NA NA NA NA NA ...
##  $ V63: num  2018 NA 55.8 70.7 51.2 ...
##  $ V64: int  2019 NA NA NA NA NA NA NA NA NA ...
##  $ V65: int  2020 NA NA NA NA NA NA NA NA NA ...
##  $ V66: int  2021 NA NA NA NA NA NA NA NA NA ...
##  $ V67: logi  NA NA NA NA NA NA ...
str(povheadc_sucia)
## 'data.frame':    267 obs. of  67 variables:
##  $ V1 : chr  "Country Name" "Aruba" "" "Afganistán" ...
##  $ V2 : chr  "Country Code" "ABW" "AFE" "AFG" ...
##  $ V3 : chr  "Indicator Name" "Poverty headcount ratio at $3.20 a day (2011 PPP) (% of population)" "Poverty headcount ratio at $3.20 a day (2011 PPP) (% of population)" "Poverty headcount ratio at $3.20 a day (2011 PPP) (% of population)" ...
##  $ V4 : chr  "Indicator Code" "SI.POV.LMIC" "SI.POV.LMIC" "SI.POV.LMIC" ...
##  $ V5 : int  1960 NA NA NA NA NA NA NA NA NA ...
##  $ V6 : int  1961 NA NA NA NA NA NA NA NA NA ...
##  $ V7 : int  1962 NA NA NA NA NA NA NA NA NA ...
##  $ V8 : int  1963 NA NA NA NA NA NA NA NA NA ...
##  $ V9 : int  1964 NA NA NA NA NA NA NA NA NA ...
##  $ V10: int  1965 NA NA NA NA NA NA NA NA NA ...
##  $ V11: int  1966 NA NA NA NA NA NA NA NA NA ...
##  $ V12: int  1967 NA NA NA NA NA NA NA NA NA ...
##  $ V13: int  1968 NA NA NA NA NA NA NA NA NA ...
##  $ V14: num  1969 NA NA NA NA ...
##  $ V15: int  1970 NA NA NA NA NA NA NA NA NA ...
##  $ V16: int  1971 NA NA NA NA NA NA NA NA NA ...
##  $ V17: int  1972 NA NA NA NA NA NA NA NA NA ...
##  $ V18: int  1973 NA NA NA NA NA NA NA NA NA ...
##  $ V19: num  1974 NA NA NA NA ...
##  $ V20: num  1975 NA NA NA NA ...
##  $ V21: int  1976 NA NA NA NA NA NA NA NA NA ...
##  $ V22: num  1977 NA NA NA NA ...
##  $ V23: num  1978 NA NA NA NA ...
##  $ V24: num  1979 NA NA NA NA ...
##  $ V25: num  1980 NA NA NA NA NA NA NA NA NA ...
##  $ V26: num  1981 NA NA NA NA ...
##  $ V27: num  1982 NA NA NA NA ...
##  $ V28: num  1983 NA NA NA NA ...
##  $ V29: num  1984 NA NA NA NA ...
##  $ V30: num  1985 NA NA NA NA ...
##  $ V31: num  1986 NA NA NA NA ...
##  $ V32: num  1987 NA NA NA NA ...
##  $ V33: num  1988 NA NA NA NA ...
##  $ V34: num  1989 NA NA NA NA ...
##  $ V35: num  1990 NA NA NA NA NA NA NA NA NA ...
##  $ V36: num  1991 NA NA NA NA ...
##  $ V37: num  1992 NA NA NA NA ...
##  $ V38: num  1993 NA NA NA NA ...
##  $ V39: num  1994 NA NA NA NA ...
##  $ V40: num  1995 NA NA NA NA ...
##  $ V41: num  1996 NA NA NA NA ...
##  $ V42: num  1997 NA NA NA NA ...
##  $ V43: num  1998 NA NA NA NA ...
##  $ V44: num  1999 NA NA NA NA ...
##  $ V45: num  2000 NA NA NA NA 59.7 NA NA NA NA ...
##  $ V46: num  2001 NA NA NA NA ...
##  $ V47: num  2002 NA NA NA NA ...
##  $ V48: num  2003 NA NA NA NA ...
##  $ V49: num  2004 NA NA NA NA ...
##  $ V50: num  2005 NA NA NA NA ...
##  $ V51: num  2006 NA NA NA NA ...
##  $ V52: num  2007 NA NA NA NA ...
##  $ V53: num  2008 NA NA NA NA ...
##  $ V54: num  2009 NA NA NA NA ...
##  $ V55: num  2010 NA NA NA NA NA NA NA NA NA ...
##  $ V56: num  2011 NA NA NA NA ...
##  $ V57: num  2012 NA NA NA NA ...
##  $ V58: num  2013 NA NA NA NA ...
##  $ V59: num  2014 NA NA NA NA ...
##  $ V60: num  2015 NA NA NA NA ...
##  $ V61: num  2016 NA NA NA NA ...
##  $ V62: num  2017 NA NA NA NA ...
##  $ V63: num  2018 NA NA NA NA ...
##  $ V64: num  2019 NA NA NA NA ...
##  $ V65: num  2020 NA NA NA NA NA NA NA NA NA ...
##  $ V66: int  2021 NA NA NA NA NA NA NA NA NA ...
##  $ V67: logi  NA NA NA NA NA NA ...
names(brecha_sucia)
##  [1] "V1"  "V2"  "V3"  "V4"  "V5"  "V6"  "V7"  "V8"  "V9"  "V10" "V11" "V12"
## [13] "V13" "V14" "V15" "V16" "V17" "V18" "V19" "V20" "V21" "V22" "V23" "V24"
## [25] "V25" "V26" "V27" "V28" "V29" "V30" "V31" "V32" "V33" "V34" "V35" "V36"
## [37] "V37" "V38" "V39" "V40" "V41" "V42" "V43" "V44" "V45" "V46" "V47" "V48"
## [49] "V49" "V50" "V51" "V52" "V53" "V54" "V55" "V56" "V57" "V58" "V59" "V60"
## [61] "V61" "V62" "V63" "V64" "V65" "V66" "V67"
names (pobtug_sucia)
##  [1] "V1"  "V2"  "V3"  "V4"  "V5"  "V6"  "V7"  "V8"  "V9"  "V10" "V11" "V12"
## [13] "V13" "V14" "V15" "V16" "V17" "V18" "V19" "V20" "V21" "V22" "V23" "V24"
## [25] "V25" "V26" "V27" "V28" "V29" "V30" "V31" "V32" "V33" "V34" "V35" "V36"
## [37] "V37" "V38" "V39" "V40" "V41" "V42" "V43" "V44" "V45" "V46" "V47" "V48"
## [49] "V49" "V50" "V51" "V52" "V53" "V54" "V55" "V56" "V57" "V58" "V59" "V60"
## [61] "V61" "V62" "V63" "V64" "V65" "V66" "V67"
names(povheadc_sucia)
##  [1] "V1"  "V2"  "V3"  "V4"  "V5"  "V6"  "V7"  "V8"  "V9"  "V10" "V11" "V12"
## [13] "V13" "V14" "V15" "V16" "V17" "V18" "V19" "V20" "V21" "V22" "V23" "V24"
## [25] "V25" "V26" "V27" "V28" "V29" "V30" "V31" "V32" "V33" "V34" "V35" "V36"
## [37] "V37" "V38" "V39" "V40" "V41" "V42" "V43" "V44" "V45" "V46" "V47" "V48"
## [49] "V49" "V50" "V51" "V52" "V53" "V54" "V55" "V56" "V57" "V58" "V59" "V60"
## [61] "V61" "V62" "V63" "V64" "V65" "V66" "V67"
  1. Quédese con los valores para un solo año de cada indicador. Los años no necesariamente deben ser iguales para los tres indicadores, sin embargo, debe cumplir con dos condiciones: el valor debe ser posterior al 2010, y el año elegido debe contener la menor cantidad de valores perdidos. (10 puntos)
brecha_sucia=brecha_sucia[,-c(2:54)]

apply(is.na(brecha_sucia), MARGIN = 2, sum)
##  V1 V55 V56 V57 V58 V59 V60 V61 V62 V63 V64 V65 V66 V67 
##   0 168 174 167 175 169 169 171 177 170 200 246 265 267
pobtug_sucia=pobtug_sucia[,-c(2:54)]

apply(is.na(pobtug_sucia), MARGIN = 2, sum)
##  V1 V55 V56 V57 V58 V59 V60 V61 V62 V63 V64 V65 V66 V67 
##   0 191 266 266 266 140 266 120 266 108 266 266 266 267
povheadc_sucia= povheadc_sucia[,-c(2:54)]

apply(is.na(povheadc_sucia), MARGIN = 2, sum)
##  V1 V55 V56 V57 V58 V59 V60 V61 V62 V63 V64 V65 V66 V67 
##   0 168 174 167 175 169 169 171 177 170 200 246 265 267
brecha_sucia=brecha_sucia[,c(1,6)]
pobtug_sucia=pobtug_sucia[,c(1,10)]
povheadc_sucia=povheadc_sucia[,c(1,6)]
brecha_sucia = brecha_sucia[-c(1),]
pobtug_sucia = pobtug_sucia[-c(1),]
povheadc_sucia = povheadc_sucia[-c(1),]
colnames(brecha_sucia)=c("country","brecha")
colnames(pobtug_sucia)=c("country","pobtug")
colnames(povheadc_sucia)=c("country","povheadc")
  1. Junte los tres indicadores en una sola base de datos, y justifique que “join” utilizó para la integración de datos. (5 puntos)
data1=merge(brecha_sucia, pobtug_sucia)
data2=merge(data1, povheadc_sucia)
data2= data2[complete.cases(data2$brecha),]
data2= data2[complete.cases(data2$pobtug),]
data2= data2[complete.cases(data2$povheadc),]  

####Es indiferente el join a utilizar pues los elementos son exactamente los mismos.

  1. Cree una variable categórica ordinal sobre uno de los indicadores, y luego elimine el indicador sobre el cual la creo. Justifique el porqué de los intervalos escogidos en la variable categórica. (10 puntos)
summary(data2$pobtug)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.001  12.475  24.950  28.631  38.139  88.400
data2$pobtug_ORD = ifelse(data2$pobtug<=12.475, 1, ifelse(data2$pobtug>12.475 & data2$pobtug<=24.950, 2, ifelse(data2$pobtug>24.950 & data2$pobtug<=38.139, 3, 4)))
data2$pobtug_ORD = factor(data2$pobtug_ORD, levels = c(1:4), ordered = TRUE)
str(data2$pobtug_ORD)
##  Ord.factor w/ 4 levels "1"<"2"<"3"<"4": 4 1 2 2 1 3 3 3 4 2 ...

###Los puntos de corte para nuestra variable ordinal fueron escogidos en función percentiles identificados mediante la función summary.

  1. Descargue la base de datos del Índice de Desarrollo Humano 2019 de la siguiente dirección https://datosmacro.expansion.com/idh ,y súbala a su repositorio de Github. (5 puntos)
IDH_link = "https://datosmacro.expansion.com/idh"
pweb=read_html(IDH_link)
pweb
## {html_document}
## <html lang="es" dir="ltr" prefix="content: http://purl.org/rss/1.0/modules/content/  dc: http://purl.org/dc/terms/  foaf: http://xmlns.com/foaf/0.1/  rdfs: http://www.w3.org/2000/01/rdf-schema#  sioc: http://rdfs.org/sioc/ns#  sioct: http://rdfs.org/sioc/types#  skos: http://www.w3.org/2004/02/skos/core#  xsd: http://www.w3.org/2001/XMLSchema#  og: http://ogp.me/ns#  schema: http://schema.org/ ">
## [1] <head>\n<meta http-equiv="Content-Type" content="text/html; charset=UTF-8 ...
## [2] <body itemscope itemtype="https://schema.org/WebPage" class="path-node pa ...
IDH_cs="tr"
IDH_html = html_nodes(pweb,IDH_cs)
IDH_texto = html_text(IDH_html)
head(IDH_texto)
## [1] "PaísesIDHRanking IDHVar."  "España [+]0,90425º0"      
## [3] "Alemania [+]0,9476º2"      "Reino Unido [+]0,93213º-1"
## [5] "Francia [+]0,90126º0"      "Italia [+]0,89229º0"
data_IDH=data.frame(IDH_texto)
head(data_IDH)
##                   IDH_texto
## 1  PaísesIDHRanking IDHVar.
## 2       España [+]0,90425º0
## 3      Alemania [+]0,9476º2
## 4 Reino Unido [+]0,93213º-1
## 5      Francia [+]0,90126º0
## 6       Italia [+]0,89229º0
export(data_IDH,"data_IDH.csv")
  1. Realice la limpieza necesaria para la integración de la base de datos de los tres indicadores elegidos y la base de datos de IDH. (10 puntos)
data_IDH= data_IDH[-1,]
str(data_IDH)
##  chr [1:189] "España [+]0,90425º0" "Alemania [+]0,9476º2" ...
data_IDH=data.frame(data_IDH)
#partición y creación de variable 
data_IDH$country=str_split(data_IDH$data_IDH,"[+]",simplify = T)[,1]

#identification "["
data_IDH$country=gsub("[[:punct:]]","",data_IDH$country)

#limpieza de espacios
data_IDH$country =trimws(data_IDH$country,which=c("right"),whitespace = "[\\h\\v]")
#atos de IDH, variacion y ranking 
data_IDH$part2=str_split(data_IDH$data_IDH,"]",simplify = T)[,2]

IDH

#cinco primero caracteres 
data_IDH$IDH_2019=substr(data_IDH$part2,1,5)
 
data_IDH$IDH_2019=gsub(",",".",data_IDH$IDH_2019)

###VARIACIÓN

#última partición del v2 para sacar el ranking y variación:
data_IDH$Var=str_split(data_IDH$part2,"º",simplify = T)[,2]

###RANKING

#caracteres 6 y 7
data_IDH$Rank=substr(data_IDH$part2,6,7)
data_IDH$Rank=gsub("º","",data_IDH$Rank)
data_IDH=data_IDH[,c(2,4)]
head(data_IDH)
##       country IDH_2019
## 1      España    0.904
## 2    Alemania    0.947
## 3 Reino Unido    0.932
## 4     Francia    0.901
## 5      Italia    0.892
## 6    Portugal    0.864
str(data_IDH$IDH_2019)
##  chr [1:189] "0.904" "0.947" "0.932" "0.901" "0.892" "0.864" "0.926" ...
data_IDH$IDH_2019 = as.numeric(data_IDH$IDH_2019)
  1. Junte ambas bases en una sola base de datos, y justifique que “join” utilizó para la integración de datos. Debe obtener una base de datos con 4 variables (5 puntos)
library(dplyr)
data_final=left_join(data2,data_IDH,by="country") 
head(data_final)
##                      country brecha   pobtug povheadc pobtug_ORD IDH_2019
## 1   África al sur del Sahara   15.4 53.59901     67.9          4       NA
## 2                   Alemania    0.0  0.01000      0.2          1    0.947
## 3 América Latina y el Caribe    1.4 20.75693     10.5          2       NA
## 4                  Argentina    0.4 14.70000      3.6          2    0.845
## 5                    Armenia    0.2  9.30000     11.4          1    0.776
## 6            Asia meridional    2.8 37.65609     52.6          3       NA

La data obtenida, tiene cinco variables pues consideramos útiles y gráficas ambas versiones de la variable “Porcentaje de la población que vive en barrios marginales”, tanto el porcentaje como la variable de tipo ordinal. En cuanto, al criterio de integración se optó por leftjoin puesto que con cualquiera de los otros criterios no tendríamos ningún NA, lo cuál en términos estadísticos y exploración de data es bastaante improbable. Por ello, a pesar del impacto de los mismos se decició conservar los NA’s frente a tener una data bastante más reducida y poco natural. Asimismo, es importante señalar que la información disponible del país a analizar condicionó mucho la elaboración del mismo.

export(data_final,"Base_Guatemala1.csv")