Agustín Deuster

12/11/2020 10:20

rm(list=ls())

###P1 Carga los paquetes necesarios para trabajar con data.table y poder abrir la base de datos en formato .dta

install.packages("readstata13")
install.packages("data.table")
install.packages("tidyverse")
install.packages("leaflet")
install.packages("rgdal")
install.packages("deldir")
install.packages("sp")
install.packages("RColorBrewer")
install.packages("KernSmooth")
library(readstata13)
library(data.table)
library(ggplot2)
library(tidyverse)
## -- Attaching packages ---------------------------------------------------------------------------------------------- tidyverse 1.3.0 --
## v tibble  3.0.3     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## v purrr   0.3.4
## -- Conflicts ------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::between()   masks data.table::between()
## x dplyr::filter()    masks stats::filter()
## x dplyr::first()     masks data.table::first()
## x dplyr::lag()       masks stats::lag()
## x dplyr::last()      masks data.table::last()
## x purrr::transpose() masks data.table::transpose()
library(leaflet)
library(rgdal)
## Loading required package: sp
## rgdal: version: 1.5-18, (SVN revision 1082)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.0.4, released 2020/01/28
## Path to GDAL shared files: C:/Users/adeus/Documents/R/win-library/4.0/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 6.3.1, February 10th, 2020, [PJ_VERSION: 631]
## Path to PROJ shared files: C:/Users/adeus/Documents/R/win-library/4.0/rgdal/proj
## Linking to sp version:1.4-4
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
library(deldir)
## deldir 0.2-2      Nickname: "Stack Smashing Detected"
## 
##      Note 1: As of version 0.2-1, error handling in this 
##      package was amended to conform to the usual R protocol. 
##      The deldir() function now actually throws an error 
##      when one occurs, rather than displaying an error number 
##      and returning a NULL.
##  
##      Note 2:  As of version 0.1-29 the arguments "col" 
##      and "lty" of plot.deldir() had their names changed to 
##      "cmpnt_col" and "cmpnt_lty" respectively basically 
##      to allow "col" and and "lty" to be passed as "..." 
##      arguments.
##  
##      Note 3: As of version 0.1-29 the "plotit" argument 
##      of deldir() was changed to (simply) "plot".
##  
##      See the help for deldir() and plot.deldir().
library(sp)
library(RColorBrewer)
library(KernSmooth)
## KernSmooth 2.23 loaded
## Copyright M. P. Wand 1997-2009
library(sp)

###P2 Descargue la base de datos de la Casen 2015. Carga esta base de datos.

casen<-read.dta13("Casen2015.dta")
casen<-data.table(casen)

###P3 Crea un subconjunto de la base, que tendrá solo las variables que nos interesa analizar: folio,o,sexo,edad,region,zona,ecivil,esc,educ,pco1,ypch,ypchtot,ypchautcor,ypchtrabajo,pobreza,expr.

casen_var_int<-casen[,.(folio,o,sexo,edad,region,zona,ecivil,esc,educ,pco1,ypch,ypchtot,ypchautcor,ypchtrabajo,pobreza,expr)]

###P4 Crea la variable mujer = 1 cuando sexo = mujer y mujer = 0 cuando sexo = hombre

casen_var_int$mujer = ifelse(casen_var_int$sexo == "mujer",1,0)

###P5 Define experiencia laboral como la edad menos los años de escolaridad menos 6

casen_var_int$experiencialaboral = casen_var_int$edad-casen_var_int$esc-6

###P6 Renombra pco1 por “relacion_jf”

casen_var_int <- rename(casen_var_int, c("relacion_jf" = "pco1"))

Regresión lineal

###P1 Plantee un modelo poblacional de regresión lineal simple para poder predecir el ingreso

lineal_model<- lm(ypchtot ~ mujer , data= casen_var_int)
summary(lineal_model)
## 
## Call:
## lm(formula = ypchtot ~ mujer, data = casen_var_int)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
##  -301243  -164110   -95630    24169 54918332 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   301243       1143   263.6   <2e-16 ***
## mujer         -16773       1582   -10.6   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 408300 on 266966 degrees of freedom
## Multiple R-squared:  0.000421,   Adjusted R-squared:  0.0004172 
## F-statistic: 112.4 on 1 and 266966 DF,  p-value: < 2.2e-16

###P2 Calcule el ingreso predicho para todas las observaciones de la muestra

casen_var_int[,predicciones:=predict(lineal_model)]

###P3 Calcule el residuo o error de predicción de nuestro modelo

casen_var_int[,residual:=resid(lineal_model)]

###P4 Agregue variables que usted considere relevantes para estudiar las diferencias salariales entre hombres y mujeres. Interprete los coeficiente. Verifique si sus variable explicativa son significativas e interprete el R cuadrado

lineal_model2<- lm(ypchtot ~ mujer + esc + experiencialaboral, data = casen_var_int, na.action = "na.exclude")
summary(lineal_model2)
## 
## Call:
## lm(formula = ypchtot ~ mujer + esc + experiencialaboral, data = casen_var_int, 
##     na.action = "na.exclude")
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
##  -680382  -161925   -62791    58896 54567560 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        -254566.00    3905.04  -65.19   <2e-16 ***
## mujer               -27150.64    1777.23  -15.28   <2e-16 ***
## esc                  42406.33     256.79  165.14   <2e-16 ***
## experiencialaboral    4866.48      51.25   94.96   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 408300 on 212338 degrees of freedom
##   (54626 observations deleted due to missingness)
## Multiple R-squared:  0.1145, Adjusted R-squared:  0.1145 
## F-statistic:  9155 on 3 and 212338 DF,  p-value: < 2.2e-16

###P5 Calcule el ingreso predicho para todas las observaciones de la muestra.

casen_var_int[,predicciones2:=predict(lineal_model2)]

###P6 Calcule el residuo o error de predicción de nuestro modelo.

casen_var_int[,residual2:=resid(lineal_model2)]

###P7 Cuantifica la diferencia de salario entre un hombre y una mujer, ambos con 17 años de escolaridad y 5 años de experiencia laboral

edad17<-subset(casen_var_int, edad== 17)
diferencia <-subset(edad17, experiencialaboral== 5)

Mujeres <-subset(diferencia, sexo== "mujer")
Hombres <-subset(diferencia, sexo== "hombre")

ingmuj <- mean(Mujeres$ypchtot)
inghom <- mean(Hombres$ypchtot)

diferencia <- inghom-ingmuj

### La diferencia de salario entre un hombre y una mujer, ambos con 17 años de escolaridad y 5 años de experiencia laboral es:
diferencia
## [1] 61812.5

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.