UNIVERSIDAD DE EL SALVADOR

FACULTAD DE CIENCIAS ECONÓMICAS

ESCUELA DE ECONOMÍA

ACTIVIDAD

GUIA DE TRABAJO 1

ASIGNATURA

ECONOMETRÍA

DOCENTE

MSF.ADEMIR PERÉZ

NOMBRE

JOHAN ELI MENDOZA LEMUS

CARNÉ

ML18043

EJERCICIO 1

Para una empresa se ha estimado un modelo que relaciona las ventas de 200 empresas, con su gasto en tv, periódicos y la interacción entre tv y periódicos

Dicho modelo se encuentra en “modelo_ventas.RData”

1. Calcule las matrices A & P & M

IMPORTACIÓN DE DATOS

load("C:/Users/johan/OneDrive/Escritorio/Guia 1 Archivos/modelo_ventas.RData")

MATRIZ X

mat_x <- model.matrix(modelo_ventas)

MATRIZ XTX

mat_xx <- t(mat_x) %*% mat_x
print(mat_xx)
##             (Intercept)         tv  periodico      radio    tv:radio
## (Intercept)       200.0    6110.80    2804.50    4652.80    164946.5
## tv               6110.8  281096.74   90851.03  164946.55   8479184.7
## periodico        2804.5   90851.03   44743.25   74126.39   2717923.3
## radio            4652.8  164946.55   74126.39  152107.86   5800007.5
## tv:radio       164946.5 8479184.74 2717923.34 5800007.52 311058503.9

MATRIZ A

mat_A <- solve(mat_xx) %*% t(mat_x)

MATRIZ P

mat_P <- mat_x %*% mat_A

MATRIZ M

diag(200) - mat_P -> mat_M

2. Compruebe que los residuos en el objeto “modelo_ventas” son iguales al producto de MY, donde “Y” es la variable endógena en el modelo “ventas”

RESIDUOS DEL MODELO

library(magrittr)
residuos_modelo <-modelo_ventas$residuals %>% as.matrix() %>% round(digits = 2)
head(residuos_modelo,n=5)
##     [,1]
## 1 -15.93
## 2  19.33
## 3  38.02
## 4 -15.43
## 5   5.16

PRODUCTO DE MY

datos_modelo <- modelo_ventas$model
mat_Y <- datos_modelo$ventas
mat_MY <- mat_M %*% mat_Y %>% round(digits = 2)
head(mat_MY,n=5)
##     [,1]
## 1 -15.93
## 2  19.33
## 3  38.02
## 4 -15.43
## 5   5.16

COMPROBACIÓN DE MY = RESIDUOS DEL MODELO

mat_MY == residuos_modelo
##     [,1]
## 1   TRUE
## 2   TRUE
## 3   TRUE
## 4   TRUE
## 5   TRUE
## 6   TRUE
## 7   TRUE
## 8   TRUE
## 9   TRUE
## 10  TRUE
## 11  TRUE
## 12  TRUE
## 13  TRUE
## 14  TRUE
## 15  TRUE
## 16  TRUE
## 17  TRUE
## 18  TRUE
## 19  TRUE
## 20  TRUE
## 21  TRUE
## 22  TRUE
## 23  TRUE
## 24  TRUE
## 25  TRUE
## 26  TRUE
## 27  TRUE
## 28  TRUE
## 29  TRUE
## 30  TRUE
## 31  TRUE
## 32  TRUE
## 33  TRUE
## 34  TRUE
## 35  TRUE
## 36  TRUE
## 37  TRUE
## 38  TRUE
## 39  TRUE
## 40  TRUE
## 41  TRUE
## 42  TRUE
## 43  TRUE
## 44  TRUE
## 45  TRUE
## 46  TRUE
## 47  TRUE
## 48  TRUE
## 49  TRUE
## 50  TRUE
## 51  TRUE
## 52  TRUE
## 53  TRUE
## 54  TRUE
## 55  TRUE
## 56  TRUE
## 57  TRUE
## 58  TRUE
## 59  TRUE
## 60  TRUE
## 61  TRUE
## 62  TRUE
## 63  TRUE
## 64  TRUE
## 65  TRUE
## 66  TRUE
## 67  TRUE
## 68  TRUE
## 69  TRUE
## 70  TRUE
## 71  TRUE
## 72  TRUE
## 73  TRUE
## 74  TRUE
## 75  TRUE
## 76  TRUE
## 77  TRUE
## 78  TRUE
## 79  TRUE
## 80  TRUE
## 81  TRUE
## 82  TRUE
## 83  TRUE
## 84  TRUE
## 85  TRUE
## 86  TRUE
## 87  TRUE
## 88  TRUE
## 89  TRUE
## 90  TRUE
## 91  TRUE
## 92  TRUE
## 93  TRUE
## 94  TRUE
## 95  TRUE
## 96  TRUE
## 97  TRUE
## 98  TRUE
## 99  TRUE
## 100 TRUE
## 101 TRUE
## 102 TRUE
## 103 TRUE
## 104 TRUE
## 105 TRUE
## 106 TRUE
## 107 TRUE
## 108 TRUE
## 109 TRUE
## 110 TRUE
## 111 TRUE
## 112 TRUE
## 113 TRUE
## 114 TRUE
## 115 TRUE
## 116 TRUE
## 117 TRUE
## 118 TRUE
## 119 TRUE
## 120 TRUE
## 121 TRUE
## 122 TRUE
## 123 TRUE
## 124 TRUE
## 125 TRUE
## 126 TRUE
## 127 TRUE
## 128 TRUE
## 129 TRUE
## 130 TRUE
## 131 TRUE
## 132 TRUE
## 133 TRUE
## 134 TRUE
## 135 TRUE
## 136 TRUE
## 137 TRUE
## 138 TRUE
## 139 TRUE
## 140 TRUE
## 141 TRUE
## 142 TRUE
## 143 TRUE
## 144 TRUE
## 145 TRUE
## 146 TRUE
## 147 TRUE
## 148 TRUE
## 149 TRUE
## 150 TRUE
## 151 TRUE
## 152 TRUE
## 153 TRUE
## 154 TRUE
## 155 TRUE
## 156 TRUE
## 157 TRUE
## 158 TRUE
## 159 TRUE
## 160 TRUE
## 161 TRUE
## 162 TRUE
## 163 TRUE
## 164 TRUE
## 165 TRUE
## 166 TRUE
## 167 TRUE
## 168 TRUE
## 169 TRUE
## 170 TRUE
## 171 TRUE
## 172 TRUE
## 173 TRUE
## 174 TRUE
## 175 TRUE
## 176 TRUE
## 177 TRUE
## 178 TRUE
## 179 TRUE
## 180 TRUE
## 181 TRUE
## 182 TRUE
## 183 TRUE
## 184 TRUE
## 185 TRUE
## 186 TRUE
## 187 TRUE
## 188 TRUE
## 189 TRUE
## 190 TRUE
## 191 TRUE
## 192 TRUE
## 193 TRUE
## 194 TRUE
## 195 TRUE
## 196 TRUE
## 197 TRUE
## 198 TRUE
## 199 TRUE
## 200 TRUE

