Pruebas de Normalidad 2

Importacion de Datos

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 Estimado

\[ \text{price} = \hat{\alpha} + \hat{\alpha}_1(\text{lotsize}) + \hat{\alpha}_2(\text{sqrft}) + \hat{\alpha}_3(\text{bdrms}) + \epsilon \]

library(stargazer)
modelo_estimado <- lm(price ~ lotsize + sqrft + bdrms, data = hprice1)
stargazer(modelo_estimado,title = "Modelo para ejemplo",type = "html")
Modelo para ejemplo
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

Ajuste de los Residuos a la Distribucion Normal

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

summary(fit_normal)
## Fitting of the distribution ' norm ' by maximum likelihood 
## Parameters : 
##           estimate Std. Error
## mean -2.321494e-15   6.231625
## sd    5.845781e+01   4.406423
## Loglikelihood:  -482.8775   AIC:  969.7549   BIC:  974.7096 
## Correlation matrix:
##      mean sd
## mean    1  0
## sd      0  1

1. Prueba Jaque Bera (JB)

Usando tseries

library(tseries)
salida_JB <- jarque.bera.test(modelo_estimado$residuals)
print(salida_JB)
## 
##  Jarque Bera Test
## 
## data:  modelo_estimado$residuals
## X-squared = 32.278, df = 2, p-value = 9.794e-08
library(fastGraph)
alpha_sign <- 0.05
JB <- salida_JB$statistic
GL <- salida_JB$parameter
VC <- qchisq(1-alpha_sign,GL,lower.tail = TRUE)
shadeDist(JB,ddist = "dchisq",
          parm1 = GL,
          lower.tail = FALSE,xmin = 0,
          sub=paste("VC:",round(VC,2)," ","JB:",round(JB,2)))

Usando notmtest (descatalogada)

library(normtest)
jb.norm.test(modelo_estimado$residuals)
## 
##  Jarque-Bera test for normality
## 
## data:  modelo_estimado$residuals
## JB = 32.278, p-value = 5e-04

2. Prueba de Kolmogorov Smirnov - Lilliefors

Calculo manual

library(dplyr)  
library(gt)  
library(gtExtras)
residuos<-modelo_estimado$residuals
residuos %>%
  as_tibble() %>%
  mutate(posicion=row_number()) %>%
  arrange(value) %>%
  mutate(dist1=row_number()/n()) %>%
  mutate(dist2=(row_number()-1)/n()) %>%
  mutate(zi=as.vector(scale(value,center=TRUE))) %>%
  mutate(pi=pnorm(zi,lower.tail = TRUE)) %>%
  mutate(dif1=abs(dist1-pi)) %>%
  mutate(dif2=abs(dist2-pi)) %>%
  rename(residuales=value) -> tabla_KS

#Formato
 tabla_KS %>%
  gt() %>%
  tab_header("Tabla para calcular el Estadistico KS") %>%
  tab_source_note(source_note = "Fuente: Elaboración propia") %>%
  tab_style(
    style = list(
      cell_fill(color = "#A569BD"),
      cell_text(style = "italic")
      ),
    locations = cells_body(
      columns = dif1,
      rows = dif1==max(dif1)
    )) %>%
   tab_style(
    style = list(
      cell_fill(color = "#3498DB"),
      cell_text(style = "italic")
      ),
    locations = cells_body(
      columns = dif2,
      rows = dif2==max(dif2)
    ))
