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)
options(scipen= 99999)
modelo_hprice<-lm(formula= price~bdrms+lotsize+sqrft, data = hprice1)
stargazer(modelo_hprice,tittle="ejercicio normalidad", type = "text",digits = 8)
## 
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                price           
## -----------------------------------------------
## bdrms                       13.85252000        
##                            (9.01014500)        
##                                                
## lotsize                    0.00206771***       
##                            (0.00064213)        
##                                                
## sqrft                      0.12277820***       
##                            (0.01323741)        
##                                                
## Constant                   -21.77031000        
##                            (29.47504000)       
##                                                
## -----------------------------------------------
## Observations                    88             
## R2                          0.67236220         
## Adjusted R2                 0.66066090         
## Residual Std. Error    59.83348000 (df = 84)   
## F Statistic         57.46023000*** (df = 3; 84)
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01
## 
## ====================
## ejercicio normalidad
## --------------------

#ajuste de los residuos

library(fitdistrplus)
library(stargazer)
fit_normal<-fitdist(data = modelo_hprice$residuals,distr="norm")
plot(fit_normal)

summary(fit_normal)
## Fitting of the distribution ' norm ' by maximum likelihood 
## Parameters : 
##                       estimate Std. Error
## mean  0.0000000000000005451369   6.231624
## sd   58.4578135693031910591344   4.406423
## Loglikelihood:  -482.8775   AIC:  969.7549   BIC:  974.7096 
## Correlation matrix:
##      mean sd
## mean    1  0
## sd      0  1

#Prueba de JB

options(scipen = 999999)
library(normtest)
library(fastGraph)
gl<-2
jb_vc<-qchisq(p= 0.95, df=gl)
jb_test<-jb.norm.test(modelo_hprice$residuals)
print(jb_test)
## 
##  Jarque-Bera test for normality
## 
## data:  modelo_hprice$residuals
## JB = 32.278, p-value = 0.001
#Mostrando resultados en la grafica
shadeDist(xshade = jb_test$statistic,ddist = "dchisq",parm1 =gl,lower.tail = FALSE,sub=paste("vc",jb_vc,"JB",jb_test$statistic))

#Prueba de Ks

options(scipen = 999999)
library(nortest)
lillie.test(modelo_hprice$residuals)
## 
##  Lilliefors (Kolmogorov-Smirnov) normality test
## 
## data:  modelo_hprice$residuals
## D = 0.075439, p-value = 0.2496

#Prueba de SW

library(fastGraph)
options(scipen = 999999)
mat_x<-model.matrix(modelo_hprice)
m<-ncol(mat_x[,-1])
gl<-m*(m-1)/2
sw_vc<-qchisq(p= 0.95, df=gl)
sw_test<-shapiro.test(modelo_hprice$residuals)
shadeDist(xshade = sw_test$statistic,ddist = "dchisq",parm1 =gl,lower.tail = FALSE,sub=paste("vc",sw_vc,"SW",sw_test$statistic ))