3. Muestre que los autovalores de x’x son positivos (use el comando eigen)

eigen(x = mat_xx,symmetric = TRUE) -> descomp
auto_valores <-descomp$values
print (auto_valores)
## [1] 3.114217e+08 7.025253e+04 4.097346e+04 3.714363e+03 1.277350e+01
print(auto_valores > 0)
## [1] TRUE TRUE TRUE TRUE TRUE

EJERCICIO 2

Para una empresa se desea estimar un modelo que relaciona el tiempo (en minutos) en acomodar cajas en una bodega, en función de la distancia (en metros) y del número de cajas (las cajas son todas iguales)

Los datos se encuentran en “datos_cajas.RData”

1. Estime el modelo propuesto y colóquele el nombre de “modelo_cajas”

IMPOTRACIÓN DE DATOS

load("C:/Users/johan/OneDrive/Escritorio/Guia 1 Archivos/datos_cajas.RData")

CONSTRUCCIÓN DEL MODELO

Variable endógena = Tiempo (en minutos)
library(stargazer)
options(scipen = 999)
modelo_cajas <-lm(formula = Tiempo ~ Distancia + N_cajas, data = datos_cajas)
stargazer(modelo_cajas, title = "MODELO PROPUESTO CAJAS", type = "text", digits = 8)
## 
## MODELO PROPUESTO CAJAS
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                               Tiempo           
## -----------------------------------------------
## Distancia                  0.45592080***       
##                            (0.14676230)        
##                                                
## N_cajas                    0.87720460***       
##                            (0.15303460)        
##                                                
## Constant                    2.31120200         
##                            (5.85730300)        
##                                                
## -----------------------------------------------
## Observations                    15             
## R2                          0.73678910         
## Adjusted R2                 0.69292060         
## Residual Std. Error    3.14079000 (df = 12)    
## F Statistic         16.79540000*** (df = 2; 12)
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

2. Calcule las matrices A, P, M

MATRIZ X

mat_x2 <-model.matrix(modelo_cajas)
print (mat_x2)
##    (Intercept) Distancia N_cajas
## 1            1        30      10
## 2            1        25      15
## 3            1        40      10
## 4            1        18      20
## 5            1        22      25
## 6            1        31      18
## 7            1        26      12
## 8            1        34      14
## 9            1        29      16
## 10           1        37      22
## 11           1        20      24
## 12           1        25      17
## 13           1        27      13
## 14           1        23      30
## 15           1        33      24
## attr(,"assign")
## [1] 0 1 2

MATRIZ XTX

mat_xx2 <- t(mat_x2) %*% mat_x2
print(mat_xx2)
##             (Intercept) Distancia N_cajas
## (Intercept)          15       420     270
## Distancia           420     12308    7347
## N_cajas             270      7347    5364

MATRIZ A

mat_A2 <- solve(mat_xx2) %*% t(mat_x2)
print (mat_A2)
##                        1            2            3            4            5
## (Intercept)  0.459747079  0.505626389 -0.317731768  0.707001469  0.053149816
## Distancia   -0.003015297 -0.009318829  0.018819615 -0.019989342 -0.006641453
## N_cajas     -0.017147338 -0.009890695 -0.007919488 -0.004479623  0.011082085
##                        6            7            8             9          10
## (Intercept) -0.166576988  0.633594572 -0.125532551  0.1260628274 -0.90735239
## Distancia    0.006550474 -0.009903692  0.009409808  0.0003379213  0.02334256
## N_cajas      0.002768355 -0.016090251 -0.003959744 -0.0038254420  0.01780152
##                       11           12           13            14          15
## (Intercept)  0.277217608  0.368482344  0.487274665 -0.3674581822 -0.73350489
## Distancia   -0.011931220 -0.007473259 -0.006797416  0.0001559637  0.01645417
## N_cajas      0.006862401 -0.005142468 -0.012793352  0.0238754370  0.01885861

