library(rio)
data= import("PLAN.xlsx")
## New names:
## • `` -> `...1`
str(data)
## 'data.frame':    188 obs. of  10 variables:
##  $ ...1                                                                                       : chr  "Provincia" "Provincia" "Provincia" "Provincia" ...
##  $ Ubigeo                                                                                     : chr  "010200" "010300" "010100" "010400" ...
##  $ Región /
## Provincia                                                                      : chr  "BAGUA" "BONGARÁ" "CHACHAPOYAS" "CONDORCANQUI" ...
##  $ Población con Discapacidad
## (2020)
## 1c/                                                : num  1586 398 1082 561 1556 ...
##  $ Porcentaje de nacidos vivos con bajo peso al nacer (2020)
## 5/ 6/                         : num  6.41 4.99 8.77 10.35 5.98 ...
##  $ Total                                                                                      : num  986 343 889 653 614 ...
##  $ Número  de niños menores de un año con DNI emitido hasta los 30 días de nacido (2020)
## 7/: num  13 4 40 2 22 12 91 113 824 61 ...
##  $ Índice de Densidad del Estado -IDE
## (2017)
## 12/                                        : num  0.649 0.716 0.767 0.409 0.705 ...
##  $ Porcentaje de la población en pobreza extrema 
## 17a/                                     : num  23.1 10.7 11.7 38.1 25 ...
##  $ Devengado per cápita en soles (2020) - Inversión
## 19/                                    : num  1050 1288 5187 596 1797 ...
colnames(data)<- c("tipo", "ubigeo", "provincia", "disc", "peso", "t", "dni", "ide", "pobext","deven")
str(data)
## 'data.frame':    188 obs. of  10 variables:
##  $ tipo     : chr  "Provincia" "Provincia" "Provincia" "Provincia" ...
##  $ ubigeo   : chr  "010200" "010300" "010100" "010400" ...
##  $ provincia: chr  "BAGUA" "BONGARÁ" "CHACHAPOYAS" "CONDORCANQUI" ...
##  $ disc     : num  1586 398 1082 561 1556 ...
##  $ peso     : num  6.41 4.99 8.77 10.35 5.98 ...
##  $ t        : num  986 343 889 653 614 ...
##  $ dni      : num  13 4 40 2 22 12 91 113 824 61 ...
##  $ ide      : num  0.649 0.716 0.767 0.409 0.705 ...
##  $ pobext   : num  23.1 10.7 11.7 38.1 25 ...
##  $ deven    : num  1050 1288 5187 596 1797 ...
modelo1=formula(peso~deven+pobext)
reg1=lm(modelo1,data=data)
summary(reg1)
## 
## Call:
## lm(formula = modelo1, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.9124 -1.1477 -0.1365  1.1693  6.1700 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  6.124e+00  2.447e-01  25.028  < 2e-16 ***
## deven       -2.683e-04  9.117e-05  -2.943  0.00366 ** 
## pobext       5.994e-02  1.202e-02   4.986 1.41e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.821 on 185 degrees of freedom
## Multiple R-squared:   0.15,  Adjusted R-squared:  0.1409 
## F-statistic: 16.33 on 2 and 185 DF,  p-value: 2.945e-07
modelo2=formula(peso~deven+pobext+disc)
reg2=lm(modelo2,data=data)
summary(reg2)
## 
## Call:
## lm(formula = modelo2, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.9103 -1.1471 -0.1321  1.1713  6.1747 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  6.116e+00  3.108e-01  19.677  < 2e-16 ***
## deven       -2.673e-04  9.433e-05  -2.834  0.00511 ** 
## pobext       6.011e-02  1.264e-02   4.757 3.96e-06 ***
## disc         4.207e-06  9.532e-05   0.044  0.96484    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.826 on 184 degrees of freedom
## Multiple R-squared:  0.1501, Adjusted R-squared:  0.1362 
## F-statistic: 10.83 on 3 and 184 DF,  p-value: 1.383e-06
modelo3=formula(peso~deven+pobext+disc+ide)
reg3=lm(modelo3,data=data)
summary(reg3)
## 
## Call:
## lm(formula = modelo3, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.6682 -1.1276 -0.1804  1.1499  6.2856 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  8.602e+00  1.363e+00   6.313 2.02e-09 ***
## deven       -2.511e-04  9.409e-05  -2.669   0.0083 ** 
## pobext       5.279e-02  1.315e-02   4.016 8.64e-05 ***
## disc         5.663e-05  9.873e-05   0.574   0.5670    
## ide         -3.589e+00  1.916e+00  -1.873   0.0626 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.814 on 183 degrees of freedom
## Multiple R-squared:  0.166,  Adjusted R-squared:  0.1478 
## F-statistic: 9.109 on 4 and 183 DF,  p-value: 9.857e-07
library(magrittr)
library(knitr)
tanova=anova(reg1,reg2,reg3)

kable(tanova,
      caption = "Tabla ANOVA para comparar modelos")%>%kableExtra::kable_styling(full_width = FALSE)
Tabla ANOVA para comparar modelos
Res.Df RSS Df Sum of Sq F Pr(>F)
185 613.4800 NA NA NA NA
184 613.4735 1 0.006496 0.0019749 0.9646019
183 601.9294 1 11.544117 3.5096694 0.0626060

PREGUNTA 2

dontselect=c("tipo","ubigeo","provincia",
             "dni","t","ide", "deven")
select=setdiff(names(data),dontselect) 
theData=data[,select]
library(polycor)
## Warning: package 'polycor' was built under R version 4.3.2
corMatrix=polycor::hetcor(theData)$correlations
round(corMatrix,2)
##         disc  peso pobext
## disc    1.00 -0.05  -0.30
## peso   -0.05  1.00   0.33
## pobext -0.30  0.33   1.00
library(ggcorrplot)
## Warning: package 'ggcorrplot' was built under R version 4.3.2
## Loading required package: ggplot2
ggcorrplot(corMatrix)

library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
## The following object is masked from 'package:polycor':
## 
##     polyserial
psych::KMO(corMatrix)
## Kaiser-Meyer-Olkin factor adequacy
## Call: psych::KMO(r = corMatrix)
## Overall MSA =  0.5
## MSA for each item = 
##   disc   peso pobext 
##    0.5    0.5    0.5
cortest.bartlett(corMatrix,n=nrow(theData))$p.value>0.05
## [1] FALSE
library(matrixcalc)

is.singular.matrix(corMatrix)
## [1] FALSE