Tabla para calcular el Estadistico KS
residuales posicion dist1 dist2 zi pi dif1 dif2
-120.026447 81 0.01136364 0.00000000 -2.041515459 0.02059981 0.0092361731 0.0205998094
-115.508697 77 0.02272727 0.01136364 -1.964673586 0.02472601 0.0019987418 0.0133623781
-107.080889 24 0.03409091 0.02272727 -1.821326006 0.03427866 0.0001877487 0.0115513850
-91.243980 48 0.04545455 0.03409091 -1.551957925 0.06033615 0.0148816002 0.0262452366
-85.461169 12 0.05681818 0.04545455 -1.453598781 0.07302879 0.0162106057 0.0275742421
-77.172687 32 0.06818182 0.05681818 -1.312620980 0.09465535 0.0264735301 0.0378371665
-74.702719 54 0.07954545 0.06818182 -1.270609602 0.10193378 0.0223883300 0.0337519664
-65.502849 39 0.09090909 0.07954545 -1.114130117 0.13261169 0.0417025941 0.0530662305
-63.699108 69 0.10227273 0.09090909 -1.083450505 0.13930425 0.0370315271 0.0483951634
-62.566594 83 0.11363636 0.10227273 -1.064187703 0.14362184 0.0299854747 0.0413491110
-59.845223 36 0.12500000 0.11363636 -1.017900230 0.15436269 0.0293626861 0.0407263225
-54.466158 13 0.13636364 0.12500000 -0.926408352 0.17711690 0.0407532663 0.0521169027
-54.300415 14 0.14772727 0.13636364 -0.923589260 0.17785010 0.0301228311 0.0414864675
-52.129801 15 0.15909091 0.14772727 -0.886669532 0.18762842 0.0285375141 0.0399011505
-51.441108 17 0.17045455 0.15909091 -0.874955638 0.19079902 0.0203444766 0.0317081129
-48.704980 47 0.18181818 0.17045455 -0.828417174 0.20371714 0.0218989601 0.0332625965
-48.350295 29 0.19318182 0.18181818 -0.822384375 0.20542908 0.0122472664 0.0236109028
-47.855859 11 0.20454545 0.19318182 -0.813974573 0.20782976 0.0032843043 0.0146479407
-45.639765 1 0.21590909 0.20454545 -0.776281294 0.21879146 0.0028823668 0.0142460032
-43.142550 9 0.22727273 0.21590909 -0.733806463 0.23153335 0.0042606233 0.0156242596
-41.749618 57 0.23863636 0.22727273 -0.710114247 0.23881665 0.0001802823 0.0115439187
-40.869022 27 0.25000000 0.23863636 -0.695136302 0.24348494 0.0065150566 0.0048485798
-37.749811 34 0.26136364 0.25000000 -0.642082009 0.26040997 0.0009536682 0.0104099682
-36.663785 71 0.27272727 0.26136364 -0.623609925 0.26644190 0.0062853771 0.0050782592
-36.646568 79 0.28409091 0.27272727 -0.623317083 0.26653809 0.0175528221 0.0061891857
-33.801248 37 0.29545455 0.28409091 -0.574921384 0.28267223 0.0127823120 0.0014186757
-29.766931 16 0.30681818 0.29545455 -0.506302171 0.30632227 0.0004959124 0.0108677240
-26.696234 22 0.31818182 0.30681818 -0.454073044 0.32488813 0.0067063089 0.0180699452
-24.271531 23 0.32954545 0.31818182 -0.412831567 0.33986501 0.0103195566 0.0216831929
-23.651448 86 0.34090909 0.32954545 -0.402284648 0.34373728 0.0028281851 0.0141918214
-19.683427 88 0.35227273 0.34090909 -0.334793052 0.36889060 0.0166178738 0.0279815102
-17.817835 10 0.36363636 0.35227273 -0.303061413 0.38092153 0.0172851663 0.0286488027
-16.762094 60 0.37500000 0.36363636 -0.285104441 0.38778206 0.0127820638 0.0241457002
-16.596960 21 0.38636364 0.37500000 -0.282295711 0.38885839 0.0024947507 0.0138583870
-16.271207 58 0.39772727 0.38636364 -0.276755010 0.39098411 0.0067431583 0.0046204781
-13.815798 56 0.40909091 0.39772727 -0.234991254 0.40710776 0.0019831485 0.0093804879
-13.462160 75 0.42045455 0.40909091 -0.228976273 0.40944368 0.0110108666 0.0003527698
-12.081520 4 0.43181818 0.42045455 -0.205493119 0.41859344 0.0132247451 0.0018611087
-11.629207 51 0.44318182 0.43181818 -0.197799788 0.42160086 0.0215809622 0.0102173258
-11.312669 74 0.45454545 0.44318182 -0.192415834 0.42370825 0.0308372092 0.0194735728
-8.236558 3 0.46590909 0.45454545 -0.140094626 0.44429261 0.0216164775 0.0102528411
-7.662789 70 0.47727273 0.46590909 -0.130335452 0.44815052 0.0291222111 0.0177585748
-6.752801 67 0.48863636 0.47727273 -0.114857588 0.45427900 0.0343573625 0.0229937262
-6.707262 31 0.50000000 0.48863636 -0.114083016 0.45458599 0.0454140074 0.0340503710
-6.402439 85 0.51136364 0.50000000 -0.108898313 0.45664157 0.0547220642 0.0433584278
-5.446904 82 0.52272727 0.51136364 -0.092645733 0.46309251 0.0596347676 0.0482711313
-3.537785 43 0.53409091 0.52272727 -0.060173762 0.47600862 0.0580822876 0.0467186512
-2.824941 61 0.54545455 0.53409091 -0.048049090 0.48083856 0.0646159857 0.0532523493
-2.745208 68 0.55681818 0.54545455 -0.046692922 0.48137899 0.0754391961 0.0640755598
-0.195089 65 0.56818182 0.55681818 -0.003318245 0.49867621 0.0695056040 0.0581419676
1.399296 55 0.57954545 0.56818182 0.023800450 0.50949411 0.0700513452 0.0586877088
5.363331 26 0.59090909 0.57954545 0.091224254 0.53634280 0.0545662924 0.0432026561
6.700640 53 0.60227273 0.59090909 0.113970383 0.54536936 0.0569033628 0.0455397265
7.386314 80 0.61363636 0.60227273 0.125632935 0.54998875 0.0636476093 0.0522839730
9.099900 41 0.62500000 0.61363636 0.154779103 0.56150227 0.0634977329 0.0521340965
12.433611 46 0.63636364 0.62500000 0.211481796 0.58374433 0.0526193043 0.0412556680
16.718018 62 0.64772727 0.63636364 0.284354766 0.61193074 0.0357965328 0.0244328965
18.093192 5 0.65909091 0.64772727 0.307744934 0.62086179 0.0382291219 0.0268654856
18.801816 38 0.67045455 0.65909091 0.319797835 0.62543921 0.0450153400 0.0336517036
19.168108 33 0.68181818 0.67045455 0.326028052 0.62779843 0.0540197476 0.0426561112
19.219211 72 0.69318182 0.68181818 0.326897255 0.62812720 0.0650546167 0.0536909803
20.334434 59 0.70454545 0.69318182 0.345865960 0.63527827 0.0692671805 0.0579035442
24.909926 78 0.71590909 0.70454545 0.423689939 0.66410402 0.0518050676 0.0404414312
26.236229 40 0.72727273 0.71590909 0.446248874 0.67229126 0.0549814685 0.0436178321
30.924022 25 0.73863636 0.72727273 0.525982978 0.70054998 0.0380863808 0.0267227444
32.253952 45 0.75000000 0.73863636 0.548603608 0.70836125 0.0416387548 0.0302751184
32.529367 49 0.76136364 0.75000000 0.553288104 0.70996693 0.0513967091 0.0400330727
32.675968 18 0.77272727 0.76136364 0.555781630 0.71081993 0.0619073452 0.0505437088
33.275839 20 0.78409091 0.77272727 0.565984762 0.71429793 0.0697929786 0.0584293423
36.031430 52 0.79545455 0.78409091 0.612854281 0.73001365 0.0654408934 0.0540772571
37.147186 84 0.80681818 0.79545455 0.631832029 0.73625168 0.0705665028 0.0592028664
40.320875 7 0.81818182 0.80681818 0.685812928 0.75358446 0.0645973596 0.0532337232
44.334467 30 0.82954545 0.81818182 0.754079634 0.77459930 0.0549461574 0.0435825211
46.907165 28 0.84090909 0.82954545 0.797838357 0.78751785 0.0533912405 0.0420276041
54.418366 87 0.85227273 0.84090909 0.925595465 0.82267187 0.0296008528 0.0182372164
55.091131 35 0.86363636 0.85227273 0.937038450 0.82563061 0.0380057535 0.0266421172
55.470305 44 0.87500000 0.86363636 0.943487765 0.82728426 0.0477157353 0.0363520989
62.939597 6 0.88636364 0.87500000 1.070532059 0.85781006 0.0285535797 0.0171899433
66.478628 50 0.89772727 0.88636364 1.130727018 0.87091500 0.0268122757 0.0154486394
67.426518 63 0.90909091 0.89772727 1.146849569 0.87427810 0.0348128083 0.0234491719
67.603959 19 0.92045455 0.90909091 1.149867648 0.87490081 0.0455537393 0.0341901029
69.707122 64 0.93181818 0.92045455 1.185640095 0.88211777 0.0497004123 0.0383367759
69.843246 8 0.94318182 0.93181818 1.187955411 0.88257451 0.0606073068 0.0492436705
74.848732 2 0.95454545 0.94318182 1.273093116 0.89850750 0.0560379553 0.0446743189
112.729191 66 0.96590909 0.95454545 1.917397313 0.97240626 0.0064971714 0.0178608078
163.795081 73 0.97727273 0.96590909 2.785970904 0.99733162 0.0200588896 0.0314225260
198.660139 42 0.98863636 0.97727273 3.378986513 0.99963623 0.0109998685 0.0223635048
209.375830 76 1.00000000 0.98863636 3.561248407 0.99981545 0.0001845478 0.0111790885
Fuente: Elaboración propia