MATRIZ P

mat_p2 <- mat_x2 %*% mat_A2
print(mat_p2)
##              1            2           3            4           5           6
## 1   0.19781478  0.127154573  0.16766180  0.062524965 -0.03527291 0.057620774
## 2   0.12715457  0.124295239  0.03396629  0.140073563  0.05334477 0.038710181
## 3   0.16766180  0.033966286  0.35585795 -0.137368460 -0.10168744 0.123125512
## 4   0.06252497  0.140073563 -0.13736846  0.257600846  0.15524536 0.006698639
## 5  -0.03527291  0.053344771 -0.10168744  0.155245361  0.18408997 0.046742309
## 6   0.05762077  0.038710181  0.12312551  0.006698639  0.04674231 0.086318088
## 7   0.17558129  0.144648497  0.07654437  0.133523089  0.01345706 0.036955589
## 8   0.11716423  0.050316476  0.21126231 -0.035350897 -0.01751039 0.094896089
## 9   0.09794605  0.077129229  0.10132526  0.055636570  0.03786105 0.067680430
## 10 -0.02906036 -0.056765574  0.20436525 -0.131155907  0.05122193 0.136694350
## 11 -0.01209498  0.081873124 -0.13140718  0.199703669  0.18629079 0.030873007
## 12  0.09285990  0.104513848  0.01812731  0.131114317  0.07550894 0.044246890
## 13  0.15541865  0.125438973  0.08744449  0.109054124  0.01789770 0.046274418
## 14 -0.12402490 -0.005427535 -0.12246527  0.112857904  0.23285894 0.067134558
## 15 -0.05129385 -0.039271650  0.11324781 -0.060157783  0.09995191 0.116029165
##              7           8          9          10          11           12
## 1   0.17558129  0.11716423 0.09794605 -0.02906036 -0.01209498  0.092859897
## 2   0.14464850  0.05031648 0.07712923 -0.05676557  0.08187312  0.104513848
## 3   0.07654437  0.21126231 0.10132526  0.20436525 -0.13140718  0.018127310
## 4   0.13352309 -0.03535090 0.05563657 -0.13115591  0.19970367  0.131114317
## 5   0.01345706 -0.01751039 0.03786105  0.05122193  0.18629079  0.075508940
## 6   0.03695559  0.09489609 0.06768043  0.13669435  0.03087301  0.044246890
## 7   0.18301556  0.07160552 0.08894348 -0.08682757  0.04935470  0.112467995
## 8   0.07160552  0.13896449 0.08399596  0.13551596 -0.03237026  0.042396988
## 9   0.08894348  0.08399596 0.07465547  0.05440619  0.04101064  0.069478345
## 10 -0.08682757  0.13551596 0.05440619  0.34795579 -0.01326471 -0.021162536
## 11  0.04935470 -0.03237026 0.04101064 -0.01326471  0.20329083  0.095597926
## 12  0.11246799  0.04239699 0.06947834 -0.02116254  0.09559793  0.094228911
## 13  0.15702161  0.07705558 0.08545596 -0.04568349  0.04428588  0.099852268
## 14 -0.07689788 -0.02789930 0.01907176  0.16357209  0.20867158  0.042323339
## 15 -0.07939330  0.08995724 0.04540362  0.29018859  0.04818497 -0.001554438
##             13           14           15
## 1   0.15541865 -0.124024902 -0.051293849
## 2   0.12543897 -0.005427535 -0.039271650
## 3   0.08744449 -0.122465266  0.113247813
## 4   0.10905412  0.112857904 -0.060157783
## 5   0.01789770  0.232858944  0.099951911
## 6   0.04627442  0.067134558  0.116029165
## 7   0.15702161 -0.076897883 -0.079393301
## 8   0.07705558 -0.027899299  0.089957240
## 9   0.08545596  0.019071756  0.045403621
## 10 -0.04568349  0.163572088  0.290188586
## 11  0.04428588  0.208671580  0.048184973
## 12  0.09985227  0.042323339 -0.001554438
## 13  0.13743085 -0.052866482 -0.044080529
## 14 -0.05286648  0.352392093  0.210699107
## 15 -0.04408053  0.210699107  0.262089133

MATRIZ M

