1. Importacion de datos

library(wooldridge)
data(hprice1)
head(force(hprice1))
##     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(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
modelo_heterocedasticidad<-lm(price = ˆα + ˆα1(lotsize) + ˆα2(sqrft) + ˆα3(bdrms) +ε, data = hprice1)
## Warning: In lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
##  extra argument 'price' will be disregarded
stargazer(modelo_heterocedasticidad,title = "Modelo para Ejemplo", type = "text")
## 
## Modelo para Ejemplo
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                 NA             
## -----------------------------------------------
## assess                       1.233***          
##                               (0.097)          
##                                                
## bdrms                          2.652           
##                               (2.118)          
##                                                
## lotsize                       0.00003          
##                              (0.0003)          
##                                                
## sqrft                          0.008           
##                               (0.019)          
##                                                
## colonial                      -0.410           
##                               (3.390)          
##                                                
## lprice                      276.094***         
##                              (10.186)          
##                                                
## lassess                     -396.819***        
##                              (38.182)          
##                                                
## llotsize                       1.755           
##                               (5.791)          
##                                                
## lsqrft                        -3.954           
##                              (41.502)          
##                                                
## Constant                     606.372**         
##                              (246.442)         
##                                                
## -----------------------------------------------
## Observations                    88             
## R2                             0.986           
## Adjusted R2                    0.984           
## Residual Std. Error      12.921 (df = 78)      
## F Statistic           602.196*** (df = 9; 78)  
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01
summary(modelo_heterocedasticidad)
## 
## Call:
## lm(data = hprice1, price = ˆα + ˆα1(lotsize) + ˆα2(sqrft) + 
##     ˆα3(bdrms) + ε)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -41.299  -4.117   0.383   4.547  53.462 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  6.064e+02  2.464e+02   2.461   0.0161 *  
## assess       1.233e+00  9.706e-02  12.704  < 2e-16 ***
## bdrms        2.652e+00  2.118e+00   1.252   0.2143    
## lotsize      3.452e-05  2.524e-04   0.137   0.8916    
## sqrft        8.147e-03  1.859e-02   0.438   0.6624    
## colonial    -4.100e-01  3.390e+00  -0.121   0.9041    
## lprice       2.761e+02  1.019e+01  27.107  < 2e-16 ***
## lassess     -3.968e+02  3.818e+01 -10.393 2.23e-16 ***
## llotsize     1.755e+00  5.791e+00   0.303   0.7627    
## lsqrft      -3.954e+00  4.150e+01  -0.095   0.9243    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 12.92 on 78 degrees of freedom
## Multiple R-squared:  0.9858, Adjusted R-squared:  0.9842 
## F-statistic: 602.2 on 9 and 78 DF,  p-value: < 2.2e-16

2. Verificacion de varianza residual, si es homocedastica o no.

a) Prueba de white con calculos a mano

library(stargazer)
u_i<-modelo_heterocedasticidad$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*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=3+3+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 critico","p value")
stargazer(salida_white,title = "Resultados prueba white", type="text", digits = 6)
## 
## Resultados prueba white
## ================================
## LMw       valor critico p value 
## --------------------------------
## 10.975530   14.067140   0.139690
## --------------------------------

como 0.139690 es > 0.05 no se rechaza la H0 por lo tanto hay evidencia que la varianza de los residuos es homocedastica.

Calculo mediante libreria LMTEST

library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
prueba_white<-bptest(modelo_heterocedasticidad,~I(lotsize^2)+I(sqrft^2)+I(bdrms^2)+lotsize*sqrft*bdrms,data = hprice1)
print(prueba_white)
## 
##  studentized Breusch-Pagan test
## 
## data:  modelo_heterocedasticidad
## BP = 10.976, df = 10, p-value = 0.3594

como 0.3594 es > 0.05 no se rechaza la H0 por lo tanto hay evidencia que la varianza de los residuos es homocedastica.

library(fastGraph)
LM_W<-n*R_2
gl<-3*2+choose(3,2)
vc<-qchisq(p=0.95,df=gl)
shadeDist(xshade = prueba_white$statistic,
          ddist = "dchisq",
          parm1 = VC,
          lower.tail = FALSE, col = c("black","orange"),
          sub=paste("VC:",VC,"White:",LM_w))