library(rio)

#PREGUNTA 1

library(readr)
data <- read_csv("V-Dem-2024 - V-Dem-2024.csv")
## Rows: 179 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): country_name
## dbl (5): v2x_polyarchy, v2x_liberal, v2x_partip, v2xdl_delib, v2x_egal
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(data)

Electoral democracy index (v2x_polyarchy)

Liberal component index (v2x_liberal)

Participatory component index (v2x_partip)

Deliberative component index (v2xdl_delib)

Egalitarian component index (v2x_egal)

#HIPOTESIS: El componente participativo (v2x_partip), el deliberativo (v2xdl_delib), el igualitario (v2x_egal) y el liberal (v2x_liberal) explican el índice de democracia electoral (v2x_polyarchy).

modelolineal <- lm(v2x_polyarchy ~ v2x_partip + v2xdl_delib + v2x_egal + v2x_liberal, data = data)
summary(modelolineal)
## 
## Call:
## lm(formula = v2x_polyarchy ~ v2x_partip + v2xdl_delib + v2x_egal + 
##     v2x_liberal, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.27888 -0.06142  0.01883  0.06897  0.19187 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.09343    0.02400  -3.893 0.000141 ***
## v2x_partip   0.34928    0.06361   5.491 1.39e-07 ***
## v2xdl_delib -0.01623    0.05634  -0.288 0.773587    
## v2x_egal     0.12920    0.05385   2.399 0.017474 *  
## v2x_liberal  0.60183    0.06025   9.989  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.09657 on 174 degrees of freedom
## Multiple R-squared:  0.8663, Adjusted R-squared:  0.8632 
## F-statistic: 281.9 on 4 and 174 DF,  p-value: < 2.2e-16

#PREGUNTA 2

data2 <- read_csv("PL2 - PC3.csv")
## Rows: 1874 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (3): Departamento, Provincia, Distrito
## dbl (12): Ubigeo, Hogares_T, V_CarFisInadec_T, V_CarFisInadec, V_Hacin_T, V_...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(data2)

Hogares_T: Número de hogares V_CarFisIna_des_T: Viviendas con características físicas inadecuadas V_Hacin_T: Viviendas con hacinamiento H_NinosNoEsc_T: Hogares con niños que no asisten a la escuela H_AltaDepEcon_T: Hogares con alta dependencia económica V_ServHig_T: Vivienda sin acceso a servicios higiénicos

names(data2)
##  [1] "Ubigeo"           "Departamento"     "Provincia"        "Distrito"        
##  [5] "Hogares_T"        "V_CarFisInadec_T" "V_CarFisInadec"   "V_Hacin_T"       
##  [9] "V_Hacin"          "V_ServHig_T"      "V_ServHig"        "H_NinosNoEsc_T"  
## [13] "H_NinosNoEsc"     "H_AltaDepEcon_T"  "H_AltaDepEcon"
modelopoisson1 <- glm(Hogares_T ~ H_AltaDepEcon_T + V_CarFisInadec_T + V_Hacin_T, 
                        data = data2, family = poisson(link = "log"))
summary(modelopoisson1)
## 
## Call:
## glm(formula = Hogares_T ~ H_AltaDepEcon_T + V_CarFisInadec_T + 
##     V_Hacin_T, family = poisson(link = "log"), data = data2)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -512.40   -54.30   -39.73   -13.47   648.76  
## 
## Coefficients:
##                    Estimate Std. Error z value Pr(>|z|)    
## (Intercept)       7.909e+00  4.398e-04   17983   <2e-16 ***
## H_AltaDepEcon_T   1.433e-03  1.228e-06    1167   <2e-16 ***
## V_CarFisInadec_T -1.962e-04  1.529e-07   -1284   <2e-16 ***
## V_Hacin_T         3.945e-04  2.280e-07    1730   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 22374858  on 1873  degrees of freedom
## Residual deviance: 10589754  on 1870  degrees of freedom
## AIC: 10606688
## 
## Number of Fisher Scoring iterations: 6
modelopoisson2 <- glm(Hogares_T ~ H_AltaDepEcon_T + V_CarFisInadec_T + V_Hacin_T + H_NinosNoEsc_T + V_ServHig_T, 
                        data = data2, family = poisson(link = "log"))
