library(readr)
library(stargazer)
library(kableExtra)
ejemplo_regresion <- read_csv("C:/Users/melvi/Desktop/Econometria/Datos/ejemplo_regresion.csv")
modelo_regresion<-lm(Y~X1+X2,data = ejemplo_regresion)
kable(head(ejemplo_regresion,n=6))
|
X1
|
X2
|
Y
|
|
3.92
|
7298
|
0.75
|
|
3.61
|
6855
|
0.71
|
|
3.32
|
6636
|
0.66
|
|
3.07
|
6506
|
0.61
|
|
3.06
|
6450
|
0.70
|
|
3.11
|
6402
|
0.72
|
stargazer(modelo_regresion,title = "modelo estimado",type = "html")
modelo estimado
|
|
|
|
Dependent variable:
|
|
|
|
|
|
Y
|
|
|
|
X1
|
0.237***
|
|
|
(0.056)
|
|
|
|
|
X2
|
-0.0002***
|
|
|
(0.00003)
|
|
|
|
|
Constant
|
1.564***
|
|
|
(0.079)
|
|
|
|
|
|
|
Observations
|
25
|
|
R2
|
0.865
|
|
Adjusted R2
|
0.853
|
|
Residual Std. Error
|
0.053 (df = 22)
|
|
F Statistic
|
70.661*** (df = 2; 22)
|
|
|
|
Note:
|
p<0.1; p<0.05; p<0.01
|
Prueba de white.
library(stargazer)
residuos<-modelo_regresion$residuals
data_reg_aux<-as.data.frame(cbind(residuos,ejemplo_regresion))
regresion_auxiliar<-lm(formula = I(residuos^2)~X1+X2+I(X1^2)+I(X2^2)+X1*X2,data = data_reg_aux)
resumen<-summary(regresion_auxiliar)
R_2<-resumen$r.squared
n<-nrow(data_reg_aux)
LM_w<-n*R_2
gl<-2+2+1
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 = "Resultados de la prueba de White",type = "html",digits = 6)
Resultados de la prueba de White
|
|
|
LMw
|
Valor Critico
|
p value
|
|
|
|
3.690182
|
11.070500
|
0.594826
|
|
|
Prueba white library lmtest
library(lmtest)
prueba_white<-bptest(modelo_regresion,~I(X1^2)+I(X2^2)+X1*X2,data = ejemplo_regresion)
print(prueba_white)
##
## studentized Breusch-Pagan test
##
## data: modelo_regresion
## BP = 3.6902, df = 5, p-value = 0.5948
Prueba de D-w lmtest
library(lmtest)
dwtest(modelo_regresion,alternative = "two.sided",iterations = 1000)
##
## Durbin-Watson test
##
## data: modelo_regresion
## DW = 1.9483, p-value = 0.5649
## alternative hypothesis: true autocorrelation is not 0
Prueba D-W con library car
library(car)
durbinWatsonTest(modelo_regresion,simulate = TRUE,reps = 1000)
## lag Autocorrelation D-W Statistic p-value
## 1 -0.04366918 1.948305 0.556
## Alternative hypothesis: rho != 0
library(dplyr)
library(tidyr)
library(stargazer)
library(kableExtra)#esta libreria cambia la letra xd
cbind(residuos,ejemplo_regresion) %>% as.data.frame() %>%
mutate(Lag_1=dplyr::lag(residuos,1),
Lag_2=dplyr::lag(residuos,2)) %>%
replace_na(list(Lag_1=0,Lag_2=0))->data_prueba_BG #Se puede guardar al reves xd
kable(head(data_prueba_BG,10))
|
residuos
|
X1
|
X2
|
Y
|
Lag_1
|
Lag_2
|
|
0.0734697
|
3.92
|
7298
|
0.75
|
0.0000000
|
0.0000000
|
|
-0.0033412
|
3.61
|
6855
|
0.71
|
0.0734697
|
0.0000000
|
|
-0.0391023
|
3.32
|
6636
|
0.66
|
-0.0033412
|
0.0734697
|
|
-0.0621832
|
3.07
|
6506
|
0.61
|
-0.0391023
|
-0.0033412
|
|
0.0162403
|
3.06
|
6450
|
0.70
|
-0.0621832
|
-0.0391023
|
|
0.0124247
|
3.11
|
6402
|
0.72
|
0.0162403
|
-0.0621832
|
|
0.0302362
|
3.21
|
6368
|
0.77
|
0.0124247
|
0.0162403
|
|
-0.0185979
|
3.26
|
6340
|
0.74
|
0.0302362
|
0.0124247
|
|
0.1056922
|
3.42
|
6349
|
0.90
|
-0.0185979
|
0.0302362
|
|
0.0264395
|
3.42
|
6352
|
0.82
|
0.1056922
|
-0.0185979
|
regresion_auxiliar_BG<-lm(residuos~X1+X2+Lag_1+Lag_2,data = data_prueba_BG)
resumen_BG<-summary(regresion_auxiliar_BG)
n<-nrow(ejemplo_regresion)
gl<-2
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)
prueba de BG
|
|
|
LM BG
|
Valor Critico
|
valor p
|
|
|
|
3.305189
|
5.991465
|
0.191552
|
|
|
With library lmtest
library(lmtest)
bgtest(modelo_regresion,order = 2)
##
## Breusch-Godfrey test for serial correlation of order up to 2
##
## data: modelo_regresion
## LM test = 3.3052, df = 2, p-value = 0.1916
bgtest(modelo_regresion,order = 1)
##
## Breusch-Godfrey test for serial correlation of order up to 1
##
## data: modelo_regresion
## LM test = 0.051063, df = 1, p-value = 0.8212