Detección de Heterocedasticidad

library(wooldridge)
data(hprice1)
head(force(hprice1),n=6)
##     price assess bdrms lotsize sqrft colonial   lprice  lassess llotsize
## 1 300.000  349.1     4    6126  2438        1 5.703783 5.855359 8.720297
## 2 370.000  351.5     3    9903  2076        1 5.913503 5.862210 9.200593
## 3 191.000  217.7     3    5200  1374        0 5.252274 5.383118 8.556414
## 4 195.000  231.8     3    4600  1448        1 5.273000 5.445875 8.433811
## 5 373.000  319.1     4    6095  2514        1 5.921578 5.765504 8.715224
## 6 466.275  414.5     5    8566  2754        1 6.144775 6.027073 9.055556
##     lsqrft
## 1 7.798934
## 2 7.638198
## 3 7.225482
## 4 7.277938
## 5 7.829630
## 6 7.920810
library(wooldridge)
library(equatiomatic)
data(hprice1)
modelo_lineal <- lm(formula = price ~ lotsize + sqrft + bdrms, data = hprice1)
equatiomatic::extract_eq(modelo_lineal)

\[ \operatorname{price} = \alpha + \beta_{1}(\operatorname{lotsize}) + \beta_{2}(\operatorname{sqrft}) + \beta_{3}(\operatorname{bdrms}) + \epsilon \]

library(stargazer)
stargazer(modelo_lineal,title = "Modelo lineal",type = "html")
Modelo lineal
Dependent variable:
price
lotsize 0.002***
(0.001)
sqrft 0.123***
(0.013)
bdrms 13.853
(9.010)
Constant -21.770
(29.475)
Observations 88
R2 0.672
Adjusted R2 0.661
Residual Std. Error 59.833 (df = 84)
F Statistic 57.460*** (df = 3; 84)
Note: p<0.1; p<0.05; p<0.01

a) Use la libreria lmtest para verificar si su varianza residual es homocedástica a través de la prueba de White (incluya los términos cruzados).

Calculo manual

library(stargazer)
u_i<-modelo_lineal$residuals
data_prueba_white<-as.data.frame(cbind(u_i,hprice1))
regresion_auxiliar <- lm(I(u_i^2) ~ lotsize + sqrft + bdrms + I(lotsize^2) + I(sqrft^2) + I(bdrms^2) + lotsize:sqrft + lotsize:bdrms + sqrft:bdrms, data = data_prueba_white)
sumario<-summary(regresion_auxiliar)
n<-nrow(data_prueba_white)
R_2<-sumario$r.squared
LM_w<-n*R_2
gl <- length(coef(regresion_auxiliar)) - 1 
p_value<-1-pchisq(q = LM_w,df = gl)
VC<-qchisq(p = 0.95,df = gl)
salida_white<-c(LM_w,VC,p_value)
names(salida_white)<-c("LMw","Valor Crítico","p value")
stargazer(salida_white,title = "Resultados de la prueba de White",type = "html",digits = 6)
Resultados de la prueba de White
LMw Valor Crítico p value
33.731660 16.918980 0.000100

Cv < LMw. Se rechaza la H0, por lo tanto no hay evidencia de que la varianza de los residuos es heterocedástica

Utilizando libreria “lmtest”

options(scipen = 999999)
library(lmtest)
prueba_white<-bptest(modelo_lineal,~I(lotsize^2)+I(sqrft^2)+I(bdrms^2)+(lotsize*sqrft)+(lotsize*bdrms)+(sqrft*bdrms),data = hprice1)
print(prueba_white)
## 
##  studentized Breusch-Pagan test
## 
## data:  modelo_lineal
## BP = 33.732, df = 9, p-value = 0.00009953

B) Forma gráfica usando fastGraph

library(fastGraph)

gl<-3+3+3
vc<-qchisq(p=0.95,df=gl)
shadeDist(xshade = prueba_white$statistic,
          ddist = "dchisq",
          parm1 = vc,
          lower.tail = FALSE, col = c("black","green"))