summary(modelopoisson2)
## 
## Call:
## glm(formula = Hogares_T ~ H_AltaDepEcon_T + V_CarFisInadec_T + 
##     V_Hacin_T + H_NinosNoEsc_T + V_ServHig_T, family = poisson(link = "log"), 
##     data = data2)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -463.88   -53.91   -39.22   -12.48   636.69  
## 
## Coefficients:
##                    Estimate Std. Error z value Pr(>|z|)    
## (Intercept)       7.871e+00  4.534e-04 17359.4   <2e-16 ***
## H_AltaDepEcon_T   1.140e-03  1.555e-06   733.3   <2e-16 ***
## V_CarFisInadec_T -2.132e-04  1.624e-07 -1313.1   <2e-16 ***
## V_Hacin_T         1.410e-04  9.081e-07   155.2   <2e-16 ***
## H_NinosNoEsc_T    1.420e-03  4.369e-06   325.1   <2e-16 ***
## V_ServHig_T       2.002e-04  5.312e-07   376.8   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 22374858  on 1873  degrees of freedom
## Residual deviance: 10315026  on 1868  degrees of freedom
## AIC: 10331964
## 
## Number of Fisher Scoring iterations: 6

#comparando modelo1 y modelo2

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(kableExtra)
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
anova(modelopoisson1,modelopoisson2,test = "Chisq") %>%
kable(caption = "Tabla ANOVA para comparar modelos poisson")%>%kableExtra::kable_styling(full_width = FALSE)
Tabla ANOVA para comparar modelos poisson
Resid. Df Resid. Dev Df Deviance Pr(>Chi)
1870 10589754 NA NA NA
1868 10315026 2 274727.7 0

#PREGUNTA 3

data3 <- read_csv("Ceplan.xlsx - Hoja 1.csv")
## Rows: 228 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (7): TIPO, UBIGEO, PROVINCIA, IVIA, IDE, ABASTOS, IDH
## num (1): POBREZA
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(data3)

#limpieza

data3 = data3%>%filter(TIPO == "Provincia")%>%dplyr::select(c(4:8))

data3$IVIA = gsub(",", ".", data3$IVIA)
data3$IDE = gsub(",", ".", data3$IDE)
data3$ABASTOS = gsub(",", ".", data3$ABASTOS)
data3$IDH = gsub(",", ".", data3$IDH)
data3$POBREZA = gsub(",", ".", data3$POBREZA)

data3 = as.data.frame(lapply(data3, as.numeric))
## Warning in lapply(data3, as.numeric): NAs introducidos por coerción
data3 = data3[complete.cases(data3),]
data3$IDH_cat = ifelse(data3$IDH < 0.430, 0, 1)
data3$IDH_cat = factor(data3$IDH_cat, levels = c(0,1), labels = c("No desarrollo humano", "Desarrollo humano"))
modelologistico <- glm(IDH_cat ~ ABASTOS + IVIA + IDE + POBREZA, data = data3, family = binomial(link = "logit"))
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(modelologistico)
## 
## Call:
## glm(formula = IDH_cat ~ ABASTOS + IVIA + IDE + POBREZA, family = binomial(link = "logit"), 
##     data = data3)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -2.95574  -0.09568   0.00000   0.01236   2.84968  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  2.855e+01  7.775e+00   3.672 0.000241 ***
## ABASTOS      3.187e-01  1.767e-01   1.804 0.071252 .  
## IVIA        -4.268e+01  1.024e+01  -4.167 3.08e-05 ***
## IDE         -7.285e+00  5.637e+00  -1.292 0.196215    
## POBREZA     -6.472e-04  5.893e-04  -1.098 0.272082    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 253.643  on 182  degrees of freedom
## Residual deviance:  46.614  on 178  degrees of freedom
## AIC: 56.614
## 
## Number of Fisher Scoring iterations: 9