Verifique los supuestos de Heterocedastidad y Autocorrelación para el modelo propuesto.
En caso de encontrar evidencia de violación de los supuestos, planteados en el literal anterior, corrija a través de un estimador HAC apropiado, el modelo propuesto.
library(stargazer)
library(readxl)
ventas_empresa <- read_excel("C:/Users/IRMA/Downloads/ventas_empresa.xlsx")
Modelo_ventas<-lm(formula = V~C+P+M,data = ventas_empresa)
stargazer(Modelo_ventas,title = "Modelo estimado",type = "html")
| Dependent variable: | |
| V | |
| C | 0.923*** |
| (0.223) | |
| P | 0.950*** |
| (0.156) | |
| M | 1.298*** |
| (0.431) | |
| Constant | 107.444*** |
| (18.057) | |
| Observations | 24 |
| R2 | 0.980 |
| Adjusted R2 | 0.977 |
| Residual Std. Error | 9.506 (df = 20) |
| F Statistic | 323.641*** (df = 3; 20) |
| Note: | p<0.1; p<0.05; p<0.01 |
library(stargazer)
Resids<-Modelo_ventas$residuals
Data_reg_aux<-as.data.frame(cbind(Resids,ventas_empresa))
Reg_aux<-lm(formula = I(Resids**2)~C+P+M+I(C**2)+I(P**2)+I(M**2),
data = Data_reg_aux)
Resumen<-summary(Reg_aux)
R_2<-Resumen$r.squared
n<-nrow(Data_reg_aux)
LM_w<-n*R_2
gl<-3+3
VC<-qchisq(p=0.95,df=gl)
P_value<-1-pchisq(q=LM_w,df=gl)
Salida_prueba<-c(LM_w,VC,P_value)
names(Salida_prueba)<-c("LMw","Valor critico","P value")
stargazer(Salida_prueba,title="Prueba de white",type="html",digits=6)
| LMw | Valor critico | P value |
| 4.189630 | 12.591590 | 0.651031 |
library(lmtest)
Prueba_White<-bptest(Modelo_ventas,~I(C**2)+(P**2)+(M**2),data = ventas_empresa)
print(Prueba_White)
##
## studentized Breusch-Pagan test
##
## data: Modelo_ventas
## BP = 0.26019, df = 3, p-value = 0.9673
library(lmtest)
dwtest(Modelo_ventas,alternative = "two.side",iterations = 100)
##
## Durbin-Watson test
##
## data: Modelo_ventas
## DW = 1.2996, p-value = 0.05074
## alternative hypothesis: true autocorrelation is not 0
library(car)
durbinWatsonTest(Modelo_ventas,simulate=TRUE,reps=100)
## lag Autocorrelation D-W Statistic p-value
## 1 0.3013888 1.299572 0.08
## Alternative hypothesis: rho != 0
library(dplyr)
library(tidyr)
library(stargazer)
library(kableExtra)
cbind(Resids,ventas_empresa)%>%
as.data.frame()%>%
mutate(Lag_1=dplyr::lag(Resids,1),
Lag_2=dplyr::lag(Resids,2),
Lag_3=dplyr::lag(Resids,3))%>%
replace_na(list(Lag_1=0,Lag_2=0,Lag_3=0))->Data_prueba_BG
kable(head(Data_prueba_BG,n=6))
| Resids | V | C | P | M | Lag_1 | Lag_2 | Lag_3 |
|---|---|---|---|---|---|---|---|
| 10.673678 | 607 | 197 | 173 | 110 | 0.000000 | 0.000000 | 0.000000 |
| 7.372511 | 590 | 208 | 152 | 107 | 10.673678 | 0.000000 | 0.000000 |
| -2.435532 | 543 | 181 | 150 | 99 | 7.372511 | 10.673678 | 0.000000 |
| -3.322264 | 558 | 194 | 150 | 102 | -2.435532 | 7.372511 | 10.673678 |
| -9.913932 | 571 | 192 | 163 | 109 | -3.322264 | -2.435532 | 7.372511 |
| 8.704039 | 615 | 196 | 179 | 114 | -9.913932 | -3.322264 | -2.435532 |
Reg_aux_BG<-lm(Resids~C+P+M+Lag_1+Lag_2+Lag_3,data=Data_prueba_BG)
Resumen_BG<-summary(Reg_aux_BG)
n<-nrow(ventas_empresa)
gl<-3
R_2_BG<-Resumen_BG$r.squared
LM_BG<-n*R_2_BG
VC<-qchisq(p=0.05,lower.tail = FALSE,df=gl)
P_value_BG<-pchisq(q=LM_BG,lower.tail = FALSE,df=gl)
salida_BG<-c(LM_BG,VC,P_value_BG)
names(salida_BG)<-c("LM BG","Valor critico","Valor P")
stargazer(salida_BG,title="Prueba de BG",type="html",digits = 6)
| LM BG | Valor critico | Valor P |
| 4.453004 | 7.814728 | 0.216521 |
library(lmtest)
prueba_LM1<-bgtest(Modelo_ventas,order = 2)
print(prueba_LM1)
##
## Breusch-Godfrey test for serial correlation of order up to 2
##
## data: Modelo_ventas
## LM test = 3.8409, df = 2, p-value = 0.1465
Se puede concluir que no se encontro violación a los supuestos de Homocedasticidad, pero si a los supuestos de autocorrelación de 1° orden.
options(scipen = 99999)
library(lmtest)
library(sandwich)
estimacion_omega_1<-vcovHC(Modelo_ventas,type = "HC1")
coeftest(Modelo_ventas,vcov. = estimacion_omega_1)
##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 107.44351 14.40813 7.4571 0.0000003392 ***
## C 0.92257 0.17996 5.1265 0.0000514245 ***
## P 0.95018 0.15711 6.0478 0.0000065232 ***
## M 1.29779 0.42106 3.0822 0.005878 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Verifique los supuestos de Heterocedastidad y Autocorrelación para el modelo propuesto.
En caso de encontrar evidencia de violación de los supuestos, planteados en el literal anterior, corrija a través de un estimador HAC apropiado, el modelo propuesto.
library(stargazer)
library(foreign)
load("C:/Users/IRMA/Downloads/wage2.RData")
Modelo_escol<-lm(formula= educ~sibs+meduc+feduc,data=wage2)
stargazer(Modelo_escol,title="Modelo de escolaridad", type="html")
| Dependent variable: | |
| educ | |
| sibs | -0.094*** |
| (0.034) | |
| meduc | 0.131*** |
| (0.033) | |
| feduc | 0.210*** |
| (0.027) | |
| Constant | 10.364*** |
| (0.359) | |
| Observations | 722 |
| R2 | 0.214 |
| Adjusted R2 | 0.211 |
| Residual Std. Error | 1.987 (df = 718) |
| F Statistic | 65.198*** (df = 3; 718) |
| Note: | p<0.1; p<0.05; p<0.01 |
library(lmtest)
prueba_W<-bptest(Modelo_escol,~I(sibs˄2)+I(meduc˄2)+I(feduc˄2),data=wage2)
print(prueba_W)
##
## studentized Breusch-Pagan test
##
## data: Modelo_escol
## BP = 6.9345, df = 3, p-value = 0.07401
library(lmtest)
dwtest(Modelo_escol,alternative = "two.sided",iterations = 100)
##
## Durbin-Watson test
##
## data: Modelo_escol
## DW = 1.8989, p-value = 0.1705
## alternative hypothesis: true autocorrelation is not 0
library(car)
durbinWatsonTest(Modelo_escol,simulate=TRUE,reps=100)
## lag Autocorrelation D-W Statistic p-value
## 1 0.05018452 1.898938 0.18
## Alternative hypothesis: rho != 0
library(lmtest)
bgtest(Modelo_escol,order = 2)
##
## Breusch-Godfrey test for serial correlation of order up to 2
##
## data: Modelo_escol
## LM test = 4.5747, df = 2, p-value = 0.1015
Se puede concluir que no existe ninguna violación a los supuestos, no existe heterocedasticidad ni autocorrelación de 1° y 2° orden.
Verifique los supuestos de Heterocedastidad y Autocorrelación para el modelo propuesto.
En caso de encontrar evidencia de violación de los supuestos, planteados en el literal anterior, corrija a través de un estimador HAC apropiado, el modelo propuesto.
load("C:/Users/IRMA/Downloads/LAWSCH85.RData")
# Estimando el modelo
library(stargazer)
options(scipen = 99999)
modelo_salario<-lm(formula=salary~LSAT+GPA+libvol+cost+rank,data=LAWSCH85)
stargazer(modelo_salario,title= "Modelo de Salario", type="html")
| Dependent variable: | |
| salary | |
| LSAT | 155.286 |
| (192.598) | |
| GPA | 14,500.240*** |
| (4,357.070) | |
| libvol | 12.956*** |
| (3.261) | |
| cost | 0.366** |
| (0.146) | |
| rank | -115.815*** |
| (16.153) | |
| Constant | -33,124.550 |
| (25,210.760) | |
| Observations | 136 |
| R2 | 0.806 |
| Adjusted R2 | 0.798 |
| Residual Std. Error | 5,525.860 (df = 130) |
| F Statistic | 107.948*** (df = 5; 130) |
| Note: | p<0.1; p<0.05; p<0.01 |
library(lmtest)
Prueba_White_3<-bptest(modelo_salario,~I(LSAT˄2)+I(GPA˄2)+I(libvol˄2)+I(cost˄2)+I(rank˄2),data=LAWSCH85)
print(Prueba_White_3)
##
## studentized Breusch-Pagan test
##
## data: modelo_salario
## BP = 19.289, df = 5, p-value = 0.001698
library(lmtest)
dwtest(modelo_salario,alternative = "two.sided",iterations = 100)
##
## Durbin-Watson test
##
## data: modelo_salario
## DW = 1.666, p-value = 0.04431
## alternative hypothesis: true autocorrelation is not 0
library(car)
durbinWatsonTest(modelo_salario,simulate = TRUE,reps = 100)
## lag Autocorrelation D-W Statistic p-value
## 1 0.1645504 1.66604 0.04
## Alternative hypothesis: rho != 0
library(lmtest)
prueba_LM3<-bgtest(modelo_salario,order = 2)
print(prueba_LM3)
##
## Breusch-Godfrey test for serial correlation of order up to 2
##
## data: modelo_salario
## LM test = 4.4579, df = 2, p-value = 0.1076
En los resultados obtenidos se encontro evidencia de heterocedasticidad y autocorreación de 1° orden, la cual se procede a corregir.
options(scipen = 99999)
library(lmtest)
# Sin corregir
coeftest(modelo_salario)
##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -33124.5521 25210.7590 -1.3139 0.191192
## LSAT 155.2859 192.5983 0.8063 0.421561
## GPA 14500.2356 4357.0702 3.3280 0.001138 **
## libvol 12.9562 3.2612 3.9729 0.000117 ***
## cost 0.3658 0.1455 2.5141 0.013154 *
## rank -115.8145 16.1527 -7.1700 0.00000000005053 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
options(scipen = 99999)
library(lmtest)
library(sandwich)
Estimacion_omega<-vcovHC(modelo_salario,type="HC0")
coeftest(modelo_salario,vcov. = Estimacion_omega)
##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -33124.5521 25780.5250 -1.2849 0.2011233
## LSAT 155.2859 200.8346 0.7732 0.4408059
## GPA 14500.2356 4301.1036 3.3713 0.0009856 ***
## libvol 12.9562 4.2300 3.0629 0.0026647 **
## cost 0.3658 0.1473 2.4834 0.0142882 *
## rank -115.8145 14.3892 -8.0487 0.0000000000004596 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
library(lmtest)
library(sandwich)
estimacion_omega_3<-vcovHC(modelo_salario,type = "HC1")
coeftest(modelo_salario,vcov. = estimacion_omega_3)
##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -33124.55206 26368.74950 -1.2562 0.211295
## LSAT 155.28590 205.41694 0.7560 0.451044
## GPA 14500.23557 4399.24024 3.2961 0.001264 **
## libvol 12.95618 4.32651 2.9946 0.003291 **
## cost 0.36580 0.15066 2.4280 0.016553 *
## rank -115.81451 14.71746 -7.8692 0.000000000001219 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1