##Abrimos la data

library(readxl)
dataPeru <- read_excel("dataPeru.xlsx")
View(dataPeru)

##Analizamos la data

#install.packages("tidyverse")
library("tidyverse")
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
glimpse(dataPeru)
## Rows: 25
## Columns: 8
## $ DEPARTAMENTO        <chr> "AMAZONAS", "ÁNCASH", "APURÍMAC", "AREQUIPA", "AYA…
## $ UBIGEO              <chr> "010000", "020000", "030000", "040000", "050000", …
## $ buenEstado          <dbl> 18.6, 13.9, 8.7, 27.4, 17.0, 18.0, 33.8, 11.9, 10.…
## $ contribuyentesSunat <dbl> 75035, 302906, 103981, 585628, 151191, 277457, 499…
## $ peaOcupada          <dbl> 130019, 387976, 140341, 645001, 235857, 461312, 44…
## $ pobUrbana           <dbl> 205976, 806065, 243354, 1383694, 444473, 567141, 1…
## $ PobRural            <dbl> 211389, 333050, 180905, 76739, 206467, 860386, 0, …
## $ pobTotal            <dbl> 417365, 1139115, 424259, 1460433, 650940, 1427527,…

##Para la pregunta 1, realizamos la fórmula para el modelo según la variables del caso y probamos hipótesis

modelo1=formula(buenEstado~contribuyentesSunat+peaOcupada)

reg1=lm(modelo1,data=dataPeru)
summary(reg1)
## 
## Call:
## lm(formula = modelo1, data = dataPeru)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -10.589  -3.966  -1.347   1.907  21.518 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          1.865e+01  2.694e+00   6.922 5.98e-07 ***
## contribuyentesSunat  1.786e-05  2.060e-05   0.867    0.395    
## peaOcupada          -1.596e-05  2.241e-05  -0.712    0.484    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 7.925 on 22 degrees of freedom
## Multiple R-squared:  0.1561, Adjusted R-squared:  0.07939 
## F-statistic: 2.035 on 2 and 22 DF,  p-value: 0.1546

###Es posible concluir que, ya que el nivel significancia es relevante para un 0,05, ninguna de las variables independientes es significativa

##Para la preguna 2, formulamos un nuevo modelo, y probamos hipótesis

modelo2=formula(peaOcupada~contribuyentesSunat+buenEstado)

reg2=lm(modelo2,data=dataPeru)
summary(reg2)
## 
## Call:
## lm(formula = modelo2, data = dataPeru)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -91867 -58573 -11166  46174 155851 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          1.155e+05  3.787e+04   3.049  0.00588 ** 
## contribuyentesSunat  9.206e-01  1.741e-02  52.872  < 2e-16 ***
## buenEstado          -1.412e+03  1.983e+03  -0.712  0.48395    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 74540 on 22 degrees of freedom
## Multiple R-squared:  0.9932, Adjusted R-squared:  0.9926 
## F-statistic:  1603 on 2 and 22 DF,  p-value: < 2.2e-16

###Para las variables mostradas, la contribuyentes sunat tiene efecto