library(wooldridge)
data(hprice1)
head(force(hprice1),n=5)
##   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
library(wooldridge)
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_precio<-lm(formula = price~lotsize+sqrft+bdrms, data=hprice1)
stargazer(modelo_precio,title = "modelo precio",type="text")
## 
## modelo precio
## ===============================================
##                         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
library(fastGraph)
library(wooldridge)
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_precio,~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_precio
## BP = 33.732, df = 9, p-value = 9.953e-05
#Se rechaza H0, por lo tanto, hay evidencia de que la varianza de los residuos no es homocedastica
library(lmtest)
library(stargazer)
u_i<-modelo_precio$residuals
datos_pb<-as.data.frame(cbind(u_i,hprice1))
rg<-lm(I(u_i^2)~lotsize+sqrft+bdrms+I(lotsize^2)+I(sqrft^2)+I(bdrms^2)+lotsize*sqrft+lotsize*bdrms+sqrft*bdrms,data = datos_pb)
sumario<-summary(rg)
n<-nrow(datos_pb)
R_2<-sumario$r.squared
lm_w<-n*R_2
gl=length(terms(rg))
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 prueba White",type = "text",digits = 6)
## 
## Resultados prueba White
## =================================
## lmw       Valor Crítico  P value 
## ---------------------------------
## 33.731660   7.814728    0.0000002
## ---------------------------------
library(fastGraph)
shadeDist(xshade = lm_w,
          ddist = "dchisq",
          parm1 = lm_w,
          lower.tail = FALSE)