diag(15) - mat_p2 -> mat_M2
print(mat_M2)
##              1            2           3            4           5            6
## 1   0.80218522 -0.127154573 -0.16766180 -0.062524965  0.03527291 -0.057620774
## 2  -0.12715457  0.875704761 -0.03396629 -0.140073563 -0.05334477 -0.038710181
## 3  -0.16766180 -0.033966286  0.64414205  0.137368460  0.10168744 -0.123125512
## 4  -0.06252497 -0.140073563  0.13736846  0.742399154 -0.15524536 -0.006698639
## 5   0.03527291 -0.053344771  0.10168744 -0.155245361  0.81591003 -0.046742309
## 6  -0.05762077 -0.038710181 -0.12312551 -0.006698639 -0.04674231  0.913681912
## 7  -0.17558129 -0.144648497 -0.07654437 -0.133523089 -0.01345706 -0.036955589
## 8  -0.11716423 -0.050316476 -0.21126231  0.035350897  0.01751039 -0.094896089
## 9  -0.09794605 -0.077129229 -0.10132526 -0.055636570 -0.03786105 -0.067680430
## 10  0.02906036  0.056765574 -0.20436525  0.131155907 -0.05122193 -0.136694350
## 11  0.01209498 -0.081873124  0.13140718 -0.199703669 -0.18629079 -0.030873007
## 12 -0.09285990 -0.104513848 -0.01812731 -0.131114317 -0.07550894 -0.044246890
## 13 -0.15541865 -0.125438973 -0.08744449 -0.109054124 -0.01789770 -0.046274418
## 14  0.12402490  0.005427535  0.12246527 -0.112857904 -0.23285894 -0.067134558
## 15  0.05129385  0.039271650 -0.11324781  0.060157783 -0.09995191 -0.116029165
##              7           8           9          10          11           12
## 1  -0.17558129 -0.11716423 -0.09794605  0.02906036  0.01209498 -0.092859897
## 2  -0.14464850 -0.05031648 -0.07712923  0.05676557 -0.08187312 -0.104513848
## 3  -0.07654437 -0.21126231 -0.10132526 -0.20436525  0.13140718 -0.018127310
## 4  -0.13352309  0.03535090 -0.05563657  0.13115591 -0.19970367 -0.131114317
## 5  -0.01345706  0.01751039 -0.03786105 -0.05122193 -0.18629079 -0.075508940
## 6  -0.03695559 -0.09489609 -0.06768043 -0.13669435 -0.03087301 -0.044246890
## 7   0.81698444 -0.07160552 -0.08894348  0.08682757 -0.04935470 -0.112467995
## 8  -0.07160552  0.86103551 -0.08399596 -0.13551596  0.03237026 -0.042396988
## 9  -0.08894348 -0.08399596  0.92534453 -0.05440619 -0.04101064 -0.069478345
## 10  0.08682757 -0.13551596 -0.05440619  0.65204421  0.01326471  0.021162536
## 11 -0.04935470  0.03237026 -0.04101064  0.01326471  0.79670917 -0.095597926
## 12 -0.11246799 -0.04239699 -0.06947834  0.02116254 -0.09559793  0.905771089
## 13 -0.15702161 -0.07705558 -0.08545596  0.04568349 -0.04428588 -0.099852268
## 14  0.07689788  0.02789930 -0.01907176 -0.16357209 -0.20867158 -0.042323339
## 15  0.07939330 -0.08995724 -0.04540362 -0.29018859 -0.04818497  0.001554438
##             13           14           15
## 1  -0.15541865  0.124024902  0.051293849
## 2  -0.12543897  0.005427535  0.039271650
## 3  -0.08744449  0.122465266 -0.113247813
## 4  -0.10905412 -0.112857904  0.060157783
## 5  -0.01789770 -0.232858944 -0.099951911
## 6  -0.04627442 -0.067134558 -0.116029165
## 7  -0.15702161  0.076897883  0.079393301
## 8  -0.07705558  0.027899299 -0.089957240
## 9  -0.08545596 -0.019071756 -0.045403621
## 10  0.04568349 -0.163572088 -0.290188586
## 11 -0.04428588 -0.208671580 -0.048184973
## 12 -0.09985227 -0.042323339  0.001554438
## 13  0.86256915  0.052866482  0.044080529
## 14  0.05286648  0.647607907 -0.210699107
## 15  0.04408053 -0.210699107  0.737910867

3. Compruebe que los residuos en el objeto “modelo_ventas” son iguales al producto de MY, donde “y” es la variable endógena en el modelo (“Tiempo”)

RESIDUOS DEL MODELO

library (magrittr)
residuos_modelo_cajas <- modelo_cajas$residuals %>% as.matrix() %>% round(digits = 2)
head(residuos_modelo_cajas)
##    [,1]
## 1 -0.76
## 2  0.13
## 3 -0.32
## 4  2.94
## 5 -9.27
## 6  0.77

PRODUCTO DE MY

datos_modelo_cajas <- modelo_cajas$model
mat_Y2 <- datos_modelo_cajas$Tiempo
mat_MY2 <- mat_M2 %*% mat_Y2 %>%  round(digits = 2)
head (mat_MY2)
##    [,1]
## 1 -0.76
## 2  0.13
## 3 -0.32
## 4  2.94
## 5 -9.27
## 6  0.77

COMPROBACIÓN DE MY = RESIDUOS DEL MODELO

mat_MY2 == residuos_modelo_cajas
##    [,1]
## 1  TRUE
## 2  TRUE
## 3  TRUE
## 4  TRUE
## 5  TRUE
## 6  TRUE
## 7  TRUE
## 8  TRUE
## 9  TRUE
## 10 TRUE
## 11 TRUE
## 12 TRUE
## 13 TRUE
## 14 TRUE
## 15 TRUE

4. Muestre que los autovalores de x’x son positivos (use el comando eigen)

eigen(x = mat_xx2,symmetric = TRUE) -> descomp2
auto_valores2 <-descomp2$values
print (auto_valores)
## [1] 311421698.6388     70252.5341     40973.4590      3714.3627        12.7735
print(auto_valores > 0)
## [1] TRUE TRUE TRUE TRUE TRUE