Calculo del estadistico D

D<-max(max(tabla_KS$dif1),max(tabla_KS$dif2))
print(D)
## [1] 0.0754392
library(gt)
data.frame(
  n = c("50", ">100*"), 
  α_0.20 = c(0.086, 0.061),
  α_0.05 = c(0.115, 0.081),
  Comparación = c("0.0754 < 0.115", "0.0754 < 0.081")
) %>% 
  gt() %>% 
  tab_footnote("*Valores para n=88")
n α_0.20 α_0.05 Comparación
50 0.086 0.115 0.0754 < 0.115
>100* 0.061 0.081 0.0754 < 0.081
*Valores para n=88

Conclusion

En este caso dado que 0.0754 < 0.081 No se rechaza \(H_0: \epsilon \sim N(0,\sigma^2)\) por lo que los residuos siguen una distribución normal.

Usando nortest

library(nortest)
prueba_KS <- lillie.test(modelo_estimado$residuals)
print(prueba_KS)
## 
##  Lilliefors (Kolmogorov-Smirnov) normality test
## 
## data:  modelo_estimado$residuals
## D = 0.075439, p-value = 0.2496
p.value <- prueba_KS$p.value

En este caso dado que 0.2496 > 0.05 No se rechaza la Hipótesis Nula: \(H_0: \epsilon \sim N(0,\sigma^2)\) , por lo que los residuos siguen una distribución normal.

