practica2

José Manuel Canales López cl12025

21/3/2019

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(readr)
ejemplo_regresion <- read_csv("ejemplo_regresion.csv")
## Parsed with column specification:
## cols(
##   X1 = col_double(),
##   X2 = col_double(),
##   Y = col_double()
## )
head(ejemplo_regresion,n = 6)
## # A tibble: 6 x 3
##      X1    X2     Y
##   <dbl> <dbl> <dbl>
## 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.7 
## 6  3.11  6402  0.72
knitr::opts_chunk$set(echo = FALSE)
## 
## Please cite as:
##  Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.2. https://CRAN.R-project.org/package=stargazer
## 
## Call:
## lm(formula = Y ~ X1 + X2, data = ejemplo_regresion)
## 
## 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
## 
## Ejemplo de Regresión Multiple
## ===============================================
##                         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 βˆ:

##   (Intercept)            X1            X2 
##  1.5644967711  0.2371974748 -0.0002490793

#Matriz de Varianza - Covarianza de los parametros V[β]

##                  (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

##                     2.5 %        97.5 %
## (Intercept)  1.3998395835  1.7291539588
## X1           0.1219744012  0.3524205485
## X2          -0.0003155438 -0.0001826148

#Valores Ajustados Yˆ

##         [,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 ϵˆ

##               [,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
## Parsed with column specification:
## cols(
##   `X1;X2;Y` = col_character()
## )
## # A tibble: 6 x 3
##      X1    X2     Y
##   <dbl> <dbl> <dbl>
## 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.7 
## 6  3.11  6402  0.72
##             X1
##  [1,] 28608.16
##  [2,] 24746.55
##  [3,] 22031.52
##  [4,] 19973.42
##  [5,] 19737.00
##  [6,] 19910.22
##  [7,] 20441.28
##  [8,] 20668.40
##  [9,] 21713.58
## [10,] 21723.84
## [11,] 21945.45
## [12,] 22801.02
## [13,] 23958.36
## [14,] 25220.16
## [15,] 26319.80
## [16,] 28246.55
## [17,] 29820.89
## [18,] 32070.50
## [19,] 34975.71
## [20,] 36355.53
## [21,] 39799.60
## [22,] 39923.86
## [23,] 42167.79
## [24,] 43233.09
## [25,] 44540.04

regresion 2

library(stargazer)
options(scipen = 9999)
modelo_lineal2<-lm(formula = Y~X1+X2+X3,data = ejemplo_regresion)
summary(modelo_lineal2)
## 
## Call:
## lm(formula = Y ~ X1 + X2 + X3, data = ejemplo_regresion)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.083899 -0.037236 -0.000873  0.029121  0.103552 
## 
## Coefficients:
##                 Estimate   Std. Error t value Pr(>|t|)
## (Intercept)  1.795015397  1.423255597   1.261    0.221
## X1           0.192561146  0.280950175   0.685    0.501
## X2          -0.000287021  0.000236164  -1.215    0.238
## X3           0.000007607  0.000046889   0.162    0.873
## 
## Residual standard error: 0.05452 on 21 degrees of freedom
## Multiple R-squared:  0.8655, Adjusted R-squared:  0.8462 
## F-statistic: 45.03 on 3 and 21 DF,  p-value: 0.000000002526
stargazer(modelo_lineal2,title = "Regresion Multiple",type = "text",digits = 8)
## 
## Regresion Multiple
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                  Y             
## -----------------------------------------------
## X1                          0.19256110         
##                            (0.28095020)        
##                                                
## X2                          -0.00028702        
##                            (0.00023616)        
##                                                
## X3                          0.00000761         
##                            (0.00004689)        
##                                                
## Constant                    1.79501500         
##                            (1.42325600)        
##                                                
## -----------------------------------------------
## Observations                    25             
## R2                          0.86546470         
## Adjusted R2                 0.84624530         
## Residual Std. Error    0.05452240 (df = 21)    
## F Statistic         45.03094000*** (df = 3; 21)
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

objetos del modelo

options(scipen = 999)
modelo_lineal$coefficients
##   (Intercept)            X1            X2 
##  1.5644967711  0.2371974748 -0.0002490793
varianza_covarianza<-vcov(modelo_lineal)
print(varianza_covarianza)
##                  (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_lineal2,level = .95)
##                      2.5 %       97.5 %
## (Intercept) -1.16480664772 4.7548374408
## X1          -0.39170672684 0.7768290183
## X2          -0.00077815041 0.0002041080
## X3          -0.00008990443 0.0001051181

valores ajustados

plot(modelo_lineal2$fitted.values,main = "valores ajustados",ylab = "Y",xlab="casos")

modelo_lineal2$fitted.values %>% as.matrix()
##         [,1]
## 1  0.6727915
## 2  0.7108734
## 3  0.6972355
## 4  0.6707524
## 5  0.6831016
## 6  0.7078243
## 7  0.7408788
## 8  0.7602711
## 9  0.7964482
## 10 0.7956652
## 11 0.8005446
## 12 0.8297895
## 13 0.8031953
## 14 0.7997363
## 15 0.7532329
## 16 0.7321937
## 17 0.7025771
## 18 0.6914928
## 19 0.6338989
## 20 0.6121543
## 21 0.5723057
## 22 0.4786753
## 23 0.4382824
## 24 0.3962772
## 25 0.3798022

residuos del modelo

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

modelo_lineal2$residuals %>% matrix()
##                [,1]
##  [1,]  0.0772085032
##  [2,] -0.0008733529
##  [3,] -0.0372355120
##  [4,] -0.0607523795
##  [5,]  0.0168984492
##  [6,]  0.0121757197
##  [7,]  0.0291212040
##  [8,] -0.0202711091
##  [9,]  0.1035517985
## [10,]  0.0243348161
## [11,] -0.0505445755
## [12,] -0.0597895249
## [13,] -0.0231953430
## [14,]  0.0402637023
## [15,]  0.0367671136
## [16,] -0.0321937327
## [17,] -0.0225770767
## [18,]  0.0285071762
## [19,] -0.0838988599
## [20,]  0.0178457225
## [21,] -0.0123056810
## [22,] -0.0686752851
## [23,]  0.0717176216
## [24,]  0.0737228376
## [25,] -0.0598022321