EJERCICIO 3

Para los EEUU se ha estimado un modelo que relaciona el “número de crímenes” en un estado con el “nivel de pobreza” y la cantidad de solteros en el mismo

Dicho modelo se encuentran en “modelo_estimado.Rdata”

1 calcule las matrices A, P, M

IMPORTACIÓN DE DATOS

load("C:/Users/johan/OneDrive/Escritorio/Guia 1 Archivos/modelo_estimado.RData")
options(scipen=9999999)

MATRIZ X

mat_x3 <- model.matrix(modelo_estimado_1)

MATRIZ XXT

mat_xx3 <- t(mat_x3) %*% mat_x3

MATRIZ A

solve(mat_xx3) %*% t(mat_x3) -> mat_A3

MATRIZ P

mat_P3 <- mat_x3 %*% mat_A3

MATRIZ M

diag(51) - mat_P3 -> mat_M3

2. Compruebe que los residuos en el objeto “modelo_estimado” son iguales al producto de MY, donde “y” es la variable endógena en el modelo (“crime”)

library(magrittr)
residuos_modelo_estimado <-modelo_estimado_1$residuals %>% as.matrix() %>% round(digits = 2)
print(residuos_modelo_estimado,)
##       [,1]
## 1  -311.71
## 2   116.80
## 3    45.25
## 4   -34.45
## 5   243.00
## 6  -145.12
## 7    86.13
## 8    88.31
## 9   689.82
## 10 -163.29
## 11   60.90
## 12  126.92
## 13  -19.27
## 14  322.59
## 15  -22.44
## 16  128.18
## 17  -70.82
## 18 -227.95
## 19  287.10
## 20  303.88
## 21 -341.99
## 22 -107.18
## 23  -30.63
## 24  189.45
## 25 -811.14
## 26 -351.77
## 27  102.71
## 28  -23.36
## 29   73.38
## 30  -91.63
## 31  324.03
## 32 -115.85
## 33  113.65
## 34  217.94
## 35 -112.70
## 36   21.38
## 37  -88.91
## 38   99.42
## 39 -102.65
## 40  217.88
## 41  -84.09
## 42  137.78
## 43   48.89
## 44  -67.16
## 45  -39.29
## 46 -415.78
## 47 -145.50
## 48 -183.61
## 49 -138.39
## 50 -232.91
## 51  434.17

PRODUCTO DE MY

datos_modelo_estimado <- modelo_estimado_1$model
mat_Y3 <- datos_modelo_estimado$crime
mat_MY3 <- mat_M3 %*% mat_Y3 %>% round(digits = 2)
print(mat_MY3)
##       [,1]
## 1  -311.71
## 2   116.80
## 3    45.25
## 4   -34.45
## 5   243.00
## 6  -145.12
## 7    86.13
## 8    88.31
## 9   689.82
## 10 -163.29
## 11   60.90
## 12  126.92
## 13  -19.27
## 14  322.59
## 15  -22.44
## 16  128.18
## 17  -70.82
## 18 -227.95
## 19  287.10
## 20  303.88
## 21 -341.99
## 22 -107.18
## 23  -30.63
## 24  189.45
## 25 -811.14
## 26 -351.77
## 27  102.71
## 28  -23.36
## 29   73.38
## 30  -91.63
## 31  324.03
## 32 -115.85
## 33  113.65
## 34  217.94
## 35 -112.70
## 36   21.38
## 37  -88.91
## 38   99.42
## 39 -102.65
## 40  217.88
## 41  -84.09
## 42  137.78
## 43   48.89
## 44  -67.16
## 45  -39.29
## 46 -415.78
## 47 -145.50
## 48 -183.61
## 49 -138.39
## 50 -232.91
## 51  434.17

COMPROBACIÓN DE MY = RESIDUOS DEL MODELO

mat_MY3 == residuos_modelo_estimado
##    [,1]
## 1  TRUE
## 2  TRUE
## 3  TRUE
## 4  TRUE
## 5  TRUE
## 6  TRUE
## 7  TRUE
## 8  TRUE
## 9  TRUE
## 10 TRUE
## 11 TRUE
## 12 TRUE
## 13 TRUE
## 14 TRUE
## 15 TRUE
## 16 TRUE
## 17 TRUE
## 18 TRUE
## 19 TRUE
## 20 TRUE
## 21 TRUE
## 22 TRUE
## 23 TRUE
## 24 TRUE
## 25 TRUE
## 26 TRUE
## 27 TRUE
## 28 TRUE
## 29 TRUE
## 30 TRUE
## 31 TRUE
## 32 TRUE
## 33 TRUE
## 34 TRUE
## 35 TRUE
## 36 TRUE
## 37 TRUE
## 38 TRUE
## 39 TRUE
## 40 TRUE
## 41 TRUE
## 42 TRUE
## 43 TRUE
## 44 TRUE
## 45 TRUE
## 46 TRUE
## 47 TRUE
## 48 TRUE
## 49 TRUE
## 50 TRUE
## 51 TRUE

3. Muestre que los autovalores de x’x son positivos (use el comando eigen)