Prueba de shapiro - Wilk

calculo manual

library(dplyr)
library(gt)
residuos<-modelo_estimado$residuals
residuos %>%  
  as_tibble() %>%
  rename(residuales=value) %>%
  arrange(residuales) %>%
  mutate(pi=(row_number()-0.375)/(n()+0.25)) %>%
  mutate(mi=qnorm(pi,lower.tail = TRUE)) %>% 
  mutate(ai=0)->tabla_SW

m<-sum(tabla_SW$mi^2)
n <- nrow(residuos)
print(class(residuos))
## [1] "numeric"
print(n)
## NULL
residuos <- as.data.frame(residuos)
n <- nrow(residuos)
theta<-1/sqrt(n)
tabla_SW$ai[n]<- -2.706056*theta^5+4.434685*theta^4-2.071190*theta^3-0.147981*theta^2+0.2211570*theta+tabla_SW$mi[n]/sqrt(m)
tabla_SW$ai[n-1]<- -3.582633*theta^5+5.682633*theta^4-1.752461*theta^3-0.293762*theta^2+0.042981*theta+tabla_SW$mi[n-1]/sqrt(m)
tabla_SW$ai[1]<- -tabla_SW$ai[n]
tabla_SW$ai[2]<- -tabla_SW$ai[n-1]
omega<-(m-2*tabla_SW$mi[n]^2-2*tabla_SW$mi[n-1]^2)/(1-2*tabla_SW$ai[n]^2-2*tabla_SW$ai[n-1]^2)
tabla_SW$ai[3:(n-2)]<-tabla_SW$mi[3:(n-2)]/sqrt(omega)

