Guia practica 2

Luis Ernesto Juarez Linares

29 de marzo de 2019

EJEMPLO DE REGRESION

Cargar datos

Ruta <-read.csv("C:/Users/luisn/Downloads/ejemplo_regresion.csv")
colnames(Ruta)<- c("X1", "X2", "Y")
library(readr)
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
head(Ruta,n=7)
##     X1   X2    Y
## 1 3.92 7298 0.75
## 2 3.61 6855 0.71
## 3 3.32 6636 0.66
## 4 3.07 6506 0.61
## 5 3.06 6450 0.70
## 6 3.11 6402 0.72
## 7 3.21 6368 0.77
print(Ruta)
##      X1   X2    Y
## 1  3.92 7298 0.75
## 2  3.61 6855 0.71
## 3  3.32 6636 0.66
## 4  3.07 6506 0.61
## 5  3.06 6450 0.70
## 6  3.11 6402 0.72
## 7  3.21 6368 0.77
## 8  3.26 6340 0.74
## 9  3.42 6349 0.90
## 10 3.42 6352 0.82
## 11 3.45 6361 0.75
## 12 3.58 6369 0.77
## 13 3.66 6546 0.78
## 14 3.78 6672 0.84
## 15 3.82 6890 0.79
## 16 3.97 7115 0.70
## 17 4.07 7327 0.68
## 18 4.25 7546 0.72
## 19 4.41 7931 0.55
## 20 4.49 8097 0.63
## 21 4.70 8468 0.56
## 22 4.58 8717 0.41
## 23 4.69 8991 0.51
## 24 4.71 9179 0.47
## 25 4.78 9318 0.32

Ejemplo regresion lineal

library(stargazer)
options(scipen = 9999)
modelo_lineal<-lm(formula = Y~X1+X2,data = Ruta)
summary(modelo_lineal)
## 
## Call:
## lm(formula = Y ~ X1 + X2, data = Ruta)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.085090 -0.039102 -0.003341  0.030236  0.105692 
## 
## Coefficients:
##                Estimate  Std. Error t value            Pr(>|t|)    
## (Intercept)  1.56449677  0.07939598  19.705 0.00000000000000182 ***
## X1           0.23719747  0.05555937   4.269            0.000313 ***
## X2          -0.00024908  0.00003205  -7.772 0.00000009508790794 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.0533 on 22 degrees of freedom
## Multiple R-squared:  0.8653, Adjusted R-squared:  0.8531 
## F-statistic: 70.66 on 2 and 22 DF,  p-value: 0.000000000265
stargazer(modelo_lineal,title="Ejemplo regresion lineal", type="text",digits=8)
## 
## Ejemplo regresion lineal
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                  Y             
## -----------------------------------------------
## X1                         0.23719750***       
##                            (0.05555937)        
##                                                
## X2                        -0.00024908***       
##                            (0.00003205)        
##                                                
## Constant                   1.56449700***       
##                            (0.07939598)        
##                                                
## -----------------------------------------------
## Observations                    25             
## R2                          0.86529610         
## Adjusted R2                 0.85305030         
## Residual Std. Error    0.05330222 (df = 22)    
## F Statistic         70.66057000*** (df = 2; 22)
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

Vector de coeficientes estimados β/hat

options(scipen=999)
modelo_lineal$coefficients
##   (Intercept)            X1            X2 
##  1.5644967711  0.2371974748 -0.0002490793

Matriz de varianza-covarianza de los parametros V[β]

var_covar<-vcov(modelo_lineal)
print(var_covar)
##                  (Intercept)              X1                 X2
## (Intercept)  0.0063037218732  0.000240996434 -0.000000982806321
## X1           0.0002409964344  0.003086843196 -0.000001675537651
## X2          -0.0000009828063 -0.000001675538  0.000000001027106

Intervalos de confianza

confint(object = modelo_lineal,level = .95)
##                     2.5 %        97.5 %
## (Intercept)  1.3998395835  1.7291539588
## X1           0.1219744012  0.3524205485
## X2          -0.0003155438 -0.0001826148

Valores ajustados Y/hat

plot(modelo_lineal$fitted.values,main = "Valores Ajustados",
    ylab = "Y",xlab = "casos")

modelo_lineal$fitted.values  %>% as.matrix()
##         [,1]
## 1  0.6765303
## 2  0.7133412
## 3  0.6991023
## 4  0.6721832
## 5  0.6837597
## 6  0.7075753
## 7  0.7397638
## 8  0.7585979
## 9  0.7943078
## 10 0.7935605
## 11 0.7984347
## 12 0.8272778
## 13 0.8021665
## 14 0.7992462
## 15 0.7544349
## 16 0.7339716
## 17 0.7048866
## 18 0.6930338
## 19 0.6350898
## 20 0.6127185
## 21 0.5701215
## 22 0.4796371
## 23 0.4374811
## 24 0.3953981
## 25 0.3773799

Residuos del modelo ϵ/hat

plot(modelo_lineal$residuals,main = "Residuos",
     ylab = "Residuos",xlab = "casos")

modelo_lineal$residuals %>% as.matrix()
##            [,1]
## 1   0.073469743
## 2  -0.003341163
## 3  -0.039102258
## 4  -0.062183196
## 5   0.016240338
## 6   0.012424659
## 7   0.030236216
## 8  -0.018597878
## 9   0.105692240
## 10  0.026439478
## 11 -0.048434733
## 12 -0.057277771
## 13 -0.022166535
## 14  0.040753758
## 15  0.035565142
## 16 -0.033971640
## 17 -0.024886579
## 18  0.026966239
## 19 -0.085089833
## 20  0.017281530
## 21 -0.010121525
## 22 -0.069637086
## 23  0.072518915
## 24  0.074601871
## 25 -0.057379932