eigen(x = mat_xx3,symmetric = TRUE) -> descomp3
auto_valores3 <-descomp3$values
print (auto_valores)
## [1] 311421698.6388     70252.5341     40973.4590      3714.3627        12.7735
print(auto_valores > 0)
## [1] TRUE TRUE TRUE TRUE TRUE

EJERCICIO 4

Dentro del archivo “Investiment_Equation.xlsx” se encuentran datos para estimar una función de inversión, para un país, y contiene las siguientes variables:

InvReal=Inversión Real en millones de US$

Trend=tendencia,

Inflation=inflación

PNBr=Producto Nacional Bruto Real en US$,

Interest=Tasa de interés

Se solicita:

A) Estimar la ecuación de inversión, presenta tus resultados en formato APA

IMPORTACIÓN DE ARCHIVOS

library(readxl)
Investiment_Equation <- read_excel("C:/Users/johan/OneDrive/Escritorio/Guia 1 Archivos/Investiment_Equation.xlsx")

ECUACIÓN DE INVERSIÓN

library(stargazer)
ecu_in <- lm(formula = InvReal~Trend+Inflation+PNBr+Interest,data = Investiment_Equation)

stargazer(ecu_in,title = "ECUACIÓN DE INVERSIÓN", type = "text", digits = 8)
## 
## ECUACIÓN DE INVERSIÓN
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                               InvReal          
## -----------------------------------------------
## Trend                     -0.01637845***       
##                            (0.00194114)        
##                                                
## Inflation                   0.00002291         
##                            (0.00133035)        
##                                                
## PNBr                       0.66454700***       
##                            (0.05411848)        
##                                                
## Interest                   -0.24032170*        
##                            (0.12033660)        
##                                                
## Constant                  -0.50335230***       
##                            (0.05423952)        
##                                                
## -----------------------------------------------
## Observations                    15             
## R2                          0.97299900         
## Adjusted R2                 0.96219860         
## Residual Std. Error    0.00662875 (df = 10)    
## F Statistic         90.08923000*** (df = 4; 10)
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

B) Calcule los residuos a través de la matriz M

mat_x4 <- model.matrix(ecu_in)
n4 <- nrow(mat_x4)
m4 <- diag(n4) - mat_x4 %*% solve(t(mat_x4)%*%mat_x4)%*%t(mat_x4)
Investiment_Equation$InvReal -> y4
residuos_inversion <- m4 %*% y4
print(residuos_inversion)
##             [,1]
## 1  -0.0100602233
## 2  -0.0009290882
## 3   0.0029656679
## 4   0.0078576839
## 5   0.0028109133
## 6   0.0006259732
## 7   0.0075909286
## 8  -0.0055352778
## 9  -0.0037254127
## 10  0.0006953129
## 11  0.0019904770
## 12 -0.0001288433
## 13 -0.0101976729
## 14  0.0068712384
## 15 -0.0008316770
COMPROBACIÓN
as.matrix(ecu_in$residuals)
##             [,1]
## 1  -0.0100602233
## 2  -0.0009290882
## 3   0.0029656679
## 4   0.0078576839
## 5   0.0028109133
## 6   0.0006259732
## 7   0.0075909286
## 8  -0.0055352778
## 9  -0.0037254127
## 10  0.0006953129
## 11  0.0019904770
## 12 -0.0001288433
## 13 -0.0101976729
## 14  0.0068712384
## 15 -0.0008316770

C) Calcule un intervalo de confianza del 93% para el impacto del PNBr en la Inversión, e interprételo.

confint(object = ecu_in,parm = "PNBr",level = .93)
##         3.5 %   96.5 %
## PNBr 0.554777 0.774317
INTERPRETACIÓN: por cada cambio de un millón de dólares en el PNB real se esperaría que la inversión real se incrementara en .66 millones de dólares y se esperaría que en el 93% de las ocasiones que se estime la ecuación de inversión, el impacto de un millón en el PNB real se traduzca en un mínimo de de 0.55 millones de dólares en la inversión real hasta un máximo de 0.77 millones de dólares en inversión real

EJERCICIO 5

Dentro del archivo “consumption_equation.RData” se encuentran objetos relacionados a una función de consumo, que se construyó usando las variables:

C=Consumo en millones de US$, Yd=Ingreso disponible, W=Riqueza, I=Tasa de interés

a) Calcule los residuos del modelo

IMPORTACIÓN DE DATOS
library(stargazer)
options(scipen = 99999)
load("C:/Users/johan/OneDrive/Escritorio/Guia 1 Archivos/consumption_equation.RData")
RESIDUOS DEL MODELO
n5<-nrow(P)
m5<- diag(n5) - P
residuo5 <- m5 %*% C
print(residuo5)
##          [,1]
## 1   -5.859103
## 2    2.605057
## 3   45.765735
## 4   31.102448
## 5  -21.037889
## 6    7.008120
## 7   17.859663
## 8   10.705631
## 9   22.002328
## 10  -2.689665
## 11   7.784083
## 12 -13.127696
## 13  17.521565
## 14  17.304695
## 15 -16.308260
## 16  -5.255508
## 17   2.788211
## 18 -16.379339
## 19 -14.327554
## 20  11.749135
## 21 -31.424669
## 22 -23.329596
## 23  22.171806
## 24  -5.040038
## 25 -36.191398
## 26 -25.211753
## 27 -21.411271
## 28   1.410519
## 29 -24.229564
## 30  20.971808
## 31  43.342653
## 32  36.808458
## 33  17.882297
## 34 -33.100273
## 35 -37.819995
## 36 -49.370820
## 37  23.456143
## 38 -25.510341
## 39 -11.960629
## 40  -9.234201
## 41  21.949616
## 42   3.211123
## 43 -14.511436
## 44   3.197576
## 45 -62.396763
## 46 -66.854500
## 47   8.330745
## 48  91.963380
## 49  61.620735
## 50  48.148861
## 51 -10.717721
## 52 -84.069717
## 53 -56.426627
## 54 125.113605