tabla_SW %>% 
  mutate(ai_ui=ai*residuales,ui2=residuales^2) ->tabla_SW

tabla_SW %>%
  gt() %>% tab_header("Tabla para calcular el Estadistico W") %>% 
  tab_source_note(source_note = "Fuente: Elaboración propia")
Tabla para calcular el Estadistico W
residuales pi mi ai ai_ui ui2
-120.026447 0.007082153 -2.45306927 -0.286093929 34.338837782 1.440635e+04
-115.508697 0.018413598 -2.08767462 -0.226331231 26.143225495 1.334226e+04
-107.080889 0.029745042 -1.88455395 -0.201511408 21.578020632 1.146632e+04
-91.243980 0.041076487 -1.73832835 -0.185875811 16.960048752 8.325464e+03
-85.461169 0.052407932 -1.62194155 -0.173430814 14.821600075 7.303611e+03
-77.172687 0.063739377 -1.52411994 -0.162970954 12.576906330 5.955624e+03
-74.702719 0.075070822 -1.43903134 -0.153872609 11.494702279 5.580496e+03
-65.502849 0.086402266 -1.36324747 -0.145769197 9.548297773 4.290623e+03
-63.699108 0.097733711 -1.29457343 -0.138426027 8.817614500 4.057576e+03
-62.566594 0.109065156 -1.23151500 -0.131683320 8.238976839 3.914579e+03
-59.845223 0.120396601 -1.17300649 -0.125427129 7.506214499 3.581451e+03
-54.466158 0.131728045 -1.11825971 -0.119573169 6.512691096 2.966562e+03
-54.300415 0.143059490 -1.06667420 -0.114057239 6.193355472 2.948535e+03
-52.129801 0.154390935 -1.01778137 -0.108829231 5.673246083 2.717516e+03
-51.441108 0.165722380 -0.97120790 -0.103849228 5.342119306 2.646188e+03
-48.704980 0.177053824 -0.92665123 -0.099084876 4.825926905 2.372175e+03
-48.350295 0.188385269 -0.88386232 -0.094509548 4.569564512 2.337751e+03
-47.855859 0.199716714 -0.84263354 -0.090101040 4.311862673 2.290183e+03
-45.639765 0.211048159 -0.80278966 -0.085840618 3.917745629 2.082988e+03
-43.142550 0.222379603 -0.76418130 -0.081712307 3.525277277 1.861280e+03
-41.749618 0.233711048 -0.72667986 -0.077702356 3.244043648 1.743031e+03
-40.869022 0.245042493 -0.69017366 -0.073798824 3.016085791 1.670277e+03
-37.749811 0.256373938 -0.65456498 -0.069991263 2.642156946 1.425048e+03
-36.663785 0.267705382 -0.61976766 -0.066270458 2.429725818 1.344233e+03
-36.646568 0.279036827 -0.58570518 -0.062628228 2.295109622 1.342971e+03
-33.801248 0.290368272 -0.55230918 -0.059057264 1.996209250 1.142524e+03
-29.766931 0.301699717 -0.51951819 -0.055550992 1.653582575 8.860702e+02
-26.696234 0.313031161 -0.48727661 -0.052103467 1.390966354 7.126889e+02
-24.271531 0.324362606 -0.45553386 -0.048709282 1.182248861 5.891072e+02
-23.651448 0.335694051 -0.42424369 -0.045363489 1.072912217 5.593910e+02
-19.683427 0.347025496 -0.39336354 -0.042061540 0.827915257 3.874373e+02
-17.817835 0.358356941 -0.36285409 -0.038799229 0.691318234 3.174752e+02
-16.762094 0.369688385 -0.33267878 -0.035572645 0.596272007 2.809678e+02
-16.596960 0.381019830 -0.30280344 -0.032378138 0.537378676 2.754591e+02
-16.271207 0.392351275 -0.27319601 -0.029212277 0.475319006 2.647522e+02
-13.815798 0.403682720 -0.24382619 -0.026071824 0.360203050 1.908763e+02
-13.462160 0.415014164 -0.21466524 -0.022953704 0.309006447 1.812298e+02
-12.081520 0.426345609 -0.18568573 -0.019854987 0.239878409 1.459631e+02
-11.629207 0.437677054 -0.15686137 -0.016772858 0.195055032 1.352385e+02
-11.312669 0.449008499 -0.12816677 -0.013704604 0.155035654 1.279765e+02
-8.236558 0.460339943 -0.09957734 -0.010647596 0.087699542 6.784089e+01
-7.662789 0.471671388 -0.07106908 -0.007599268 0.058231584 5.871833e+01
-6.752801 0.483002833 -0.04261848 -0.004557105 0.030773222 4.560033e+01
-6.707262 0.494334278 -0.01420234 -0.001518626 0.010185824 4.498736e+01
-6.402439 0.505665722 0.01420234 0.001518626 -0.009722911 4.099122e+01
-5.446904 0.516997167 0.04261848 0.004557105 -0.024822110 2.966876e+01
-3.537785 0.528328612 0.07106908 0.007599268 -0.026884576 1.251592e+01
-2.824941 0.539660057 0.09957734 0.010647596 -0.030078835 7.980294e+00
-2.745208 0.550991501 0.12816677 0.013704604 -0.037621996 7.536170e+00
-0.195089 0.562322946 0.15686137 0.016772858 -0.003272200 3.805971e-02
1.399296 0.573654391 0.18568573 0.019854987 0.027782994 1.958028e+00
5.363331 0.584985836 0.21466524 0.022953704 0.123108313 2.876532e+01
6.700640 0.596317280 0.24382619 0.026071824 0.174697904 4.489858e+01
7.386314 0.607648725 0.27319601 0.029212277 0.215771059 5.455764e+01
9.099900 0.618980170 0.30280344 0.032378138 0.294637808 8.280817e+01
12.433611 0.630311615 0.33267878 0.035572645 0.442296424 1.545947e+02
16.718018 0.641643059 0.36285409 0.038799229 0.648646203 2.794921e+02
18.093192 0.652974504 0.39336354 0.042061540 0.761027520 3.273636e+02
18.801816 0.664305949 0.42424369 0.045363489 0.852915978 3.535083e+02
19.168108 0.675637394 0.45553386 0.048709282 0.933664777 3.674164e+02
19.219211 0.686968839 0.48727661 0.052103467 1.001387528 3.693781e+02
20.334434 0.698300283 0.51951819 0.055550992 1.129598008 4.134892e+02
24.909926 0.709631728 0.55230918 0.059057264 1.471112049 6.205044e+02
26.236229 0.720963173 0.58570518 0.062628228 1.643128534 6.883397e+02
30.924022 0.732294618 0.61976766 0.066270458 2.049349072 9.562951e+02
32.253952 0.743626062 0.65456498 0.069991263 2.257494854 1.040317e+03
32.529367 0.754957507 0.69017366 0.073798824 2.400629035 1.058160e+03
32.675968 0.766288952 0.72667986 0.077702356 2.538999708 1.067719e+03
33.275839 0.777620397 0.76418130 0.081712307 2.719045583 1.107281e+03
36.031430 0.788951841 0.80278966 0.085840618 3.092960242 1.298264e+03
37.147186 0.800283286 0.84263354 0.090101040 3.347000059 1.379913e+03
40.320875 0.811614731 0.88386232 0.094509548 3.810707636 1.625773e+03
44.334467 0.822946176 0.92665123 0.099084876 4.392875123 1.965545e+03
46.907165 0.834277620 0.97120790 0.103849228 4.871272904 2.200282e+03
54.418366 0.845609065 1.01778137 0.108829231 5.922308882 2.961359e+03
55.091131 0.856940510 1.06667420 0.114057239 6.283542333 3.035033e+03
55.470305 0.868271955 1.11825971 0.119573169 6.632760113 3.076955e+03
62.939597 0.879603399 1.17300649 0.125427129 7.894332885 3.961393e+03
66.478628 0.890934844 1.23151500 0.131683320 8.754126443 4.419408e+03
67.426518 0.902266289 1.29457343 0.138426027 9.333585010 4.546335e+03
67.603959 0.913597734 1.36324747 0.145769197 9.854574914 4.570295e+03
69.707122 0.924929178 1.43903134 0.153872609 10.726016772 4.859083e+03
69.843246 0.936260623 1.52411994 0.162970954 11.382420482 4.878079e+03
74.848732 0.947592068 1.62194155 0.173430814 12.981076532 5.602333e+03
112.729191 0.958923513 1.73832835 0.185875811 20.953629849 1.270787e+04
163.795081 0.970254958 1.88455395 0.201511408 33.006577315 2.682883e+04
198.660139 0.981586402 2.08767462 0.226331231 44.962993843 3.946585e+04
209.375830 0.992917847 2.45306927 0.286093929 59.901153719 4.383824e+04
Fuente: Elaboración propia

