#EJERCICIO 6
Ejercicio 6: Dentro del archivo “datos_ventas.RData” se encuentran los datos para estimar una función deventas, para una empresa, y contiene las siguientes variables:
ventas=Ventas en milones de US\(, tv=gasto en publicidad en TV en millones de US\), radio=gasto en publicidad en radio en millones de US\(. periodico=gasto en publicidad en periodico en millones de US\)
options(scipen = 999999)
load("C:/Users/jacob/Desktop/ROJAS/00 UES/Econometria/Guia 1 Archivos/datos_ventas.RData")
ecuacion_ventas<-lm(formula = ventas ~ tv+radio+periodico, data = datos_ventas)
library(stargazer)
stargazer(ecuacion_ventas,title = "Ecuación de Ventas", type = "text")
##
## Ecuación 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
#Calculamos matriz X
matriz_x<-model.matrix(ecuacion_ventas)
#Calculamos matriz M (M=I-P o M=I-x*(x'x)*t(x))
n<-nrow(matriz_x)
#utilizamos diag para obtener matriz identidad y posteriormente obtener M
matriz_M<-diag(n)-(matriz_x)%*%solve(t(matriz_x)%*%matriz_x)%*%t(matriz_x)
#Asignamos valor a la variable endógena según base de datos "datos_ventas
y<-datos_ventas$ventas
#Obtenemos residuo a través de la matriz M
residuos<-matriz_M%*%y
print(residuos)
## [,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
Comprobación de residuos (extra)
library(magrittr)
residuo_modelo_ventas<-ecuacion_ventas$residuals
residuos<-matriz_M%*%y
cbind(residuos, residuo_modelo_ventas,residuo_modelo_ventas- residuos) %>% round(digits=2) %>%as.data.frame->comparacion
names(comparacion)<-c("Por matrices","En modelo","Dif")
comparacion
## Por matrices En modelo Dif
## 1 -17.85 -17.85 0
## 2 19.08 19.08 0
## 3 33.79 33.79 0
## 4 -17.35 -17.35 0
## 5 10.26 10.26 0
## 6 74.20 74.20 0
## 7 -15.25 -15.25 0
## 8 -23.42 -23.42 0
## 9 -39.64 -39.64 0
## 10 45.16 45.16 0
## 11 -40.66 -40.66 0
## 12 8.96 8.96 0
## 13 5.14 5.14 0
## 14 -22.62 -22.62 0
## 15 -2.41 -2.41 0
## 16 -23.21 -23.21 0
## 17 -8.85 -8.85 0
## 18 -2.26 -2.26 0
## 19 -36.50 -36.50 0
## 20 -7.71 -7.71 0
## 21 12.11 12.11 0
## 22 56.16 56.16 0
## 23 -4.41 -4.41 0
## 24 32.19 32.19 0
## 25 -41.08 -41.08 0
## 26 85.56 85.56 0
## 27 -0.58 -0.58 0
## 28 36.05 36.05 0
## 29 25.17 25.17 0
## 30 -36.85 -36.85 0
## 31 26.29 26.29 0
## 32 -15.50 -15.50 0
## 33 -43.14 -43.14 0
## 34 46.23 46.23 0
## 35 -42.12 -42.12 0
## 36 101.14 101.14 0
## 37 -18.46 -18.46 0
## 38 4.61 4.61 0
## 39 -19.79 -19.79 0
## 40 -7.53 -7.53 0
## 41 4.44 4.44 0
## 42 7.67 7.67 0
## 43 39.72 39.72 0
## 44 29.52 29.52 0
## 45 -12.03 -12.03 0
## 46 9.16 9.16 0
## 47 -40.41 -40.41 0
## 48 -13.34 -13.34 0
## 49 39.17 39.17 0
## 50 -40.41 -40.41 0
## 51 31.49 31.49 0
## 52 -31.15 -31.15 0
## 53 -26.01 -26.01 0
## 54 -19.27 -19.27 0
## 55 21.22 21.22 0
## 56 -38.20 -38.20 0
## 57 33.99 33.99 0
## 58 -9.03 -9.03 0
## 59 -26.45 -26.45 0
## 60 5.21 5.21 0
## 61 -57.00 -57.00 0
## 62 -7.92 -7.92 0
## 63 34.61 34.61 0
## 64 -21.07 -21.07 0
## 65 -22.00 -22.00 0
## 66 -37.58 -37.58 0
## 67 -26.06 -26.06 0
## 68 -25.55 -25.55 0
## 69 15.69 15.69 0
## 70 -11.92 -11.92 0
## 71 -2.07 -2.07 0
## 72 -38.22 -38.22 0
## 73 10.39 10.39 0
## 74 -22.39 -22.39 0
## 75 16.71 16.71 0
## 76 36.09 36.09 0
## 77 -62.17 -62.17 0
## 78 -11.03 -11.03 0
## 79 43.44 43.44 0
## 80 -28.53 -28.53 0
## 81 -17.33 -17.33 0
## 82 58.20 58.20 0
## 83 -31.73 -31.73 0
## 84 2.20 2.20 0
## 85 -7.53 -7.53 0
## 86 6.03 6.03 0
## 87 -18.09 -18.09 0
## 88 -14.56 -14.56 0
## 89 -32.21 -32.21 0
## 90 -3.04 -3.04 0
## 91 -22.96 -22.96 0
## 92 -69.36 -69.36 0
## 93 5.28 5.28 0
## 94 -3.53 -3.53 0
## 95 -24.09 -24.09 0
## 96 -9.18 -9.18 0
## 97 26.42 26.42 0
## 98 3.12 3.12 0
## 99 -2.92 -2.92 0
## 100 -7.67 -7.67 0
## 101 52.00 52.00 0
## 102 10.42 10.42 0
## 103 73.79 73.79 0
## 104 7.99 7.99 0
## 105 6.93 6.93 0
## 106 -26.32 -26.32 0
## 107 -38.20 -38.20 0
## 108 -37.14 -37.14 0
## 109 -51.36 -51.36 0
## 110 15.23 15.23 0
## 111 37.13 37.13 0
## 112 2.05 2.05 0
## 113 1.37 1.37 0
## 114 19.56 19.56 0
## 115 1.49 1.49 0
## 116 -6.16 -6.16 0
## 117 -4.85 -4.85 0
## 118 -61.98 -61.98 0
## 119 -11.20 -11.20 0
## 120 -15.12 -15.12 0
## 121 -21.56 -21.56 0
## 122 -4.72 -4.72 0
## 123 50.44 50.44 0
## 124 -5.79 -5.79 0
## 125 6.71 6.71 0
## 126 -35.91 -35.91 0
## 127 51.00 51.00 0
## 128 -49.59 -49.59 0
## 129 -34.11 -34.11 0
## 130 -46.96 -46.96 0
## 131 140.62 140.62 0
## 132 71.80 71.80 0
## 133 30.06 30.06 0
## 134 4.31 4.31 0
## 135 0.75 0.75 0
## 136 28.91 28.91 0
## 137 17.40 17.40 0
## 138 19.50 19.50 0
## 139 -12.75 -12.75 0
## 140 -13.09 -13.09 0
## 141 -36.74 -36.74 0
## 142 -9.21 -9.21 0
## 143 -4.94 -4.94 0
## 144 -36.24 -36.24 0
## 145 -31.94 -31.94 0
## 146 -10.66 -10.66 0
## 147 54.18 54.18 0
## 148 -26.00 -26.00 0
## 149 8.28 8.28 0
## 150 -20.64 -20.64 0
## 151 62.66 62.66 0
## 152 -33.35 -33.35 0
## 153 3.77 3.77 0
## 154 -11.38 -11.38 0
## 155 5.08 5.08 0
## 156 18.00 18.00 0
## 157 -7.85 -7.85 0
## 158 -0.22 -0.22 0
## 159 35.30 35.30 0
## 160 -11.55 -11.55 0
## 161 0.66 0.66 0
## 162 -5.59 -5.59 0
## 163 7.55 7.55 0
## 164 -9.33 -9.33 0
## 165 -19.02 -19.02 0
## 166 55.73 55.73 0
## 167 32.04 32.04 0
## 168 31.64 31.64 0
## 169 11.41 11.41 0
## 170 76.59 76.59 0
## 171 -32.80 -32.80 0
## 172 -0.28 -0.28 0
## 173 -19.03 -19.03 0
## 174 9.33 9.33 0
## 175 54.25 54.25 0
## 176 -22.10 -22.10 0
## 177 11.55 11.55 0
## 178 12.54 12.54 0
## 179 98.73 98.73 0
## 180 -0.32 -0.32 0
## 181 4.39 4.39 0
## 182 43.66 43.66 0
## 183 -53.01 -53.01 0
## 184 -18.32 -18.32 0
## 185 33.88 33.88 0
## 186 -24.78 -24.78 0
## 187 -11.56 -11.56 0
## 188 2.78 2.78 0
## 189 73.16 73.16 0
## 190 -31.17 -31.17 0
## 191 14.67 14.67 0
## 192 -37.23 -37.23 0
## 193 -45.85 -45.85 0
## 194 -17.50 -17.50 0
## 195 -14.27 -14.27 0
## 196 -56.86 -56.86 0
## 197 -35.28 -35.28 0
## 198 5.47 5.47 0
## 199 -12.58 -12.58 0
## 200 46.96 46.96 0
confint(object = ecuacion_ventas, parm = "tv", level = 0.968)
## 1.6 % 98.4 %
## tv -0.2097376 0.2998052
INTERPRETACIÓN: En el 96.8% de ocasiones que estimásemos el modelo, se esperaría que el impacto de un cambio en la publicidad en TV aumentaría en un millón de dólares las ventas, significando un mínimo de -0.20 millones en publicidad en TV hasta un máximo de 0.77 millones de dólares.