b) Calcule la varianza del error del modelo

SIGMA CUADRADO
#TRES VARIABLES EXPLICATIVAS MÁS LA INTERSECCIÓN 
k <-4 
var_error = (t(residuo5)%*% residuo5)/(n5 - k)
print(var_error)
##          [,1]
## [1,] 1428.746

c) Obtenga la matriz de Var-Cov del modelo

var_error <- as.vector(var_error)
var_cov_5 <- var_error*solve(XX)
print(var_cov_5)
##               (Intercept)             Yd               W             I
## (Intercept) 164.522304918 -0.09333539523  0.009670913575 10.5186890800
## Yd           -0.093335395  0.00018911268 -0.000032769561 -0.0072901023
## W             0.009670914 -0.00003276956  0.000006165749  0.0004193421
## I            10.518689080 -0.00729010228  0.000419342092  5.3203789879

d) Obtenga las estimaciones del Consumo, del modelo propuesto.

C_estima <- P%*%C
print(C_estima)
##         [,1]
## 1   982.2591
## 2   995.4949
## 3   979.5343
## 4  1059.7976
## 5  1128.1379
## 6  1135.3919
## 7  1179.3403
## 8  1211.1944
## 9  1288.3977
## 10 1351.4897
## 11 1374.0159
## 12 1406.1277
## 13 1453.1784
## 14 1493.4953
## 15 1557.5083
## 16 1622.5555
## 17 1681.2118
## 18 1801.1793
## 19 1911.9276
## 20 1994.3509
## 21 2097.6247
## 22 2207.5296
## 23 2242.6282
## 24 2322.5400
## 25 2441.3914
## 26 2575.7118
## 27 2697.3113
## 28 2652.2895
## 29 2735.1296
## 30 2847.9282
## 31 2948.7573
## 32 3087.8915
## 33 3185.3177
## 34 3226.1003
## 35 3273.8200
## 36 3324.8708
## 37 3430.8439
## 38 3666.1103
## 39 3832.8606
## 40 3990.4342
## 41 4091.4504
## 42 4276.2889
## 43 4408.2114
## 44 4471.3024
## 45 4528.9968
## 46 4661.3545
## 47 4740.5693
## 48 4836.1366
## 49 5013.9793
## 50 5189.3511
## 51 5434.6177
## 52 5767.7697
## 53 6024.8266
## 54 6132.6864

EJERCICIO 6

Dentro del archivo “datos_ventas.RData” se encuentran los datos para estimar una función de ventas, para una empresa, y contiene las siguientes variables:

1. ventas= Ventas en milones de US$

2. tv= gasto en publicidad en TV en millones de US$

3. radio= gasto en publicidad en radio en millones de US$.

4. periodico= gasto en publicidad en periodico en millones de US$

Se solicita:

a) Estima la ecuación de ventas, presenta sus resultados en formato APA.

IMPORTACIÓN DE DATOS
library(stargazer)
load("C:/Users/johan/OneDrive/Escritorio/Guia 1 Archivos/datos_ventas.RData")
ecua_ventas <- lm(formula = ventas ~ tv+radio+periodico,data= datos_ventas)
stargazer(ecua_ventas, title ="Modelo de ventas", type = "text")
## 
## Modelo de ventas
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                               ventas           
## -----------------------------------------------
## tv                             0.045           
##                               (0.118)          
##                                                
## radio                        -3.450***         
##                               (0.206)          
##                                                
## periodico                    18.485***         
##                               (0.563)          
##                                                
## Constant                    -33.289***         
##                               (7.172)          
##                                                
## -----------------------------------------------
## Observations                    200            
## R2                             0.847           
## Adjusted R2                    0.844           
## Residual Std. Error      33.875 (df = 196)     
## F Statistic          360.758*** (df = 3; 196)  
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

b) Calcule los residuos a través de la matriz M

#matriz_x
mat_x6 <- model.matrix(ecua_ventas)
mat_xx6 <- t(mat_x6)%*% mat_x6

#Matriz M
n6 <- nrow(mat_x6)
mat_M6 <- diag(n6)-(mat_x6 %*% solve(mat_xx6) %*% t(mat_x6))

