Bárbara M. González
Marzo de 2019
library(dplyr)
library(readr)
library(stargazer)
ej_reg<- read.csv("F:/DESCARGAS/2ejercicio.csv")head(ej_reg,n = 6)## 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
options(scipen = 9999)
mod_lineal<- lm(formula = Y~X1+X2, data = ej_reg)
summary(mod_lineal)##
## Call:
## lm(formula = Y ~ X1 + X2, data = ej_reg)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.084497 -0.038781 -0.003203 0.030250 0.105212
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.57048772 0.08016799 19.590 0.00000000000000205 ***
## X1 0.23936758 0.05609960 4.267 0.000314 ***
## X2 -0.00025112 0.00003236 -7.760 0.00000009750145864 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.05382 on 22 degrees of freedom
## Multiple R-squared: 0.8648, Adjusted R-squared: 0.8525
## F-statistic: 70.34 on 2 and 22 DF, p-value: 0.0000000002768
stargazer(mod_lineal, title= "Ejemplo de Regrsión Multiple", type = "text", digits = 8)##
## Ejemplo de Regrsión Multiple
## ===============================================
## Dependent variable:
## ---------------------------
## Y
## -----------------------------------------------
## X1 0.23936760***
## (0.05609960)
##
## X2 -0.00025112***
## (0.00003236)
##
## Constant 1.57048800***
## (0.08016799)
##
## -----------------------------------------------
## Observations 25
## R2 0.86475760
## Adjusted R2 0.85246280
## Residual Std. Error 0.05382050 (df = 22)
## F Statistic 70.33543000*** (df = 2; 22)
## ===============================================
## Note: *p<0.1; **p<0.05; ***p<0.01
options(scipen = 999)
mod_lineal$coefficients## (Intercept) X1 X2
## 1.5704877207 0.2393675751 -0.0002511161
var_covar <- vcov(mod_lineal)
print(var_covar)## (Intercept) X1 X2
## (Intercept) 0.006426906750 0.00024570589 -0.000001002011939
## X1 0.000245705893 0.00314716508 -0.000001708280355
## X2 -0.000001002012 -0.00000170828 0.000000001047177
confint(object = mod_lineal, level = .95)## 2.5 % 97.5 %
## (Intercept) 1.4042294836 1.7367459577
## X1 0.1230241263 0.3557110240
## X2 -0.0003182269 -0.0001840054
plot(mod_lineal$fitted.values, main = "Valores Ajustados", ylab = "Y", xlab = "casos")mod_lineal$fitted.values %>% as.matrix()## [,1]
## 1 0.6761630
## 2 0.7132035
## 3 0.6987813
## 4 0.6715845
## 5 0.6832534
## 6 0.7072753
## 7 0.7397500
## 8 0.7587496
## 9 0.7947884
## 10 0.7940351
## 11 0.7989560
## 12 0.8280649
## 13 0.8027667
## 14 0.7998502
## 15 0.7546816
## 16 0.7340856
## 17 0.7047857
## 18 0.6928775
## 19 0.6344966
## 20 0.6119607
## 21 0.5690638
## 22 0.4778118
## 23 0.4353364
## 24 0.3929139
## 25 0.3747645
plot(mod_lineal$residuals, main = "Residuos", ylab = "Residuos", xlab = "casos")mod_lineal$residuals %>% as.matrix()## [,1]
## 1 0.073837024
## 2 -0.003203481
## 3 -0.038781320
## 4 -0.061584525
## 5 0.016746646
## 6 0.012724692
## 7 0.030249986
## 8 -0.018749645
## 9 0.105211588
## 10 0.025964937
## 11 -0.048956045
## 12 -0.058064901
## 13 -0.022766749
## 14 0.040149777
## 15 0.035318394
## 16 -0.034085610
## 17 -0.024785744
## 18 0.027122529
## 19 -0.084496567
## 20 0.018039307
## 21 -0.009063793
## 22 -0.067811763
## 23 0.074663628
## 24 0.077086112
## 25 -0.064764474
library(dplyr)
library(readr)
library(stargazer)
ejem_reg <- read_csv("F:/DESCARGAS/Ejercicio_tarea.csv")
print(ejem_reg)## # A tibble: 20 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
## 15 940 229 3.2
## 16 1070 240 2.4
## 17 1160 243 4.9
## 18 1210 247 8.8
## 19 1450 249 10.1
## 20 1220 254 6.7
ejem_reg %>% mutate(X3=X1*X2) %>% select("Y", "X1", "X2", "X3") %>% as.data.frame() -> tabla
head(tabla,n = 6)## Y X1 X2 X3
## 1 320 50 7.4 370.0
## 2 450 53 5.1 270.3
## 3 370 60 4.2 252.0
## 4 470 63 3.9 245.7
## 5 420 69 1.4 96.6
## 6 500 82 2.2 180.4
options(scipen = 9999)
mod_lineal<-lm(formula = Y~X1+X2+X3, data = tabla)
summary(mod_lineal)##
## Call:
## lm(formula = Y ~ X1 + X2 + X3, data = tabla)
##
## Residuals:
## Min 1Q Median 3Q Max
## -108.527 -37.595 -2.745 52.292 102.808
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 303.50401 71.54695 4.242 0.000621 ***
## X1 2.32927 0.47698 4.883 0.000166 ***
## X2 -25.07113 11.48487 -2.183 0.044283 *
## X3 0.28617 0.07681 3.726 0.001840 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 67.68 on 16 degrees of freedom
## Multiple R-squared: 0.9634, Adjusted R-squared: 0.9566
## F-statistic: 140.4 on 3 and 16 DF, p-value: 0.00000000001054
stargazer(mod_lineal, title= "Ejemplo de Regresión Multiple", type = "text", digits = 8)##
## Ejemplo de Regresión Multiple
## ================================================
## Dependent variable:
## ----------------------------
## Y
## ------------------------------------------------
## X1 2.32927500***
## (0.47698220)
##
## X2 -25.07113000**
## (11.48487000)
##
## X3 0.28616860***
## (0.07681293)
##
## Constant 303.50400000***
## (71.54695000)
##
## ------------------------------------------------
## Observations 20
## R2 0.96341370
## Adjusted R2 0.95655370
## Residual Std. Error 67.67775000 (df = 16)
## F Statistic 140.44060000*** (df = 3; 16)
## ================================================
## Note: *p<0.1; **p<0.05; ***p<0.01
options(scipen = 999)
mod_lineal$coefficients## (Intercept) X1 X2 X3
## 303.5040143 2.3292746 -25.0711288 0.2861686
-Matriz de Varianza - Covarianza de los parámetros V[\(\beta\)]:
var_covar <- vcov(mod_lineal)
print(var_covar)## (Intercept) X1 X2 X3
## (Intercept) 5118.96645 -31.10997447 -722.8989902 4.493190281
## X1 -31.10997 0.22751204 4.5755139 -0.033223456
## X2 -722.89899 4.57551391 131.9021598 -0.822206343
## X3 4.49319 -0.03322346 -0.8222063 0.005900226
confint(object = mod_lineal, level = .95)## 2.5 % 97.5 %
## (Intercept) 151.8312499 455.1767786
## X1 1.3181175 3.3404318
## X2 -49.4179582 -0.7242993
## X3 0.1233324 0.4490047
plot(mod_lineal$fitted.values, main = "Valores Ajustados", ylab = "Y", xlab = "casos")mod_lineal$fitted.values %>% as.matrix()## [,1]
## 1 340.3238
## 2 376.4442
## 3 410.0762
## 4 422.7825
## 5 456.7683
## 6 490.9729
## 7 561.2516
## 8 572.4839
## 9 661.8956
## 10 805.2546
## 11 743.9514
## 12 802.6063
## 13 921.3246
## 14 1038.5268
## 15 966.3846
## 16 967.1923
## 17 1087.4101
## 18 1280.2249
## 19 1349.9604
## 20 1214.1649
plot(mod_lineal$residuals, main = "Residuos", ylab = "Residuos", xlab = "casos")mod_lineal$residuals %>% as.matrix()## [,1]
## 1 -20.323767
## 2 73.555820
## 3 -40.076233
## 4 47.217467
## 5 -36.768268
## 6 9.027138
## 7 8.748419
## 8 67.516125
## 9 8.104393
## 10 -25.254613
## 11 -53.951414
## 12 -102.606335
## 13 -11.324647
## 14 -108.526815
## 15 -26.384626
## 16 102.807683
## 17 72.589856
## 18 -70.224936
## 19 100.039646
## 20 5.835106