EJERCICIO 1
Literal A-1 (Matrices A, P & M)
options(scipen = 999999)
# Cargamos los datos
load("C:/Users/USUARIO/Desktop/UES CICLO 1-2021/Econometria/Practicas en R/Primera Guia/Telegram Desktop/Guia 1 Archivos/modelo_ventas.RData")
# Generando la Matriz X
matriz_x<-model.matrix(modelo_ventas)
# Generando la matriz x'x
matriz_xx<-t(matriz_x)%*%(matriz_x)
# Generando la matriz A
matriz_A<-solve(matriz_xx)%*%t(matriz_x)
matriz_A[1:4,1:4]
## 1 2 3 4
## (Intercept) -0.01128647020 0.01410377973 0.0350639188 0.0004283381
## tv -0.00006704103 0.00003094914 -0.0006120193 -0.0002120642
## periodico 0.00139818182 -0.00190724690 -0.0025468816 0.0002293243
## radio -0.00058002134 0.00064866654 -0.0001093284 -0.0001899710
# Generando la matriz P
matriz_P<-matriz_x%*%matriz_A
matriz_P[1:4,1:4]
## 1 2 3 4
## 1 0.03181459 0.00370346 0.01758786 0.02250872
## 2 0.00370346 0.02460480 0.03447285 0.01212022
## 3 0.01758786 0.03447285 0.06766822 0.02641047
## 4 0.02250872 0.01212022 0.02641047 0.02031981
# Generando la matriz M
n<-nrow(matriz_x)
matriz_M<-diag(n)-matriz_P
matriz_M[1:4,1:4]
## 1 2 3 4
## 1 0.96818541 -0.00370346 -0.01758786 -0.02250872
## 2 -0.00370346 0.97539520 -0.03447285 -0.01212022
## 3 -0.01758786 -0.03447285 0.93233178 -0.02641047
## 4 -0.02250872 -0.01212022 -0.02641047 0.97968019
Literal B-1 (Comparación de residuos)
library(magrittr)
# Residuos del modelo
residuos_modelo_ventas<-modelo_ventas$residuals
# Residuos por matrices
datos_modelo<-modelo_ventas$model
residuos_matrices<-matriz_M%*%datos_modelo$ventas
# Comparativa
cbind(residuos_matrices,residuos_modelo_ventas,
residuos_modelo_ventas-residuos_matrices) %>% round(digits = 2) %>% as.data.frame()->comparacion
names(comparacion)<-c("Por_matrices","En_modelo","Diferencia")
head(comparacion,n=10)
## Por_matrices En_modelo Diferencia
## 1 -15.93 -15.93 0
## 2 19.33 19.33 0
## 3 38.02 38.02 0
## 4 -15.43 -15.43 0
## 5 5.16 5.16 0
## 6 80.22 80.22 0
## 7 -16.35 -16.35 0
## 8 -22.89 -22.89 0
## 9 -34.40 -34.40 0
## 10 46.09 46.09 0
Literal C-1 (Auto_Valores_Positivos)
eigen(x=matriz_xx,symmetric = TRUE)->descomposicion
auto_valores<-descomposicion$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 2
Literal A-2 (Estimación del Modelo)
#Cargamos los datos
load("C:/Users/USUARIO/Desktop/UES CICLO 1-2021/Econometria/Practicas en R/Primera Guia/Telegram Desktop/Guia 1 Archivos/datos_cajas.RData")
#Generando el Modelo
modelo_cajas<-lm(formula=Tiempo~Distancia+N_cajas,data = datos_cajas)
summary(modelo_cajas)
##
## Call:
## lm(formula = Tiempo ~ Distancia + N_cajas, data = datos_cajas)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.2716 -0.5405 0.5212 1.4051 2.9381
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.3112 5.8573 0.395 0.70007
## Distancia 0.4559 0.1468 3.107 0.00908 **
## N_cajas 0.8772 0.1530 5.732 0.0000943 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.141 on 12 degrees of freedom
## Multiple R-squared: 0.7368, Adjusted R-squared: 0.6929
## F-statistic: 16.8 on 2 and 12 DF, p-value: 0.0003325
Literal B-2 (Matrices A, P & M)
#Generando la matriz x
mat_x<-model.matrix(modelo_cajas)
#Generando la matriz x'x
mat_xx<-t(mat_x)%*%(mat_x)
#Generando la matriz A
mat_A<-solve(mat_xx)%*%t(mat_x)
print(mat_A)
## 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
#Generando la matriz P
mat_P<-mat_x%*%mat_A
print(mat_P)
## 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
#Alternativamente, mat_P<-mat_x%*%solve(mat_xx)%*%t(mat_x)
#Generando la matriz M
n<-nrow(mat_x)
mat_M<-diag(n)-mat_P
print(mat_M)
## 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
Literal C-2 (Comparación de Residuos)
library(magrittr)
#Residuos modelo
residuos_modelo_cajas<-modelo_cajas$residuals
#Residuos por matrices
mat_Y<-datos_cajas$Tiempo
residuos_por_matrices<-mat_M%*%mat_Y
#Comparacion
comparacion_model_cajas<-cbind(residuos_modelo_cajas,residuos_por_matrices,residuos_modelo_cajas-residuos_por_matrices) %>% round(digits = 2) %>% as.data.frame()
names(comparacion_model_cajas)<-c("Residuos_modelo","Residuos_matrices","Diferencia")
print(comparacion_model_cajas)
## Residuos_modelo Residuos_matrices Diferencia
## 1 -0.76 -0.76 0
## 2 0.13 0.13 0
## 3 -0.32 -0.32 0
## 4 2.94 2.94 0
## 5 -9.27 -9.27 0
## 6 0.77 0.77 0
## 7 1.31 1.31 0
## 8 -2.09 -2.09 0
## 9 1.43 1.43 0
## 10 0.52 0.52 0
## 11 0.52 0.52 0
## 12 1.38 1.38 0
## 13 -1.02 -1.02 0
## 14 2.89 2.89 0
## 15 1.59 1.59 0
Literal D-2 (Auto_Valores_Positivos)
descomposicion<-eigen(t(mat_x)%*%mat_x)
autovalores<-descomposicion$values
print(autovalores)
## [1] 16976.7781334 709.9345923 0.2872743
print(autovalores>0)
## [1] TRUE TRUE TRUE
EJERCICIO 3
Literaal A-3 (Matrices A, P & M)
# Cargamos los datos
load("C:/Users/USUARIO/Desktop/UES CICLO 1-2021/Econometria/Practicas en R/Primera Guia/Telegram Desktop/Guia 1 Archivos/modelo_estimado.RData")
# Generamos la matriz x
MAT_X<-model.matrix(modelo_estimado_1)
# Generamos la matriz x'x
MAT_XX<-t(MAT_X)%*%MAT_X
# Generamos la matriz A
MAT_A<-solve(MAT_XX)%*%t(MAT_X)
MAT_A[1:3,1:3]
## 1 2 3
## (Intercept) -0.12023796 0.007496216 0.043732382
## poverty -0.01182361 0.003994776 0.008825494
## single 0.02723384 -0.003960021 -0.013241432
# Generando la matriz P
MAT_P<-MAT_X%*%MAT_A
MAT_P[1:4,1:4]
## 1 2 3 4
## 1 0.16161108 -0.01277963 -0.06530809 0.02720791
## 2 -0.01277963 0.03146508 0.04501952 0.02109951
## 3 -0.06530809 0.04501952 0.07855895 0.01942366
## 4 0.02720791 0.02109951 0.01942366 0.02234121
# Generando la matriz M
n<-nrow(MAT_X)
MAT_M<-diag(n)-MAT_P
MAT_M[1:4,1:4]
## 1 2 3 4
## 1 0.83838892 0.01277963 0.06530809 -0.02720791
## 2 0.01277963 0.96853492 -0.04501952 -0.02109951
## 3 0.06530809 -0.04501952 0.92144105 -0.01942366
## 4 -0.02720791 -0.02109951 -0.01942366 0.97765879
Literal B-3 (Comparación de Residuos)
# Residuos por modelo
Residuos_estimados<-resid(modelo_estimado_1)
# Residuos por matrices
Datos_Modelo<-modelo_estimado_1$model
y<-Datos_Modelo$crime
Residuos_MAT<- MAT_M%*%y
# Comparativa
comparativa_residuos<-cbind(Residuos_estimados,Residuos_MAT,Residuos_estimados-Residuos_MAT)%>%round(digits = 2)%>%as.data.frame()
names(comparativa_residuos)<-c("Resid_Model","Resid_Mat","Dif")
head(comparativa_residuos,n=10)
## Resid_Model Resid_Mat Dif
## 1 -311.71 -311.71 0
## 2 116.80 116.80 0
## 3 45.25 45.25 0
## 4 -34.45 -34.45 0
## 5 243.00 243.00 0
## 6 -145.12 -145.12 0
## 7 86.13 86.13 0
## 8 88.31 88.31 0
## 9 689.82 689.82 0
## 10 -163.29 -163.29 0
Literal C-3 (Auto_Valores_Positivos)
Descomposicion<-eigen(MAT_XX)
Auto_Valores<-Descomposicion$values
print(Auto_Valores)
## [1] 17956.580914 279.157317 1.681762
print(Auto_Valores>0)
## [1] TRUE TRUE TRUE
EJERCICIO 4
Literal A-4 (Ecuación de Inversión, formato APA)
#Carga de Datos
library(readxl)
Investiment_Equation <- read_excel("C:/Users/USUARIO/Desktop/UES CICLO 1-2021/Econometria/Practicas en R/Primera Guia/Telegram Desktop/Guia 1 Archivos/Investiment_Equation.xlsx")
#Generación la ecuación de inversión
ecuacion_inversion<-lm(formula = InvReal~Trend+Inflation+PNBr+Interest,data = Investiment_Equation)
library(stargazer)
library(magrittr)
stargazer(ecuacion_inversion,title="Ecuacion de inversion",type = "text")
##
## Ecuacion de inversion
## ===============================================
## Dependent variable:
## ---------------------------
## InvReal
## -----------------------------------------------
## Trend -0.016***
## (0.002)
##
## Inflation 0.00002
## (0.001)
##
## PNBr 0.665***
## (0.054)
##
## Interest -0.240*
## (0.120)
##
## Constant -0.503***
## (0.054)
##
## -----------------------------------------------
## Observations 15
## R2 0.973
## Adjusted R2 0.962
## Residual Std. Error 0.007 (df = 10)
## F Statistic 90.089*** (df = 4; 10)
## ===============================================
## Note: *p<0.1; **p<0.05; ***p<0.01
Literal B-4 (Residuos mediante matriz M)
#Generando la matriz M. Recordemos que M=I-P & P=XA
#Generamos x
Mat_x<-model.matrix(ecuacion_inversion)
# Matriz x'x
Mat_xx<-t(Mat_x)%*%(Mat_x)
# Matriz A
Mat_A<-solve(Mat_xx)%*%t(Mat_x)
#Generamos P
Mat_P<-Mat_x%*%Mat_A
#Habiendo Optenido "P" ahora generamos "M"
n<-nrow(Mat_x)
Mat_M<-diag(n)-Mat_P
#Alternativamente podemos calcular "M" de la siguente manera: Mat_M_Alter<-diag(n)-Mat_x%*%solve(t(Mat_x)%*%Mat_x)%*%t(Mat_x)
#Generando los Residuos
Y<-Investiment_Equation$InvReal
Residuos<-Mat_M%*%Y
print(Residuos)
## [,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
Literal C-4 (Intervalo de confianza del 93% para el inpacto del PNBr en la inversión)
confint(ecuacion_inversion,parm = "PNBr",level = 0.93)
## 3.5 % 96.5 %
## PNBr 0.554777 0.774317
Interpretació: Se tiene la confianza de que en el 93% de las veces en que hechemos a correr el modelo, una variación en el PNBr de 1 millon, ocacionará una variación en el nivél de inversión que ronde entre 0.55 millomes como mínimo y un 0.77 millones como máximo.
EJERCICIO 5
Literal A-5 (Residuos del modelo)
# Carga de datos
load("C:/Users/USUARIO/Desktop/UES CICLO 1-2021/Econometria/Practicas en R/Primera Guia/Telegram Desktop/Guia 1 Archivos/consumption_equation.RData")
# Calculando los residuos
n<-nrow(P)
M<-diag(n)-P
resid<-M%*%C
head(resid,n=20)
## [,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
Literal B-5 (Varianza del error del Modelo)
#sigma cuadrado
k<-4
var_error<-t(resid)%*%(resid)/n-k
# Convertimos var_error de una matriz a un escalar
var_error<-as.vector(var_error)
print(var_error)
## [1] 1318.913
Literal C-5 (Matriz de varianza y covarianza)
var_co_var<-var_error*solve(XX)
print(var_co_var)
## (Intercept) Yd W I
## (Intercept) 151.874861381 -0.08616035509 0.008927474359 9.7100781972
## Yd -0.086160355 0.00017457489 -0.000030250443 -0.0067296849
## W 0.008927474 -0.00003025044 0.000005691765 0.0003871057
## I 9.710078197 -0.00672968492 0.000387105701 4.9113816007
Literal D-5 (calcular las estimaciones)
C_estimada<- P%*%C
Cuadro_C_estimada<-as.data.frame(cbind(C,C_estimada,resid))
names(Cuadro_C_estimada)<-c("C_observado","C_estimada","Residuos")
head(Cuadro_C_estimada,n=20)
## C_observado C_estimada Residuos
## 1 976.4 982.2591 -5.859103
## 2 998.1 995.4949 2.605057
## 3 1025.3 979.5343 45.765735
## 4 1090.9 1059.7976 31.102448
## 5 1107.1 1128.1379 -21.037889
## 6 1142.4 1135.3919 7.008120
## 7 1197.2 1179.3403 17.859663
## 8 1221.9 1211.1944 10.705631
## 9 1310.4 1288.3977 22.002328
## 10 1348.8 1351.4897 -2.689665
## 11 1381.8 1374.0159 7.784083
## 12 1393.0 1406.1277 -13.127696
## 13 1470.7 1453.1784 17.521565
## 14 1510.8 1493.4953 17.304695
## 15 1541.2 1557.5083 -16.308260
## 16 1617.3 1622.5555 -5.255508
## 17 1684.0 1681.2118 2.788211
## 18 1784.8 1801.1793 -16.379339
## 19 1897.6 1911.9276 -14.327554
## 20 2006.1 1994.3509 11.749135
EJERCICIO 6
Literal A-6 (Ecuacion de ventas en APA)
library(stargazer)
library(readxl)
# Cargamos la data
load("C:/Users/USUARIO/Desktop/UES CICLO 1-2021/Econometria/Practicas en R/Primera Guia/Telegram Desktop/Guia 1 Archivos/datos_ventas.RData")
modelo_ventas<-lm(formula = ventas~tv+radio+periodico,data = datos_ventas)
stargazer(modelo_ventas,title = "Ecuacion_de_ventas",type = "text")
##
## Ecuacion
## ===============================================
## 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
Literal B-6: (Residuos mediante matriz M)
# Generando la Matriz M
mat_x_6<-model.matrix(modelo_ventas)
mat_xx_6<-t(mat_x_6)%*%(mat_x_6)
mat_A_6<-solve(mat_xx_6)%*%t(mat_x_6)
mat_P_6<-mat_x_6%*%mat_A_6
n<-nrow(mat_x_6)
mat_M_6<-diag(n)-mat_P_6
# Usando M para calcular los Residuos
Y_6<-datos_ventas$ventas
Resid_6<-mat_M_6%*%Y_6
head(Resid_6,n=10)
## [,1]
## 1 -17.85246
## 2 19.08216
## 3 33.79319
## 4 -17.35090
## 5 10.25721
## 6 74.20385
## 7 -15.24652
## 8 -23.42430
## 9 -39.64052
## 10 45.16139
Literal C=6: (Intervalo de confianza del 96.8% para el impaco del gasto en tv en las ventas)
confint(modelo_ventas,parm ="tv",level = 0.968)
## 1.6 % 98.4 %
## tv -0.2097376 0.2998052
Interpretación: De forma individual el gasto en TV no es significativo para explicar la variación de las ventas, ya que se incluye el “0” dentro del intervalo.