Practica 2

Cargar datos

library(readr)
library(dplyr)
Mnueva<-read_csv("C:/Users/luisn/Downloads/ejercicio econometria.csv")
head(Mnueva,n=20)
## # A tibble: 14 x 3
##        Y    x1    x2
##    <dbl> <dbl> <dbl>
##  1   320    50   7.4
##  2   450    53   5.1
##  3   370    60   4.2
##  4   470    63   3.9
##  5   420    69   1.4
##  6   500    82   2.2
##  7   570   100   7  
##  8   640   104   5.7
##  9   670   113  13.1
## 10   780   130  16.4
## 11   690   150   5.1
## 12   700   181   2.9
## 13   910   202   4.5
## 14   930   217   6.2
Mnueva%>%mutate(x3=x1*x2)%>%select(Y,x1,x2,x3)->Mnueva
print(Mnueva)
## # A tibble: 14 x 4
##        Y    x1    x2     x3
##    <dbl> <dbl> <dbl>  <dbl>
##  1   320    50   7.4  370  
##  2   450    53   5.1  270. 
##  3   370    60   4.2  252  
##  4   470    63   3.9  246. 
##  5   420    69   1.4   96.6
##  6   500    82   2.2  180. 
##  7   570   100   7    700  
##  8   640   104   5.7  593. 
##  9   670   113  13.1 1480. 
## 10   780   130  16.4 2132  
## 11   690   150   5.1  765  
## 12   700   181   2.9  525. 
## 13   910   202   4.5  909  
## 14   930   217   6.2 1345.

Modelo de regresion multiple

library(stargazer)
options(scipen = 9999)
modelo_r<-lm(formula = Y~x1+x2+x3,data = Mnueva)
summary(modelo_r)
## 
## Call:
## lm(formula = Y ~ x1 + x2 + x3, data = Mnueva)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -54.862 -34.639  -2.686  29.659  75.272 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 329.0317    85.5810   3.845  0.00324 **
## x1            1.8935     0.7226   2.621  0.02557 * 
## x2          -19.2815    16.6132  -1.161  0.27276   
## x3            0.2508     0.1381   1.816  0.09937 . 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 48.91 on 10 degrees of freedom
## Multiple R-squared:  0.9497, Adjusted R-squared:  0.9347 
## F-statistic: 62.99 on 3 and 10 DF,  p-value: 0.0000008497
stargazer(modelo_r,title="Modelo de Regresion Lineal", type="text",digits=8)
## 
## Modelo de Regresion Lineal
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                  Y             
## -----------------------------------------------
## x1                         1.89347600**        
##                            (0.72255180)        
##                                                
## x2                         -19.28153000        
##                            (16.61324000)       
##                                                
## x3                          0.25080920*        
##                            (0.13808420)        
##                                                
## Constant                  329.03170000***      
##                            (85.58103000)       
##                                                
## -----------------------------------------------
## Observations                    14             
## R2                          0.94974120         
## Adjusted R2                 0.93466350         
## Residual Std. Error    48.90988000 (df = 10)   
## F Statistic         62.99002000*** (df = 3; 10)
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

Vector de Coeficientes estimados

options(scipen=999)
modelo_r$coefficient
## (Intercept)          x1          x2          x3 
## 329.0317432   1.8934760 -19.2815258   0.2508092

Matriz de Varianza Covarianza

var_covar<-vcov(modelo_r)
print(var_covar)
##             (Intercept)           x1           x2          x3
## (Intercept)  7324.11290 -59.33025065 -1330.529339 10.79320150
## x1            -59.33025   0.52208115    10.997904 -0.09397975
## x2          -1330.52934  10.99790406   275.999661 -2.24756310
## x3             10.79320  -0.09397975    -2.247563  0.01906724

Intervalos de Confianza

confint(object = modelo_r,level = .95)
##                    2.5 %      97.5 %
## (Intercept) 138.34532258 519.7181637
## x1            0.28353022   3.5034218
## x2          -56.29812575  17.7350742
## x3           -0.05686157   0.5584799

Valores Ajustados

plot(modelo_r$fitted.values,main = "Valores Ajustados",ylab = "Y",xlab = "casos")

modelo_r$fitted.values %>% as.matrix()
##        [,1]
## 1  373.8217
## 2  398.8439
## 3  424.8618
## 4  434.7466
## 5  456.9156
## 6  487.1234
## 7  558.9751
## 8  564.7282
## 9  661.6794
## 10 793.6918
## 11 706.5864
## 12 747.4842
## 13 852.7326
## 14 957.8093

Residuos del Modelo

plot(modelo_r$residuals,main = "Residuos",ylab = "Residuos",xlab = "casos")

modelo_r$residuals %>% as.matrix()
##         [,1]
## 1  -53.82165
## 2   51.15608
## 3  -54.86181
## 4   35.25340
## 5  -36.91562
## 6   12.87660
## 7   11.02490
## 8   75.27176
## 9    8.32061
## 10 -13.69180
## 11 -16.58640
## 12 -47.48422
## 13  57.26741
## 14 -27.80927