setwd("C:/Users/marcogeovanni/Desktop/Modern Guide To Econometrics/Chapter 5")
library(readstata13)#Necesitamos estimar los retornos de la escolaridad
Schooling <- read.dta13("schooling.dta") #Utilizamos la base de datos Schooling.dta, disponible en MIU
attach(Schooling)
summary(Schooling)
##      smsa66             id           nearc2           nearc4      
##  Min.   :0.0000   Min.   :   2   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:0.0000   1st Qu.:1276   1st Qu.:0.0000   1st Qu.:0.0000  
##  Median :1.0000   Median :2541   Median :0.0000   Median :1.0000  
##  Mean   :0.6495   Mean   :2582   Mean   :0.4409   Mean   :0.6821  
##  3rd Qu.:1.0000   3rd Qu.:3859   3rd Qu.:1.0000   3rd Qu.:1.0000  
##  Max.   :1.0000   Max.   :5225   Max.   :1.0000   Max.   :1.0000  
##                                                                   
##     nearc4a          nearc4b            ed76            ed66      
##  Min.   :0.0000   Min.   :0.0000   Min.   : 1.00   Min.   : 0.00  
##  1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:12.00   1st Qu.: 9.00  
##  Median :0.0000   Median :0.0000   Median :13.00   Median :11.00  
##  Mean   :0.4927   Mean   :0.1894   Mean   :13.26   Mean   :10.76  
##  3rd Qu.:1.0000   3rd Qu.:0.0000   3rd Qu.:16.00   3rd Qu.:12.00  
##  Max.   :1.0000   Max.   :1.0000   Max.   :18.00   Max.   :18.00  
##                                                                   
##      age76           daded           nodaded           momed      
##  Min.   :24.00   Min.   : 0.000   Min.   :0.0000   Min.   : 0.00  
##  1st Qu.:25.00   1st Qu.: 8.000   1st Qu.:0.0000   1st Qu.: 9.00  
##  Median :28.00   Median : 9.940   Median :0.0000   Median :11.00  
##  Mean   :28.12   Mean   : 9.989   Mean   :0.2292   Mean   :10.34  
##  3rd Qu.:31.00   3rd Qu.:12.000   3rd Qu.:0.0000   3rd Qu.:12.00  
##  Max.   :34.00   Max.   :18.000   Max.   :1.0000   Max.   :18.00  
##                                                                   
##     nomomed          momdad14         sinmom14          step14       
##  Min.   :0.0000   Min.   :0.0000   Min.   :0.0000   Min.   :0.00000  
##  1st Qu.:0.0000   1st Qu.:1.0000   1st Qu.:0.0000   1st Qu.:0.00000  
##  Median :0.0000   Median :1.0000   Median :0.0000   Median :0.00000  
##  Mean   :0.1173   Mean   :0.7894   Mean   :0.1007   Mean   :0.03887  
##  3rd Qu.:0.0000   3rd Qu.:1.0000   3rd Qu.:0.0000   3rd Qu.:0.00000  
##  Max.   :1.0000   Max.   :1.0000   Max.   :1.0000   Max.   :1.00000  
##                                                                      
##     south66          lwage76          famed           black       
##  Min.   :0.0000   Min.   :4.605   Min.   :1.000   Min.   :0.0000  
##  1st Qu.:0.0000   1st Qu.:5.977   1st Qu.:3.000   1st Qu.:0.0000  
##  Median :0.0000   Median :6.287   Median :6.000   Median :0.0000  
##  Mean   :0.4143   Mean   :6.262   Mean   :5.934   Mean   :0.2336  
##  3rd Qu.:1.0000   3rd Qu.:6.564   3rd Qu.:8.000   3rd Qu.:0.0000  
##  Max.   :1.0000   Max.   :7.785   Max.   :9.000   Max.   :1.0000  
##                                                                   
##      smsa76         south76           wage76          enroll76      
##  Min.   :0.000   Min.   :0.0000   Min.   : 100.0   Min.   :0.00000  
##  1st Qu.:0.000   1st Qu.:0.0000   1st Qu.: 394.2   1st Qu.:0.00000  
##  Median :1.000   Median :0.0000   Median : 537.5   Median :0.00000  
##  Mean   :0.713   Mean   :0.4037   Mean   : 577.3   Mean   :0.09236  
##  3rd Qu.:1.000   3rd Qu.:1.0000   3rd Qu.: 708.8   3rd Qu.:0.00000  
##  Max.   :1.000   Max.   :1.0000   Max.   :2404.0   Max.   :1.00000  
##                                                                     
##       kww           iqscore          mar76          libcrd14     
##  Min.   : 4.00   Min.   : 50.0   Min.   :1.000   Min.   :0.0000  
##  1st Qu.:28.00   1st Qu.: 93.0   1st Qu.:1.000   1st Qu.:0.0000  
##  Median :34.00   Median :103.0   Median :1.000   Median :1.0000  
##  Mean   :33.54   Mean   :102.4   Mean   :2.271   Mean   :0.6743  
##  3rd Qu.:40.00   3rd Qu.:113.0   3rd Qu.:4.000   3rd Qu.:1.0000  
##  Max.   :56.00   Max.   :149.0   Max.   :6.000   Max.   :1.0000  
##  NA's   :47      NA's   :949     NA's   :7       NA's   :13      
##      region          exp76            exp762           _merge 
##  Min.   :1.000   Min.   : 0.000   Min.   :  0.00   Min.   :3  
##  1st Qu.:3.000   1st Qu.: 6.000   1st Qu.: 36.00   1st Qu.:3  
##  Median :5.000   Median : 8.000   Median : 64.00   Median :3  
##  Mean   :4.638   Mean   : 8.856   Mean   : 95.58   Mean   :3  
##  3rd Qu.:6.000   3rd Qu.:11.000   3rd Qu.:121.00   3rd Qu.:3  
##  Max.   :9.000   Max.   :23.000   Max.   :529.00   Max.   :3  
## 
E1 <- lm(lwage76~ed76 + exp76 + exp762 + black + smsa76 + south76)#corremos una regresión explicando el logaritmo del salario 
summary(E1)
## 
## Call:
## lm(formula = lwage76 ~ ed76 + exp76 + exp762 + black + smsa76 + 
##     south76)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.59297 -0.22315  0.01893  0.24223  1.33190 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  4.7336643  0.0676026  70.022  < 2e-16 ***
## ed76         0.0740090  0.0035054  21.113  < 2e-16 ***
## exp76        0.0835958  0.0066478  12.575  < 2e-16 ***
## exp762      -0.0022409  0.0003178  -7.050 2.21e-12 ***
## black       -0.1896315  0.0176266 -10.758  < 2e-16 ***
## smsa76       0.1614230  0.0155733  10.365  < 2e-16 ***
## south76     -0.1248615  0.0151182  -8.259  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3742 on 3003 degrees of freedom
## Multiple R-squared:  0.2905, Adjusted R-squared:  0.2891 
## F-statistic: 204.9 on 6 and 3003 DF,  p-value: < 2.2e-16
age762<-age76^2# definimos una variable de edad al cuadrado
VISchooling <- lm(ed76~age76 + age762 + black + smsa76 + south76 + nearc4) #Etapa 1 de 2SLS, forma reducida de Schooling
VIexper <- lm(exp76~age76 + age762 + black + smsa76 + south76 + nearc4)#Etapa 1 de 2SLS, forma reducida Age
VIexper2 <- lm(exp762~age76 + age762 + black + smsa76 + south76 + nearc4)#Etapa 1 de 2SLS, forma reducida Af2^2
summary(VISchooling)
## 
## Call:
## lm(formula = ed76 ~ age76 + age762 + black + smsa76 + south76 + 
##     nearc4)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -12.511  -1.722  -0.296   1.876   7.199 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.869524   4.298357  -0.435 0.663638    
## age76        1.061441   0.301398   3.522 0.000435 ***
## age762      -0.018760   0.005231  -3.586 0.000341 ***
## black       -1.468367   0.115443 -12.719  < 2e-16 ***
## smsa76       0.835403   0.109252   7.647 2.76e-14 ***
## south76     -0.459700   0.102434  -4.488 7.47e-06 ***
## nearc4       0.347105   0.106997   3.244 0.001191 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.516 on 3003 degrees of freedom
## Multiple R-squared:  0.1185, Adjusted R-squared:  0.1168 
## F-statistic: 67.29 on 6 and 3003 DF,  p-value: < 2.2e-16
summary(VIexper)
## 
## Call:
## lm(formula = exp76 ~ age76 + age762 + black + smsa76 + south76 + 
##     nearc4)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -7.199 -1.876  0.296  1.722 12.511 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -4.130476   4.298357  -0.961 0.336658    
## age76       -0.061441   0.301398  -0.204 0.838482    
## age762       0.018760   0.005231   3.586 0.000341 ***
## black        1.468367   0.115443  12.719  < 2e-16 ***
## smsa76      -0.835403   0.109252  -7.647 2.76e-14 ***
## south76      0.459700   0.102434   4.488 7.47e-06 ***
## nearc4      -0.347105   0.106997  -3.244 0.001191 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.516 on 3003 degrees of freedom
## Multiple R-squared:  0.6318, Adjusted R-squared:  0.631 
## F-statistic: 858.7 on 6 and 3003 DF,  p-value: < 2.2e-16
summary(VIexper2)
## 
## Call:
## lm(formula = exp762 ~ age76 + age762 + black + smsa76 + south76 + 
##     nearc4)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -168.49  -30.21   -1.32   25.30  389.71 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 609.9425    90.2203   6.761 1.64e-11 ***
## age76       -55.4520     6.3262  -8.765  < 2e-16 ***
## age762        1.3132     0.1098  11.960  < 2e-16 ***
## black        28.2062     2.4231  11.641  < 2e-16 ***
## smsa76      -17.5543     2.2932  -7.655 2.59e-14 ***
## south76      11.1770     2.1500   5.199 2.14e-07 ***
## nearc4       -7.2510     2.2458  -3.229  0.00126 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 52.81 on 3003 degrees of freedom
## Multiple R-squared:  0.6114, Adjusted R-squared:  0.6106 
## F-statistic: 787.3 on 6 and 3003 DF,  p-value: < 2.2e-16
SchoolingVI <-fitted(VISchooling)#tomamos los valores ajustado de la forma reducida y los guardamos en una variable
ExperVI <-fitted(VIexper)#tomamos los valores ajustado de la forma reducida y los guardamos en una variable
Exper2VI <-fitted(VIexper2)#tomamos los valores ajustados de la forma reducida y los guardamos en una variable
TSls <- lm(lwage76~SchoolingVI + ExperVI + Exper2VI + black + smsa76 + south76)# corremos una regresión E1 solo que sustituimos las variables con los valores ajustados de sus instrumentos
summary(TSls)
## 
## Call:
## lm(formula = lwage76 ~ SchoolingVI + ExperVI + Exper2VI + black + 
##     smsa76 + south76)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.5902 -0.2418  0.0124  0.2598  1.3706 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  4.0656674  0.5831172   6.972 3.82e-12 ***
## SchoolingVI  0.1329473  0.0492365   2.700 0.006969 ** 
## ExperVI      0.0559614  0.0249103   2.247 0.024743 *  
## Exper2VI    -0.0007957  0.0012844  -0.619 0.535648    
## black       -0.1031403  0.0741459  -1.391 0.164315    
## smsa76       0.1079848  0.0476654   2.265 0.023554 *  
## south76     -0.0981752  0.0275648  -3.562 0.000374 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3864 on 3003 degrees of freedom
## Multiple R-squared:  0.2436, Adjusted R-squared:  0.2421 
## F-statistic: 161.2 on 6 and 3003 DF,  p-value: < 2.2e-16