library(knitr)
library(magrittr)
library(kableExtra)
## Warning: package 'kableExtra' was built under R version 4.3.3
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:kableExtra':
## 
##     group_rows
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readxl)
library(rio)
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(modelsummary)
## Warning: package 'modelsummary' was built under R version 4.3.3
## Version 2.0.0 of `modelsummary`, to be released soon, will introduce a
##   breaking change: The default table-drawing package will be `tinytable`
##   instead of `kableExtra`. All currently supported table-drawing packages
##   will continue to be supported for the foreseeable future, including
##   `kableExtra`, `gt`, `huxtable`, `flextable, and `DT`.
##   
##   You can always call the `config_modelsummary()` function to change the
##   default table-drawing package in persistent fashion. To try `tinytable`
##   now:
##   
##   config_modelsummary(factory_default = 'tinytable')
##   
##   To set the default back to `kableExtra`:
##   
##   config_modelsummary(factory_default = 'kableExtra')
library(DescTools)
## 
## Attaching package: 'DescTools'
## The following objects are masked from 'package:modelsummary':
## 
##     Format, Mean, Median, N, SD, Var
library(margins)
## Warning: package 'margins' was built under R version 4.3.3
library(lm.beta)
## Warning: package 'lm.beta' was built under R version 4.3.3
library(readxl)
dataPeru <- read_excel("C:/Users/JHOJAN/Downloads/dataPeru.xlsx", 
    col_types = c("text", "text", "numeric", 
        "numeric", "numeric", "numeric", 
        "numeric", "numeric"))
View(dataPeru)

#convirtiendo a porcentaje

dataPeru$porc_contribuyentes <- (dataPeru$contribuyentesSunat / dataPeru$pobTotal) * 100
dataPeru$porc_ocupada <- (dataPeru$peaOcupada / dataPeru$pobTotal) * 100

#regresion lineal 1

h1=formula(buenEstado~porc_contribuyentes+porc_ocupada)
reg1=lm(h1,data=dataPeru)
summary(reg1)
## 
## Call:
## lm(formula = h1, data = dataPeru)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -10.0928  -4.3610   0.2575   4.4003  11.0196 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)
## (Intercept)         -22.6095    15.9617  -1.416    0.171
## porc_contribuyentes   0.1003     0.3121   0.321    0.751
## porc_ocupada          1.0218     0.6424   1.590    0.126
## 
## Residual standard error: 6.299 on 22 degrees of freedom
## Multiple R-squared:  0.4669, Adjusted R-squared:  0.4184 
## F-statistic: 9.633 on 2 and 22 DF,  p-value: 0.000989

#regresion lineal 2

h2=formula(peaOcupada~contribuyentesSunat+buenEstado)
reg2=lm(h2,data=dataPeru)
summary(reg2)
## 
## Call:
## lm(formula = h2, 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