library(readxl)
Provincia_ExamenParcial <- read_excel("C:/Users/Mariano/Desktop/Estadistica 2/Examen Parcial/Provincia ExamenParcial.xlsx")
## New names:
## • `Población de niños menores de un año
 (CENSO 2017)
 1a/` -> `Población de
##   niños menores de un año
 (CENSO 2017)
 1a/...6`
## • `Población de niños menores de un año
 (CENSO 2017)
 1a/` -> `Población de
##   niños menores de un año
 (CENSO 2017)
 1a/...18`
View(Provincia_ExamenParcial)
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(rio)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ readr     2.1.4
## ✔ ggplot2   3.4.3     ✔ stringr   1.5.0
## ✔ lubridate 1.9.2     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.0
## ── 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
data <- Provincia_ExamenParcial[c(1,2,25, 30, 43, 15)]
colnames(data) <- c("UBIGEO", "Provincia", "IVIA", "IDE", "Devengado_INV", "NumeroDistritos")
model <- lm(Devengado_INV ~ IVIA + IDE, data=data)
summary(model)
## 
## Call:
## lm(formula = Devengado_INV ~ IVIA + IDE, data = data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
##  -1381   -719   -361     83  11542 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   -278.6     1452.3  -0.192    0.848
## IVIA          1255.8      787.2   1.595    0.112
## IDE           1591.7     1697.0   0.938    0.349
## 
## Residual standard error: 1437 on 193 degrees of freedom
## Multiple R-squared:  0.01304,    Adjusted R-squared:  0.002814 
## F-statistic: 1.275 on 2 and 193 DF,  p-value: 0.2817
model1 <- lm(Devengado_INV ~ IVIA + IDE, data=data, offset =NumeroDistritos)
summary(model1)
## 
## Call:
## lm(formula = Devengado_INV ~ IVIA + IDE, data = data, offset = NumeroDistritos)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1380.8  -713.0  -359.4    80.3 11545.0 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   -262.3     1452.6  -0.181    0.857
## IVIA          1250.6      787.3   1.588    0.114
## IDE           1557.9     1697.4   0.918    0.360
## 
## Residual standard error: 1437 on 193 degrees of freedom
## Multiple R-squared:  0.01295,    Adjusted R-squared:  0.002721 
## F-statistic: 1.266 on 2 and 193 DF,  p-value: 0.2843
summary(model1)$coef[,-1] 
##             Std. Error    t value  Pr(>|t|)
## (Intercept)  1452.5860 -0.1805602 0.8569023
## IVIA          787.3197  1.5884905 0.1138123
## IDE          1697.3645  0.9178141 0.3598617
plot(model1)

anova(model1)
## Analysis of Variance Table
## 
## Response: Devengado_INV
##            Df    Sum Sq Mean Sq F value Pr(>F)
## IVIA        1   3489233 3489233  1.6896 0.1952
## IDE         1   1739596 1739596  0.8424 0.3599
## Residuals 193 398562353 2065090
library(readxl)
admision <- read_excel("C:/Users/Mariano/Desktop/Estadistica 2/Examen Parcial/admision.xlsx")
View(admision)
regresion_logistica <- glm(admitido ~ letras + ciencias + prestigio, data = admision, family = "binomial")
summary(regresion_logistica)
## 
## Call:
## glm(formula = admitido ~ letras + ciencias + prestigio, family = "binomial", 
##     data = admision)
## 
## Coefficients:
##              Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -6.249705   1.155732  -5.408 6.39e-08 ***
## letras       0.002294   0.001092   2.101   0.0356 *  
## ciencias     0.007770   0.003275   2.373   0.0177 *  
## prestigio    0.560031   0.127137   4.405 1.06e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 499.98  on 399  degrees of freedom
## Residual deviance: 459.44  on 396  degrees of freedom
## AIC: 467.44
## 
## Number of Fisher Scoring iterations: 4
library(readxl)
dataCarcel <- read_excel("C:/Users/Mariano/Desktop/Estadistica 2/Examen Parcial/dataCarcel.xlsx")
View(dataCarcel)
modelo_poisson <- glm(vecesEnCarcel ~ edad + casado, data = dataCarcel, family = "poisson")

# Obtener un resumen del modelo
summary(modelo_poisson)
## 
## Call:
## glm(formula = vecesEnCarcel ~ edad + casado, family = "poisson", 
##     data = dataCarcel)
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  1.50705    0.12158  12.395  < 2e-16 ***
## edad        -0.01686    0.00499  -3.379 0.000727 ***
## casado      -0.03603    0.08935  -0.403 0.686761    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 1008.54  on 431  degrees of freedom
## Residual deviance:  995.44  on 429  degrees of freedom
## AIC: 2111.3
## 
## Number of Fisher Scoring iterations: 5