Calculo de estadistico W

W<-(sum(tabla_SW$ai_ui)^2)/sum(tabla_SW$ui2)
print(W)
## [1] 0.9413208

Cálculo del \(W_n\) y su p-value

mu<-0.0038915*log(n)^3-0.083751*log(n)^2-0.31082*log(n)-1.5861
sigma<-exp(0.0030302*log(n)^2-0.082676*log(n)-0.4803)
Wn<-(log(1-W)-mu)/sigma
print(Wn)
## [1] 3.241867
p.value<-pnorm(Wn,lower.tail = FALSE)
print(p.value)
## [1] 0.0005937472
library(fastGraph)
shadeDist(Wn,ddist = "dnorm",lower.tail = FALSE)

En este caso dado que 0.0005937 < 0.05 se rechaza la Hipótesis Nula: \(H_0: \epsilon \sim N(0,\sigma^2)\) , por lo que los residuos no siguen una distribución normal.

Usando stats (Precargada al instalar R)

salida_SW<-shapiro.test(modelo_estimado$residuals)
print(salida_SW)
## 
##  Shapiro-Wilk normality test
## 
## data:  modelo_estimado$residuals
## W = 0.94132, p-value = 0.0005937

Obtenemos el mismo resultado que en el calculo manual.

Importante, a partir de esta salida se puede calcular \(W_n\) si se llegara a necesitar:

Wn_salida<-qnorm(salida_SW$p.value,lower.tail = FALSE)
print(Wn_salida)
## [1] 3.241867