library(wooldridge)
data(hprice1)
head(force(hprice1),n=5) #mostrar las primeras 5 observaciones
##   price assess bdrms lotsize sqrft colonial   lprice  lassess llotsize   lsqrft
## 1   300  349.1     4    6126  2438        1 5.703783 5.855359 8.720297 7.798934
## 2   370  351.5     3    9903  2076        1 5.913503 5.862210 9.200593 7.638198
## 3   191  217.7     3    5200  1374        0 5.252274 5.383118 8.556414 7.225482
## 4   195  231.8     3    4600  1448        1 5.273000 5.445875 8.433811 7.277938
## 5   373  319.1     4    6095  2514        1 5.921578 5.765504 8.715224 7.829630

Modelo a estimar

library(stargazer)
## 
## Please cite as:
##  Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
options(scipen = 9999)
ModeloHeterocedasticidad <- lm(formula = price~lotsize+sqrft+ bdrms, data = hprice1)
stargazer(ModeloHeterocedasticidad, title = "Modelo Estimado", type = "text", digits = 5)
## 
## Modelo Estimado
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                price           
## -----------------------------------------------
## lotsize                     0.00207***         
##                              (0.00064)         
##                                                
## sqrft                       0.12278***         
##                              (0.01324)         
##                                                
## bdrms                        13.85252          
##                              (9.01015)         
##                                                
## Constant                     -21.77031         
##                             (29.47504)         
##                                                
## -----------------------------------------------
## Observations                    88             
## R2                            0.67236          
## Adjusted R2                   0.66066          
## Residual Std. Error     59.83348 (df = 84)     
## F Statistic          57.46023*** (df = 3; 84)  
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01
library(stargazer)
Ui <- ModeloHeterocedasticidad$residuals
Mat_X <- model.matrix(ModeloHeterocedasticidad)
MatrizX <- Mat_X[,-1]
VectorY <- hprice1$price
Data.PruebaWhite <- as.data.frame(cbind(Ui,VectorY, MatrizX ))
RegresionAuxiliar <- lm(formula = I(Ui^2)~lotsize+sqrft+bdrms+I(lotsize^2) + I(sqrft^2) + I(bdrms^2) + lotsize*sqrft+lotsize*bdrms+ sqrft*bdrms, data = Data.PruebaWhite)
sumario <- summary(RegresionAuxiliar)
n <- nrow(Data.PruebaWhite)
R2 <- sumario$r.squared
LMw <- n*R2
Gl <- 3+3+3
P.Value <- 1-pchisq(q = LMw, df = Gl)
Vc <- qchisq(p = 0.95, df = Gl)
Salida_Whitw <- c(LMw, Vc, P.Value)
names(Salida_Whitw) <- c("LMw", "Valor Critico", "P Value")
stargazer(Salida_Whitw, title = "Resultado de la prueba de White", type = "text", digits = 5)
## 
## Resultado de la prueba de White
## ==============================
## LMw      Valor Critico P Value
## ------------------------------
## 33.73166   16.91898    0.00010
## ------------------------------

#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).

library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
options(scipen = 999999)
Prueba_White <- bptest(ModeloHeterocedasticidad, ~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:  ModeloHeterocedasticidad
## BP = 33.732, df = 9, p-value = 0.00009953

#Presente sus resultados de forma gráfica a través de la librería fastGraph

library(fastGraph)
PW <- Prueba_White$statistic
gl <- Prueba_White$parameter
VC <- qchisq(p = 0.05, df = gl,lower.tail = FALSE)
shadeDist(PW, ddist = "dchisq", 
          parm1 = gl, 
          lower.tail = FALSE, xmin = 0, 
          sub= paste("VC:", round(VC, digits = 3),"  "," ",
                     "LMw:", round(PW,digits = 3)), 
          main = "Prueba White")