#residuos con la matriz M E=M*Y
ventas6 <- ecua_ventas$model
mat_Y6 <- ventas6$ventas
Residuo6 <- mat_M6 %*% mat_Y6
print(Residuo6)
##            [,1]
## 1   -17.8524638
## 2    19.0821552
## 3    33.7931916
## 4   -17.3508987
## 5    10.2572135
## 6    74.2038531
## 7   -15.2465204
## 8   -23.4242974
## 9   -39.6405207
## 10   45.1613878
## 11  -40.6649446
## 12    8.9590198
## 13    5.1392043
## 14  -22.6236420
## 15   -2.4070766
## 16  -23.2129522
## 17   -8.8531474
## 18   -2.2553254
## 19  -36.4998224
## 20   -7.7078500
## 21   12.1068249
## 22   56.1602254
## 23   -4.4128052
## 24   32.1887265
## 25  -41.0755512
## 26   85.5635292
## 27   -0.5813419
## 28   36.0534051
## 29   25.1740669
## 30  -36.8482228
## 31   26.2868044
## 32  -15.4987679
## 33  -43.1444263
## 34   46.2272722
## 35  -42.1231189
## 36  101.1406281
## 37  -18.4643544
## 38    4.6103727
## 39  -19.7868698
## 40   -7.5311456
## 41    4.4398079
## 42    7.6680340
## 43   39.7209714
## 44   29.5192706
## 45  -12.0296830
## 46    9.1587903
## 47  -40.4095721
## 48  -13.3392963
## 49   39.1663986
## 50  -40.4133097
## 51   31.4947034
## 52  -31.1473697
## 53  -26.0085689
## 54  -19.2664951
## 55   21.2230654
## 56  -38.1989298
## 57   33.9900112
## 58   -9.0293035
## 59  -26.4532617
## 60    5.2080674
## 61  -57.0047861
## 62   -7.9150425
## 63   34.6127519
## 64  -21.0722866
## 65  -22.0009918
## 66  -37.5816067
## 67  -26.0583831
## 68  -25.5511822
## 69   15.6898060
## 70  -11.9155347
## 71   -2.0728694
## 72  -38.2242913
## 73   10.3876466
## 74  -22.3937288
## 75   16.7129825
## 76   36.0899293
## 77  -62.1710571
## 78  -11.0250420
## 79   43.4373665
## 80  -28.5252657
## 81  -17.3349979
## 82   58.2041869
## 83  -31.7292205
## 84    2.1957465
## 85   -7.5263700
## 86    6.0297930
## 87  -18.0886178
## 88  -14.5646838
## 89  -32.2092742
## 90   -3.0357398
## 91  -22.9596676
## 92  -69.3639459
## 93    5.2832261
## 94   -3.5250431
## 95  -24.0859349
## 96   -9.1837060
## 97   26.4214991
## 98    3.1212001
## 99   -2.9193041
## 100  -7.6730899
## 101  52.0041912
## 102  10.4210157
## 103  73.7871802
## 104   7.9854131
## 105   6.9306678
## 106 -26.3200156
## 107 -38.1956964
## 108 -37.1411765
## 109 -51.3551775
## 110  15.2312039
## 111  37.1313178
## 112   2.0545188
## 113   1.3651902
## 114  19.5562302
## 115   1.4943123
## 116  -6.1604300
## 117  -4.8525782
## 118 -61.9776211
## 119 -11.2002151
## 120 -15.1234596
## 121 -21.5609770
## 122  -4.7202419
## 123  50.4386227
## 124  -5.7864987
## 125   6.7136905
## 126 -35.9140143
## 127  50.9977671
## 128 -49.5940852
## 129 -34.1058846
## 130 -46.9621445
## 131 140.6245747
## 132  71.7959550
## 133  30.0581974
## 134   4.3121884
## 135   0.7502335
## 136  28.9102154
## 137  17.3960181
## 138  19.5045244
## 139 -12.7465335
## 140 -13.0911168
## 141 -36.7362018
## 142  -9.2131004
## 143  -4.9409642
## 144 -36.2423122
## 145 -31.9387023
## 146 -10.6584048
## 147  54.1763052
## 148 -25.9962973
## 149   8.2843516
## 150 -20.6385139
## 151  62.6625611
## 152 -33.3544363
## 153   3.7729886
## 154 -11.3760631
## 155   5.0805779
## 156  17.9950120
## 157  -7.8494101
## 158  -0.2201702
## 159  35.3022384
## 160 -11.5540752
## 161   0.6591253
## 162  -5.5871655
## 163   7.5462798
## 164  -9.3303245
## 165 -19.0175482
## 166  55.7263685
## 167  32.0402256
## 168  31.6353338
## 169  11.4108832
## 170  76.5904762
## 171 -32.7991015
## 172  -0.2825814
## 173 -19.0264844
## 174   9.3293014
## 175  54.2493038
## 176 -22.1047290
## 177  11.5543472
## 178  12.5352601
## 179  98.7318828
## 180  -0.3195726
## 181   4.3908269
## 182  43.6649823
## 183 -53.0060931
## 184 -18.3203130
## 185  33.8772333
## 186 -24.7792769
## 187 -11.5610805
## 188   2.7811315
## 189  73.1591927
## 190 -31.1749134
## 191  14.6672356
## 192 -37.2279106
## 193 -45.8529068
## 194 -17.4973688
## 195 -14.2672641
## 196 -56.8557034
## 197 -35.2780738
## 198   5.4730833
## 199 -12.5781921
## 200  46.9637691

c) Calcule un intervalo de confianza del 96.8% para el impacto del gasto de publicidad en TV, en las ventas, e interprételo

confint(object = ecua_ventas,parm = "tv", level = .968)
##         1.6 %    98.4 %
## tv -0.2097376 0.2998052

INTERPRETACIÓN: No hay relación entre el gasto en publicidad en TV y las ventas, no se rechaza la hipótesis nula de esta variable