#cargando la base de datos

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

#Estimando el modelo

library(stargazer)
modelo_hprice<-lm(formula = price~lotsize+sqrft+bdrms,data = hprice1)
stargazer(modelo_hprice, type ="text",title ="modelo estimado")
## 
## modelo estimado
## ===============================================
##                         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

#Matiz X´X

library(stargazer)
mat_x<-model.matrix(modelo_hprice)
stargazer(head(mat_x, n=5),type="text")
## 
## =================================
##   (Intercept) lotsize sqrft bdrms
## ---------------------------------
## 1      1       6,126  2,438   4  
## 2      1       9,903  2,076   3  
## 3      1       5,200  1,374   3  
## 4      1       4,600  1,448   3  
## 5      1       6,095  2,514   4  
## ---------------------------------
#formando matriz xx
mat_XX<-t(mat_x)%*%mat_x
stargazer(mat_XX,type = "text")
## 
## ==============================================================
##             (Intercept)    lotsize         sqrft       bdrms  
## --------------------------------------------------------------
## (Intercept)     88         793,748        177,205       314   
## lotsize       793,748   16,165,159,010 1,692,290,257 2,933,767
## sqrft         177,205   1,692,290,257   385,820,561   654,755 
## bdrms           314       2,933,767       654,755      1,182  
## --------------------------------------------------------------

#Verificando la independencia de los regresores indice de condicion

options(scipen=999999)
library(mctest)
library(stargazer)
eigprop(modelo_hprice)
## 
## Call:
## eigprop(mod = modelo_hprice)
## 
##   Eigenvalues      CI (Intercept) lotsize  sqrft  bdrms
## 1      3.4816  1.0000      0.0037  0.0278 0.0042 0.0029
## 2      0.4552  2.7656      0.0068  0.9671 0.0061 0.0051
## 3      0.0385  9.5082      0.4726  0.0051 0.8161 0.0169
## 4      0.0247 11.8678      0.5170  0.0000 0.1737 0.9750
## 
## ===============================
## Row 2==> lotsize, proportion 0.967080 >= 0.50 
## Row 3==> sqrft, proportion 0.816079 >= 0.50 
## Row 4==> bdrms, proportion 0.975026 >= 0.50

Prueba FG

options(scipen = 999999)
library(psych)
library(stargazer)
library(fastGraph)
prueb_FG<-cortest.bartlett(mat_x[,-1])
prueba_FG<-t(prueb_FG)
stargazer(prueba_FG,title = "Resultados de Prueba FG",
          type = "text")
## 
## Resultados de Prueba FG
## ===========================================
## chisq                    p.value         df
## -------------------------------------------
## 31.3812164469326 0.000000706580566587692 3 
## -------------------------------------------
#Mostrando resultado es grafica
gl<-3
fg_vc<-qchisq(p=0.96,df=gl)
shadeDist(xshade = prueb_FG$chisq,ddist = "dchisq",parm1 = prueb_FG$df,lower.tail = FALSE,sub = paste("VC",fg_vc,"FG",prueb_FG$chisq))

#Factores inflacionarios de la varianza

options(scipen = 999999)
library(car)
library(mctest)
vif<-vif(modelo_hprice)
print(vif)
##  lotsize    sqrft    bdrms 
## 1.037211 1.418654 1.396663
mc.plot(mod = modelo_hprice,vif=2)